Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Running a backticks command in a CGI script

by Cody Fendant (Friar)
on Apr 14, 2013 at 01:52 UTC ( #1028584=perlquestion: print w/replies, xml ) Need Help??
Cody Fendant has asked for the wisdom of the Perl Monks concerning the following question:

I have a command foobar which runs just fine from the command line as a normal user.

When I run foobar it normally takes about 5 seconds, then returns "success".

I want to run the command in a CGI script and print the "success" text back to the browser.

If I put $result = `foobar` into my CGI script, it finishes instantly (no 5-second wait) and $result is empty.

How do I troubleshoot this? If the script couldn't find foobar in the path, I would at least get something like "unknown command" back, right?

So the command is silently failing, perhaps because of permissions, because it's running as the www username? What can I do to figure out what's happening and how to get it to work?

Replies are listed 'Best First'.
Re: Running a backticks command in a CGI script
by NetWallah (Canon) on Apr 14, 2013 at 05:18 UTC
    Try capturing STDERR in addtion to the STDOUT output:
    $result = `foobar 2>&1`
    You should also check the value of "$?". From the docs:
    $? may be set to non-0 value if the external program fails. The upper eight bits reflect specific error conditions encountered by the program (the program's "exit()" value).

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

      That's done the trick, thank you. I have a full, detailed and ... incomprehensible error message now. But that's cool, I've solved the immediate problem.
Re: Running a backticks command in a CGI script
by kcott (Chancellor) on Apr 14, 2013 at 06:02 UTC

    G'day Cody Fendant,

    Yes, I can make that silently fail too:

    $ perl -e '$result = `foobar`'

    But I start getting feedback if I ask for it (i.e. with strict and warnings):

    $ perl -Mstrict -Mwarnings -e '$result = `foobar`' Global symbol "$result" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.

    OK, let's fix up that one from strict:

    $ perl -Mstrict -Mwarnings -e 'my $result = `foobar`' Can't exec "foobar": No such file or directory at -e line 1.

    Now this one from warnings which clears all errors:

    $ perl -Mstrict -Mwarnings -e 'my $result = `ls`'

    Given you're working in a CGI environment, you may find the fatalsToBrowser feature of CGI::Carp to be useful.

    -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1028584]
Approved by kcott
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2018-05-21 02:10 GMT
Find Nodes?
    Voting Booth?