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

I'm trying to execute a command from within my perl script(cgi application) and retrieve the output using tics to surround my command. Since what I was trying wasn't working (a larger command and supplying parameters) I tried running it for just dir and it still failed.
$newTest = `dir`; print "String back : " . $newTest;
I get the error "Use of uninitialized value $newTest in concatenation (.) or string at <scriptname>...." What could possibly cause the error and how can I fix the problem? Thanks.

Replies are listed 'Best First'.
Re: Executing / Retrieving Output from Command
by Corion (Patriarch) on Mar 15, 2010 at 17:57 UTC

    dir likely is not an external program but a shell built-in. Thus your backticks fail, and thus $newTest is undef. You can inspect $? and $! for the error(s). Maybe you meant to run cmd /c dir, or maybe you wanted readdir, glob or File::Find?

      Perl automatically tries cmd /c $command is executing $command fails (or something like that), so `dir` actually does work on Windows.

      I consider that a bug since it harms error reporting.

      >perl -e"print `dir /b`" bar foo Windows: >perl -le"`nonexistant`; print $?; print $! if $?==-1" 'nonexistant' is not recognized as an internal or external command, operable program or batch file. 256 unix: $ perl -le'`nonexistant`; print $?; print $! if $?==-1' -1 No such file or directory
Re: Executing / Retrieving Output from Command
by toolic (Bishop) on Mar 15, 2010 at 18:03 UTC
    Your script works for me on windoze if I run it from the command line (not from a cgi app). Does it work from the command line for you?

    Maybe something in here can help: Troubleshooting Perl CGI scripts

Re: Executing / Retrieving Output from Command
by Khen1950fx (Canon) on Mar 16, 2010 at 00:24 UTC
    You can use IPC::System::Simple to do what you want:
    #!/usr/bin/perl use strict; use warnings; use IPC::System::Simple qw(capture); my $results = capture('gunzip -cf /usr/lib/perl5/5.8.8/CGI.pm'); print $results;
    The capture method replaces the backticks.
      I'm using Activestate version of Perl and download modules using ppm. When I try loading that module it gives me an error back, 403 forbidden error. Is there another approach to take to accomplish the same thing? Thanks for the message.
Re: Executing / Retrieving Output from Command
by rpike (Scribe) on Mar 15, 2010 at 19:57 UTC
    My apologies, I was sure I had posted the few lines I had in the script. There are only a couple of lines in it because all it's doing is taking a .gz file and trying to unzip it. I tried this :
    results = `gunzip -cf <C:/temp/decrypt/out-3908.gz`; print $results;
    and this to see
    eval("results = `gunzip -cf <C:/temp/decrypt/out-3908.gz`"); print $results . " | " . $!;
      eval("results = `gunzip -cf <C:/temp/decrypt/out-3908.gz`");

      will likely be an error, because the backticks get interpolated in your double quotes. So unless gunzip -cf <C:/temp/decrypt/out-3908.gz returns Perl code, that won't work.

      How big is the file you're unzipping?


      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.
Re: Executing / Retrieving Output from Command
by rpike (Scribe) on Mar 15, 2010 at 18:16 UTC
    When I wrap the whole gunzip command and parameters in tics I get nothin in the returned string and $! gets set to "Inappropriate I/O control operation".

      First, could you post the code you actually used?

      It might also help if you said which platform you are using. (Windows, Cygwin, unix)

      What's $?? $! is only meaningful if $? is -1.

Re: Executing / Retrieving Output from Command
by rpike (Scribe) on Mar 15, 2010 at 20:08 UTC
    Okay. So $results = 'gunzip -cf filepathspecified'; returns the error I mentioned. What can I do to get around this or correct the problem?