sierrathedog04 has asked for the wisdom of the Perl Monks concerning the following question:

I have a script which works great when I read from a file:
open (MYFILE, 'mypath'); while (<MYFILE>){ #my code here }
I would like to be able to use this same syntax when the text to be processed is in the form of a string rather than a file. Can I associate a string with a virtual filehandle? Ideally the syntax would be something like:
#magically turn $myString into a filehandle. while (<MYSTRING>){ #my code here }

Replies are listed 'Best First'.
Re: Can Perl Turn A String Into A Virtual File?
by Masem (Monsignor) on May 25, 2001 at 00:35 UTC
    Look at the module IO::String, while allows you to treat a string as an IO::File handle, which act just like your typical perl file handles.
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      I believe it's named IO::Stringy.

      -Lee

      "To be civilized is to deny one's nature."
(boo) Re: Can Perl Turn A String Into A Virtual File?
by boo_radley (Parson) on May 25, 2001 at 00:27 UTC
    figure out what your "virtual newlines" would be,and split on them, so you can go back to
    while (@lines) {...}
    not very magical, but hey.
Re: Can Perl Turn A String Into A Virtual File?
by Big Willy (Scribe) on May 25, 2001 at 00:59 UTC
    If you're looking to be more ambiguous (bonus!), the way to go would be to implement as mentioned by the others:
    while(@lines) { ... }
    Then, if you want to use a file, you can do
    @lines = <MYFILE>;
    or for a string
    @lines = split /\n/, $string;
    prior to executing the loop. Setting up a string to work via a file handle is a little extreme, unless you need it to work in both cases and the files you are working with are too large to be worth reading in in a single gulp. This is one of the things the IO::String is supposed to do, and the thing you would need to do to use the form you originally proposed. But IO::String, according to its documentation, does not yet have this feature implemented! So, unless have huge files, just go with the the way of the array.
Re: Can Perl Turn A String Into A Virtual File?
by bikeNomad (Priest) on May 25, 2001 at 10:21 UTC
    Sure, you can use IO::Scalar:
    use IO::Scalar; my $myString = "stuff here\nmore stuff"; my $fh = IO::Scalar->new(\$myString); while <$fh> { # yer code here }
Re: Can Perl Turn A String Into A Virtual File?
by DBX (Pilgrim) on May 25, 2001 at 00:40 UTC
    Anything after the __DATA__ token can be read with the <DATA> filehandle, like:
    while(<DATA>){ # do something } __DATA__ Process here, line 1 Process here some more, line 2
Re: Can Perl Turn A String Into A Virtual File?
by Anonymous Monk on May 25, 2001 at 04:01 UTC
    my $myString = "This is a test\nthis is only a test\n"; pipe(READER, WRITER); if(fork()) #should have some error checking { close(WRITER); while(<READER>) { print "I just read: $_"; } } else { close(READER); print WRITER $myString; close(WRITER); exit; }