Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Running a backticks command in a CGI script

by Cody Fendant (Hermit)
on Apr 14, 2013 at 01:52 UTC ( [id://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 (Archbishop) 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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1028584]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found