Logicus has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know how to run a script from within another script, and to capture it's output?
  • Comment on Running a script from a script and capturing its output

Replies are listed 'Best First'.
Re: Running a script from a script and capturing its output
by zentara (Cardinal) on Jul 19, 2011 at 16:37 UTC
    Read perldoc perlipc. Also see perldoc -q capture. There are several ways. The easiest is backticks.
    my $return = `path_to_script`;
    If you want more control, like getting stdout separate from stderr, and the ability to use stdin, then IPC::Open3 or IPC::Run, and many other modules are available.

    Some programs don't behave well without a real terminal, in those cases you may need to use IO::Pty.

    The IPC::Open3 (and similar modules), along with the piped-open

    my $pid = open( CHILD, " $command |" ); my $return = <CHILD>;
    have the added benefit of giving your the pid of the spawned process, in case you need to manually kill them.

    I usually use IPC::Open3, see Reading only STDERR for example.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Running a script from a script and capturing its output
by zek152 (Pilgrim) on Jul 19, 2011 at 16:28 UTC

    Capture using backticks.

    #script to be called print "hello world\n";
    #script that does the calling $output = `perl hello.pl`; print "Hello.pl printed\n\t$output"; #OUTPUT #Hello.pl printed # hello world #

    Hope this helps.

Re: Running a script from a script and capturing its output
by choroba (Cardinal) on Jul 19, 2011 at 16:28 UTC
    my $output = qx{ another_script.pl };
Re: Running a script from a script and capturing its output
by planetscape (Chancellor) on Jul 19, 2011 at 20:29 UTC