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

This code, when run as a cgi script (demo.pl, eg), does not perform the "dir"
(or does not return data to the $tmp variable, in any event).

It works in all other respects (I get a page in the browser that displays "Dir:")

Thoughts?  Thanks!!

#! c:\perl\bin\perl.exe use CGI qw(:standard :html3); use CGI::Carp qw(fatalsToBrowser); print header(); print '<html><body>'; $tmp = `dir`; if (defined($tmp)) { print "Dir: $tmp"; } else { print "Error: $!"; } print "<p>"; print "Path is $ENV{'PATH'}"; print '</body></html>';

Here's the PATH at runtime:

Path is d:\bin\;D:\oracle\ora81\bin;C:\Program Files\Oracle\jre\1.1.7\ +bin;C:\Perl\bin\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\W +bem

Replies are listed 'Best First'.
Re: backtick in perl 5.8 cgi under iis 6
by ikegami (Patriarch) on Feb 16, 2006 at 21:30 UTC

    Maybe your Perl can't locate your shell (cmd.exe), which is needed in order to run dir. I get the exact behaviour you descibe when I unset my PATH.

    If you're really trying to get a dir listing, it would be much more efficent to use glob or readdir (like neilwatson suggested).

      Agree. I used "dir" only to avoid getting tangled in the details of the real problem.
Re: backtick in perl 5.8 cgi under iis 6
by neilwatson (Priest) on Feb 16, 2006 at 21:10 UTC
    Use the function readdir instead of backticks. See perldoc -f readdir.

    Neil Watson
    watson-wilson.ca

      Hi. Actually, "dir" is a stand-in for the real external job.
      The problem is that I can't get anything to work with backticks in cgi.
        I think you are missing the point. You should never need backticks. They are a security risk. Why don't you tell us exactly what you are trying to do?

        Neil Watson
        watson-wilson.ca

Re: backtick in perl 5.8 cgi under iis 6
by mab (Acolyte) on Apr 26, 2006 at 03:33 UTC
    I've been battling with this as well. I found that my problem was that the IIS user did not have read and execute permissions on the directory containing the programs I was trying to backtick execute. Here's my test program:
    #!c:\Tools\perl\5.6.1\bin\MSWin32-x86\perl.exe use CGI qw/:standard/; $ENV{PATH} = "C:\\httpdocs;"; $| = 1; print header; print `dir`;
    Inside of C:\httpdocs, I put a copy of cmd.exe. On the C:\httpdocs directory, I gave the IIS user "read & execute," "list folder contents" and "read" permissions. After that, the test started working. Good luck, Mike P.S. If your scripts write to a file, be sure the IIS user has write permissions on the target directory.