http://qs321.pair.com?node_id=173257

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

I have activestate perl installed on a w2k server running iis. Scripts run from the commandline open files without difficulty, but not when run as a cgi... comments?
  • Comment on activestate opens files from commandline but not when executed as a cgi

Replies are listed 'Best First'.
Re: activestate opens files from commandline but not when executed as a cgi
by Ovid (Cardinal) on Jun 10, 2002 at 19:03 UTC

    Remember to add an "or die: $!" (or croak) after the open. That will write a useful message to the error log.

    my $file = 'somedir/somefile.dat'; open FILE, "< $file" or die "Cannot open $file for reading: $!";

    The problem is likely bad permissions. You run from the command line as a different set of permissions from the Web server. It's also possible that if you're running the file from a different location, your paths could be wrong and the system can't find the file specified.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      To elaborate, I am using absolute, not relative paths, I do have a die after the open, no message is printed. The server is running as LOCAL_USER, the file permissions are set such that any user has full privileges on the file its parent directories.
        Where are you looking for the error messages?

        Try
        use CGI::Carp qw(fatalsToBrowser);
        to see the error in the browser, or check the server error logs.

Re: activestate opens files from commandline but not when executed as a cgi
by dree (Monsignor) on Jun 10, 2002 at 19:20 UTC
    Are you using relative paths to read the file?

    IIS doesn't like relative paths... try absolute ones.
    For example, try:
    ($relative_path=$ENV{SCRIPT_NAME}) =~ s|[^/]+$||; $absolute_path=$ENV{PATH_TRANSLATED}.$relative_path; $filename=$absolute_path.'file.txt';
Re: activestate opens files from commandline but not when executed as a cgi
by feanor (Initiate) on Jun 10, 2002 at 21:34 UTC
    THE ANSWER IS: use single quotes, not double quotes in the open() statement.

    So:
    open(HEADER, 'D:\path\to\file\file.txt') || die ("$!"); This works!

    NOT: open(HEADER, "D:\path\to\file\file.txt") || die ("$!"); This doesn't work!
      or escape the slashes, of course