Octavian Rasnita wrote:
> Hi,
>
> I have a program that contains a pretty big block of text:
>
> my $text = <<EOF;
> line1
> line2
> ...
> line 120000
> EOF
>
> I want to read this block of text line by line and analyse each line without
> needing to create a big array that contains all these lines (exactly like
> when reading line by line from a text file).
>
> It works fine if the program contains just a block of text, because in that
> case I can put the data after __DATA__ and then use while(<DATA>), but the
> program contains 2 blocks of text.
>
> Is there a solution for this?

In Perl 5.8 and onwards you can read directly from the string as if it were a
file by just opening with a scalar reference instead of a filname. That seems to
be exactly what you want.

HTH,

Rob



use strict;
use warnings;

die "Too old a version of Perl" unless $] >= 5.008;

my $text = <<EOF;
line1
line2
...
line 120000
EOF

open my $fh, '<', \$text or die $!;

while (<$fh>) {
  print;
}

OUTPUT

line1
line2
...
line 120000

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


Reply via email to