Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Answer: How do I treat a string like a filehandle?

( #32768=categorized answer: print w/ replies, xml ) Need Help??

Q&A > strings > How do I treat a string like a filehandle? - Answer contributed by indigo

Use a pipe:
#!/usr/bin/perl -w use FileHandle; use strict; my $string =<<EOD; One fish Two fish Red fish Blue fish EOD my $fh = new FileHandle("echo \'$string\' |") or die; print $_ while <$fh>;

Replies are listed 'Best First'.
RE: Answer: How do I treat a string like a filehandle?
by merlyn (Sage) on Sep 16, 2000 at 04:25 UTC
    No point in invoking a shell there, when this will do:
    my $fh = FileHandle->new or die "Cannot create filehandle: $!"; defined(my $pid = open($fh, "-|")) or die "Cannot fork: $!"; unless ($pid) { print <<'EOD'; One fish Two fish Red fish Blue fish EOD exit 0; } print $_ while <$fh>; # in parent
    But again, this forks (as does yours), and it's much better to use IO::Stringy to do it all within one process.

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2016-07-29 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?


















    Results (260 votes). Check out past polls.