On 6/7/07, Paul Lalli <[EMAIL PROTECTED]> wrote:
On Jun 7, 10:53 am, [EMAIL PROTECTED] (Rodrick Brown) wrote:
> Is there a simple method to read data from scalar like a file handle?

Yes.  Simpler than you might imagine.  Just pass a reference to your
scalar to open(), rather than the name of a file.

#!/usr/bin/perl
use strict;
use warnings;

my $data = "lots of random\ntext including\newlines\n";
open my $fh, '<', \$data
     or die "Cannot open data stream: $!";

print while <$fh>;
__END__

Paul Lalli
snip

Nifty, I didn't remember this at all, so I went back and reread the
perldoc for open.  Apparently it was added with 5.8.  So, amend my
answer to if your code might run on a pre-Perl 5.8 interpreter use
IO::String, otherwise use the new functionality.

from perldoc -f open
              Since v5.8.0, perl has built using PerlIO by default.  Unless
              you've changed this (i.e. Configure -Uuseperlio), you can open
              file handles to "in memory" files held in Perl scalars via:

                  open($fh, '>', \$variable) || ..

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to