On 8/9/06, Rob Dixon <[EMAIL PROTECTED]> wrote:


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


Wow. Learn something new every day. That's a really nifty trick.

It requires you to put a lengthy heredoc in the middle of your code,
though, which could be a maintenance nightmare. A quick search didn't
run it up, but if you have a copy of the Perl Cookbook, they also
mention a module that lets you include multiple __DATA__ blocks. You
can also replicate that behavior yourself by doing something like:

   my $fake_eof = '--DATA1--';

   ## process block1 ##
   while (<DATA>) {
        chomp;
        last if /^--DATA1--$/;
        print "DATA0:\t$_\n";
   }

   ## process block 2 ##
   while (<DATA>) {
       chomp;
       print "DATA1:\t$_\n";
   }

   __END__
   line1.001
   line1.002
   ...
   line 1.100001
   --DATA1--
   line 2.001
   line 2.002
   ...
   line 2.200001


HTH,

--jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to