Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by indigo (Scribe)
on Sep 16, 2000 at 03:59 UTC ( #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?
erix tu-tuu-tududu
Discipulus english has serious problems that reflect on programming terms..
[Discipulus]: ++erix
[Lady_Aleena]: Hello.
[Discipulus]: why github use 'pull request' instead of 'push request'? where is the subject?
[Discipulus]: I'm the subject or is the program? i think i always am the subject
[erix]: it an invite to pull, as I understand it

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2017-05-27 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?