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

This is related to my other question. Can I step into a program called with `backticks`? Example called in programa.pl:

@z=`perl /full/path/programb.pl -option`
I need to both capture the output of programb.pl, and step into programb.pl to see why it is not completing, as it might have an error, but there is no error in @z (captured output).

Thanks!

Perl 5.8.8 on Redhat Linux RHEL 5.5.56 (64-bit)

  • Comment on Can I step into a program called with `backticks`?

Replies are listed 'Best First'.
Re: Can I step into a program called with `backticks`?
by davido (Cardinal) on Dec 03, 2014 at 18:00 UTC

    You probably just need something like Capture::Tiny so that you can easily capture not only STDOUT but also STDERR and the exit code:

    use Capture::Tiny 'capture'; my ($stdout, $stderr, @result) = capture { system( 'perl', '/full/path/to/program.pl', '-option' ) }; print "STDOUT received: <<$stdout>>\n"; print "STDERR received: <<$stderr>>\n"; print "Return value of system call was: <<@result>>\n";

    The other issue is the question of why it matters whether program.pl is called from another program, or from the command line. Run it from the command line instead and maybe you'll see the issue there too.


    Dave

Re: Can I step into a program called with `backticks`?
by Laurent_R (Canon) on Dec 03, 2014 at 17:55 UTC
    You would like to use the debuger on programb.pl, if I understand correctly. I doubt it is possible, as the backticks are forking a different process.

    But calling a Perl program from another Perl program is somewhat uncommon (although I admit that I have done it a few times, but for very specific reasons) and suggests that there may be an easier way to do it. May be you can put the content of programb.pl into a module and call the right functions of this module. If you do that, you'll be able to enter in debug mode in the functions of the module. I can't tell more, not knowing what your programs are doing and the reasons you decided to use separate Perl programs.

    Update: Note that you can redirect STDERR of your forked process into a file, to see any error message printed to standard error. Something like this:

    my @result = `perl programb.pl -option 2>error.txt`;
Re: Can I step into a program called with `backticks`?
by roboticus (Chancellor) on Dec 03, 2014 at 18:38 UTC

    bulrush:

    You probably don't need to debug into the script you call via backticks--since it's a separate process, you should be able to just run the program under the debugger. You'll need to verify that you're setting up the arguments and environment properly, but from that point it should be easy enough to debug the program and find out why it's not completing.

    Having said that, if your problem is that your spreadsheet isn't getting created, I get that problem occasionally, and usually get no error message for it. Normally it's because I'm holding something open by accident. Normally an explicit close on the workbook causes the spreadsheet to be created.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.