Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: running string as perl script and pass into STDIN programatically

by aitap (Deacon)
on Feb 17, 2013 at 18:43 UTC ( #1019188=note: print w/ replies, xml ) Need Help??


in reply to running string as perl script and pass into STDIN programatically

I think that this will run your Perl code with STDIN fed and STDOUT/STDERR catched properly:

use IPC::Run 'run'; my ($stdout, $stderr); run [ qw/perl -e/, $code ], '<', \$args, '>>', \$stdout, '2>>', \$stde +rr;

Example run:

$ perl my $code = q[use feature 'say'; say for 1..10; warn 'zzz'; $z = <STDIN +>; $z=~y/a-z/A-Z/; say $z]; my $args = q[this is STDIN, why not?]; use IPC::Run 'run'; my ($stdout, $stderr); run [ qw/perl -e/, $code ], '<', \$args, '>>', \$stdout, '2>>', \$stde +rr; print "STDOUT: $stdout\n"; print "STDERR: $stderr\n"; __END__ STDOUT: 1 2 3 4 5 6 7 8 9 10 THIS IS STDIN, WHY NOT? STDERR: zzz at -e line 1.

__DATA__ will be broken unless you use real files instead of -e 'code' above. Feeding the code to STDIN (with "\n__END__\n" between the code and actual STDIN) may also work, but can break in strange ways with syntax errors in code.

Sorry if my advice was wrong.


Comment on Re: running string as perl script and pass into STDIN programatically
Select or Download Code
Re^2: running string as perl script and pass into STDIN programatically
by gideondsouza (Pilgrim) on Feb 17, 2013 at 19:13 UTC

    Thank you VERY much both of you for your answers :D

    Its super late at night, I gave the code a go and it works. I will try with my app immediately tomorrow.

    Quick question I was planning to use temp files, but should use File::Tempdir for the temporary directory? How should I best take care of all those files generated?

      AFAIU, both File::Temp and File::Tempdir destroy the apporpriate files/directories when their object comes out of scope (e.g. at the end of {} block where it was declared using my).
      Sorry if my advice was wrong.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1019188]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-10-26 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (151 votes), past polls