<?xml version="1.0" encoding="windows-1252"?>
<node id="1019188" title="Re: running string as perl script and pass into STDIN programatically" created="2013-02-17 13:43:43" updated="2013-02-17 13:43:43">
<type id="11">
note</type>
<author id="980564">
aitap</author>
<data>
<field name="doctext">
&lt;p&gt;I think that this will run your Perl code with STDIN fed and STDOUT/STDERR catched properly:
&lt;c&gt;
use IPC::Run 'run';
my ($stdout, $stderr);
run [ qw/perl -e/, $code ], '&lt;', \$args, '&gt;&gt;', \$stdout, '2&gt;&gt;', \$stderr;
&lt;/c&gt;&lt;/p&gt;

&lt;p&gt;Example run:
&lt;c&gt;$ perl
my $code = q[use feature 'say'; say for 1..10; warn 'zzz'; $z = &lt;STDIN&gt;; $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 ], '&lt;', \$args, '&gt;&gt;', \$stdout, '2&gt;&gt;', \$stderr;
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.

&lt;/c&gt;&lt;/p&gt;

&lt;p&gt;&lt;c&gt;__DATA__&lt;/c&gt; will be broken unless you use real files instead of &lt;c&gt;-e 'code'&lt;/c&gt; above. Feeding the code to STDIN (with &lt;c&gt;"\n__END__\n"&lt;/c&gt; between the code and actual STDIN) may also work, but can break in strange ways with syntax errors in code.&lt;/p&gt;


&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-980564"&gt;
Sorry if my advice was wrong.
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1019107</field>
<field name="parent_node">
1019107</field>
</data>
</node>
