Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by BrowserUk (Patriarch)
on Feb 17, 2013 at 06:38 UTC ( [id://1019109]=note: print w/replies, xml ) Need Help??


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

Do I have this straight?

From one running perl program, you want to invoke a second copy of perl running a second, pre-existing perl program, pass it a 3rd perl program, in the form of a single string, that it will then pass to a 3rd copy of perl to run.

And the problems you are having are:

  1. How to pass a single(?) switch from the first program to the second so that it can give it to the third copy of perl as a command line argument.

    Assuming $arg was 'x', this :open(my $fh,'|-', 'perl', '-', $arg); is equivalent to typing this at the command line:

    perl - x

    Which is obviously wrong. Try: open(my $fh,'|-', 'perl', "-$arg" );

  2. How to capture the STDERR output.

    From which instance of Perl?

    You are invoking the second copy of perl with command line redirection, which means the first program should receive all stdout and stderr output from the second.

    But any output from the third instance of perl will be going to its (virtual) console. Is it the STDERR output from this process you are having trouble capturing?

    Because without trying it out, I could not even guess aa to what would happen to it on my OS; never mind on any other. At that point you have a hierarchy of processes like this:

    <someshell> +-- perl +-- <someshell> (because of the command line redirection used +in backticks in the first program.) +-- perl +-- perl

    And you are hoping the command line redirection in second level of shell, will capture both stdout & stderr output from the third level of perl.

One question: why? (Including, but not limited to:why not just embed the 10 lines of code in second perl program in the first? Wrapped over as a function if necessary.)

Another question: Could you post an example of the program that is being fed to the 3rd level perl?

Update:Another question: Why? :)


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: running string as perl script and pass into STDIN programatically
by gideondsouza (Pilgrim) on Feb 17, 2013 at 08:10 UTC

    So I'll just start from the use case :)

    I'm developing a little pet project project here : www.tryperl.com

    I'm a linux/perl beginner (touched it for the first time 3 months ago) but I want to really learn it well, so this is my learning project

    My problem:
    Given C (=perl code in a string) and A(=argument) I want to evaluate C and just return STDOUT and STDERR (the output of the program, or an error if it occurred). I also want to do this within a timeout.

    So : my ($stdout, $err) = eval_perl($program, $args); What would be the simplest way of writing eval_perl with good performance?

    I did just have a loong discussion on irc #perl and plan on building better security processes later on, but I don't know enough right now. I run the code in a EC2 vm now so I'm pretty safe. So I just want to stick to the above use case.

    I also have a list of things I've tried and my issues here on stackoverflow. No answers yet :( I tried capture::tiny and also App:EvalServer I'm told executes one liners, I want to exec the whole some_script.pl program at once.

Re^2: running string as perl script and pass into STDIN programatically
by gideondsouza (Pilgrim) on Feb 17, 2013 at 08:41 UTC

    I sort of have an approach, this is how I plan do things, do you see issues, (performance etc?) or any other flaws?

    my $fh = File::Temp->new(); my $fname = $fh->filename; print $fh $code; close $fh; my $retVal = "nothing"; $retVal = timeout 5 => sub { my $r = `perl $fname $arg &2>1`; return $r; }; if ($@){ return $@; } return $retVal;

    My issue is that if $code has an error, that error is printed into the dancer bash console, I want to capture it instead.

    Update

    I also tried using capture_exec from IO::CaptureOutput, but this time, I get the STDERR but STDOUT is instead printed onto the bash command line, if there was a success :( *sigh*.

    I did ($stdout, $stderr, $success, $ex) = capture_exec('perl', "$fname","$arg");

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-03-19 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found