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

I have Apache running on a NT box. I'm trying to use the Fcntl package in order to do some file locking in a script I'm writing. For some reason, when I include Fcntl, I get a error when loading the webpage. If I comment it out, it works fine.
#!c:/perl5/perl use Fcntl qw(:DEFAULT :flock); use CGI qw/:standard/; use strict; ...my content... sysopen(OUTFILE,$OUTFILE,O_WRONLY); #LOCK THE FILE unless(flock(OUTFILE,LOCK_EX)){ flock(OUTFILE,LOCK_EX) or die; }
Any reason this shouldn't work? The crazy thing is that it works via command line. Just doesn't serve up the page.

Replies are listed 'Best First'.
Re: Using Fcntl in CGI
by hardburn (Abbot) on Apr 08, 2003 at 14:33 UTC

    I know flock often doesn't work on Win(9[58]|ME) boxes. Not sure about NT. Try running this:

    #!perl open(TEST, '>', 'file.tmp') or die "Can't open file: $!\n"; flock(TEST, LOCK_EX) or die "Can't lock file: $!\n"; print "Passed\n"; print TEST "Hello, World!\n"; close(TEST);

    And see if you get any error messages.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      Your code worked fine as long as I include the Fcntl;
Re: Using Fcntl in CGI (20 questions)
by tye (Sage) on Apr 08, 2003 at 16:05 UTC

    You win. We haven't been able to guess what the problem is.

    Why don't you go look at your error log or even use CGI::Carp qw(fatalsToBrowser) so you can actually see the error message, since that should usually be the first step when debugging a problem, not one of the last.

                    - tye
      Thanks for your reply. I'm not trying to be adversarial here. I'm relatively new to Perl and do not know all the debugging methods. But you are correct in that I should have checked the Apache error logs sooner. I apologize for that. Here is the log that is generated. note: I changed the IP's to x's. They are not that way in the log.
      [Tue Apr 08 11:23:34 2003] [error] [client x.x.x.x] Premature end of +script headers: c:/program files/apache group/apache/cgi-bin/process_ +results.cgi [Tue Apr 08 11:23:34 2003] [error] [client x.x.x.x] Can't locate XSLoa +der.pm in @INC at C:\perl5\lib/Fcntl.pm line 61. [Tue Apr 08 11:23:34 2003] [error] [client x.x.x.x] BEGIN failed--comp +ilation aborted at C:\perl5\lib/Fcntl.pm line 61. [Tue Apr 08 11:23:34 2003] [error] [client x.x.x.x] BEGIN failed--comp +ilation aborted at c:\PROGRA~1\APACHE~1\apache\cgi-bin\PROCES~1.CGI l +ine 2.
      I'm not trying to win anything. Just trying to get my work done. I really love perlmonks and respect all of you who frequent this site and make it such a wonderful community and resource. Thanks for all the help. I'm not trying to be a jerk or seem unappreciative.

        Oh, I didn't think you were trying to be adversarial or trying to "win" anything. It was just a (lame) joke in my part.

        So modify your script to report "@INC", $INC{"Fcntl.pm"}, and $INC{"XSLoader.pm"} and run it from the command line and run it from CGI (with the line that breaks it commented out -- it will report blank values for those last two items, that is okay).

        Then you'll know where the working version is loading XSLoader.pm from, which Fcntl.pm it is using (you already know from the above which Fcntl.pm the CGI script is using), and why one finds it and the other doesn't (because "@INC" will be different between the two).

        What to do next depends on what you find.

                        - tye
(jeffa) Re: Using Fcntl in CGI
by jeffa (Bishop) on Apr 08, 2003 at 14:23 UTC
    "he crazy thing is that it works via command line. Just doesn't serve up the page."

    Sounds like you are forgetting to print the content header:

    print header();
    Be sure that is the first thing you print to STDOUT. If that is not the problem, then the next item is proper paths. Your script runs as a different user when requested as a CGI script, so you have to use full paths to the files you are trying to access.

    Next time, be sure and give us the error message as well. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      I am printing the header. I just didn't include all that in the post. The paths are correct. I comment out the sysopen and locking code and just try to leave the use Fcntl qw(:DEFAULT :flock); in and that causes the webpage to fail. I comment it out, and the page loads fine. EDIT: the error is an Internal Server Error.
        Is this the error displayed on the browser?
        I'm assuming it is....
        If so, could you please post the error printed to your Apache error_log?
Re: Using Fcntl in CGI
by Abigail-II (Bishop) on Apr 08, 2003 at 14:20 UTC
    Well, you aren't checking the return value of sysopen. Perhaps it failed. Perl will tell you so, and even the reason why. But if you opt to ignore that, strange things happen.

    Abigail

      Thanks for the tip. I've commented out the sysopen. I don't think sysopen is not what is failing.