in reply to I want to run a script from another script, use the same version of perl, and reroute IO to a terminal-like textbox

I second what jethro said. Maybe use a Modulino (Example from 'Mastering Perl' - see link at end of article) ?

But since you asked... (because it is possible, not recommended):

use strict; use warnings; our $exchange = "before (set by caller)"; my $other_file = "this_other_thing.pl"; my ($out, $err) = redir_file( $other_file ); #-- proof of re-direction and import/export print "Caller: GOT from STDOUT ($other_file): ", join("\n\tOUT> ", "", split( /\n/, $out) ), "\n"; print "Caller: GOT from STDERR ($other_file): ", join("\n\tERR> ", "", split( /\n/, $err) ), "\n"; print "Caller: Exchange : $exchange\n"; sub redir_file { my $do_filename = shift; #-- we DUP the current STREAMS and then we close the original stream +s... open(my $oldout, ">&STDOUT") or die "Can't dup STDOUT: $!"; open(my $olderr, ">&STDERR") or die "Can't dup STDERR: $!"; close STDOUT; close STDERR; #-- now, we re-open the streams with redirection into strings... my $redir_stdout; my $redir_stderr; open( STDOUT, '>', \$redir_stdout ) or die "Cannot redirect STDOUT t +o string: $!"; open( STDERR, '>', \$redir_stderr ) or die "Cannot redirect STDERR t +o string: $!"; #-- this is like "do"... and exactly as insecure... my $do_file_content = qx{cat $do_filename}; # e.g. use Slurp... my $read = eval $do_file_content; # =8-O #... now $exchange might have been updated #-- now, we restore the previous streams... open(STDOUT, ">&", $oldout) or die "Can't dup \$oldout: $!"; open(STDERR, ">&", $olderr) or die "Can't dup \$olderr: $!"; return ($redir_stdout, $redir_stderr); }

Modified this_other_thing.pl (our...)

#!/usr/bin/env perl use strict; use warnings; use utf8; our $exchange; print STDOUT "PRG: Hello World!\n"; print STDERR "PRG: Hello Err!\n"; print "PRG: Exchange: ", ( $exchange // '(nothing)'), "\n"; $exchange = "after (called program)";

Result:

Caller: GOT from STDOUT (this_other_thing.pl): OUT> PRG: Hello World! OUT> PRG: Exchange: before (set by caller) Caller: GOT from STDERR (this_other_thing.pl): ERR> PRG: Hello Err! Caller: Exchange : after (called program)

Don't know if this works well for Windows and in threaded/forked environments...

  • Comment on Re: I want to run a script from another script, use the same version of perl, and reroute IO to a terminal-like textbox
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: I want to run a script from another script, use the same version of perl, and reroute IO to a terminal-like textbox
by ric00015 (Beadle) on Oct 01, 2013 at 16:41 UTC

    The modulino concept looks neat, and that may even be the best solution, but I may not be able to do that. Especially if I have to change the name of the script.

    That code works. I had to massage it a bit to work with the Tk, and display in the text box, but it works. Can this also do stdin too? Some scripts may have y/n options that the user needs to select.

      Hello ric00015, redirecting STDIN is a little bit harder due to blocking I/O. You might be able to fork a sub-process in the TK program that feeds the external program (which is still running in the same parent process) -- ultra fragile!
      Since you can modify the external script, you might be able to read alternatively from STDIN or an @EXT_INPUT array exported from the Tk program to the other one -- and again -- better do not follow/implement this concept!

      I strongly suggest to refactor your Perl program collection.
      You get something that is easier to maintain and to test. It's a little bit more work in the beginning, though - but if you have to extend and maintain this collection for a while, it will return your investment soon.

      Your modified version of the script would use a module and than invoke the sub or object-method with parameters taken from the command-line or ENVironment. Your current programs become wrapper, not Modulinos.
      Perhaps you should also have a look at App::Cmd for further inspiration?

      Update: Perhaps also useful: OT: Rewrite or Refactor?

        Yeah, the module solution by jethro seems to be the best solution. I'll have to pester the guy who originally wrote the scripts, but that is a much better solution than the code you presented (no offense :) ). The module approach should solve all my problems.

Re^2: I want to run a script from another script, use the same version of perl, and reroute IO to a terminal-like textbox
by ric00015 (Beadle) on Oct 01, 2013 at 17:00 UTC

    Oh, and the environment is Linux, with a non-threading version of Perl.