Here is an example from the docs for IO::Scalar:
### Open a handle on a string, read it line-by-line, then close it +: $SH = new IO::Scalar \$data; while (defined($_ = $SH->getline)) { print "Got line: $_"; } $SH->close;
I know you were looking for an approach that uses or acts like a filehandle, but here is something that uses split instead:
use warnings; use strict; my $string = "This is some text\nto be processed as\na file"; # use a zero-width positive look-behind assertion to split # the string on whatever is used as the input record # separator ($/) without consuming it foreach my $line ( split( m[(?<=$/)], $string ) ) { print $line; } # or, if you prefer the one-liner look print for split ( m[(?<=$/)], $string );
Update: Added the alternate form of the for/split loop.
In reply to Re: Converting a String to a FileHandle
by bobf
in thread Converting a String to a FileHandle
by monkfan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |