Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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? 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>;

Comment on Answer: How do I treat a string like a filehandle?
Download Code
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 pondering the Monastery: (2)
As of 2015-07-06 01:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls