[Israel.pm] convert perl string to file handle

Mikhael Goikhman migo at homemail.com
Sun Jun 6 06:18:10 PDT 2004


On 06 Jun 2004 16:03:39 +0300, Itamar Elem wrote:
> 
> is there a efficinet way to convert perl string to file handle?
> i used something like:
> $buffer="....."
> open IN,qq{echo $buffer|};
> 
> print <IN>;
> 
> but is there a better way?

Yes, this is a pretty hacky way to achieve what you want, there is a fork
and you should at least shell-quote the command, and it is almost
impossible to do correctly.

With new perl versions (check out perldelta and perlfunc man pages) you
may read and write from/to the scalar directly. Use:

  my $buffer = `cat /etc/passwd`;
  open(IN, '<', \$buffer);
  print scalar <IN> for 1 .. 5;  # print first 5 lines of scalar $buffer

See "perldoc -f open".

Regards,
Mikhael.

-- 
perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'



More information about the Perl mailing list