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

I would like to construct a web page that uses perl to run a unix shell command. I'm sure this is really simple but then so is my skill level with perl as well. Thank you, Michael

Replies are listed 'Best First'.
Re: Using perl to call a shell command?
by Zaxo (Archbishop) on Mar 07, 2003 at 21:16 UTC

    Depending on what you want, system, qx// (`backticks`), or (unlikely) exec. Look at the perldocs.

    After Compline,
    Zaxo

Re: Using perl to call a shell command?
by CukiMnstr (Deacon) on Mar 07, 2003 at 21:20 UTC
    you could use system(), exec() or `` (the backtick operator). Each has different uses, so read the docs to decide which one suits your needs.

    good luck,

Re: Using perl to call a shell command?
by grantm (Parson) on Mar 07, 2003 at 22:17 UTC

    If you decide to use system rather than backticks, you might want to set $| = 1 to avoid buffering problems.

    Also bear in mind that any shell command you run will be running under the web server's user ID and it's environment will be different to when you run the same command from a shell prompt.

      How is setting $| = 1 going to avoid buffering problems when calling system() on a non-Perl program?

      I believe you are referring to fork(), and exec(), not system(). I also believe that modern versions of Perl include special code in fork() and exec() to implicitly flush all open file handles.

      UPDATE: After discussing with grantm, the problem that he was dealing with in the past was a parent process expecting output from the perl script and a child of the perl script in the proper order, such as would be the case for a CGI that wishes to insert the output of a shell command directly into the HTML at a certain point. In this case, in order for the parent to see the output in the correct order, the output must be flushed before the shell command is executed. At least Perl 5.8.0 does this automatically for all systems that support the 'flush all handles' semantics (often fflush(NULL)). I recommend not using auto-flushing for CGI handles as it unnecessarily breaks the writes up into separate system calls, leading to inefficient web page serving. Flush when necessary, and let Perl autoflush when necessary.

        Wow! I am lost already. What I am after most specifically is to have a web page with an entry blank for a specified IP address entry. When the user submits the request, the page instructs the system to run a command with the IP address inserted into the command on the system. This command can be assigned the proper UID credentials and is also something I would normally run from the command line as a regular user. I will be calling nmap from the web page. Your lofty assistance is appreciated.
        I believe you are referring to fork(), and exec(), not system()

        So if you don't think system() does a fork() and an exec(), what do you think it does?

        Update: Sorry MarkM I hadn't taken the time to understand your point properly. My experience of 500 server errors resulting from no autoflush dates back possibly as far as 5.001 and may have only been under IIS 2.0 on Windows - it's a habit I've carried for a while and can probably drop. The conclusion: Don't mess unnecessarily with $| in your CGI scripts - it won't improve efficiency.