Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Running a backticks command in a CGI script

by Cody Fendant (Pilgrim)
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?

Comment on Running a backticks command in a CGI script
Select or Download Code
Re: Running a backticks command in a CGI script
by NetWallah (Abbot) 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 (Abbot) 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
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? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2014-09-20 22:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (163 votes), past polls