Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Problem with CGI script.

by DigitalKitty (Parson)
on Aug 04, 2002 at 03:05 UTC ( #187439=perlquestion: print w/replies, xml ) Need Help??
DigitalKitty has asked for the wisdom of the Perl Monks concerning the following question:

Hi all.

In an effort to improve my knowledge / skill with writing CGI applications, I am experimenting with the following example from one of my perl related textbooks:
#!c:\perl\perl.exe use strict; use warnings; use CGI qw( fatalsToBrowser ); use CGI qw ( :standard ); use Fcntl qw( :flock ); my @vars = qw( REMOTE_ADDR REMOTE_PORT REQUEST_URI QUERY_STRING ); my @stuff = @ENV{ @vars }; my $info = join( " | ", @stuff ); open( FILE, "+>>log.txt" ) or die( "Could not open log.txt: $!" ); flock( FILE, LOCK_EX ) or die( "Could not get exclusive lock: $!" ); print( FILE "$info\n\n" ); flock( FILE, LOCK_UN ) or die( "Could not unlock file: $!" ); close( FILE ); if ( $stuff[3] ne "" ) { print( header( -Refresh=> '5; URL=' ) ); print( start_html( "log.txt" ) ); print( h1( "Welcome to my web site , $stuff[3]!\n" ) ); print( p( i( "You will now be redirected to our home page." ) ) ); } else { print( header() ); print( start_html( "log.txt" ) ); print( h1( "Please add a \"?\" and your name to the URL.\n" ) ); } print( end_html() );

The errors I am receiving are:
Use of uninitialized value in join or string at line 13.
Use of uninitialized value in join or string at line 13.
Use of uninitialized value in join or string at line 13.
Use of uninitialized value in join or string at line 13.
flock() unimplemented on this platform at line 17.

I am using a win32 machine, Apache 1.3.20 w/ mod_perl and ActivePerl ( 5.6.1 ).

Replies are listed 'Best First'.
Re: Problem with CGI script.
by december (Pilgrim) on Aug 04, 2002 at 03:36 UTC

    flock is a traditional unix/posix function which perl simply implements, I don't think there has been a similar function implemented in perl for windows.

    About the 'uninitialized' warnings, well... You didn't initialize those keys, that's all it's saying. I don't think it's a problem here, I get those often when not all cgi-parameters are given.

    Btw, you might want to upgrade your apache, since everything before 1.3.26 contains a nasty buffer overflow in the way chuncked encoding is handled (I don't think it's actively exploitable in windows, at least not more than an annoying DoS attack).



      flock works "fine" on Windows NT based Win32 operating systems, so it should be fine on NT4, 2K and XP. flock does not work on the 9x based Win32 operating systems, so this can't work on 95, 98 or Me. You don't say which flavour of Win32 you have, but I assume it's a 9x rather than an NT version.

      The warnings are there to suggest that you are using something that you never initialised. Perl is just making a suggestion that as they contain nothing yet, this may not be what you want.

      As you haven't loaded CGI::Carp your warnings are going to STDOUT, which is probaly what is upsetting the server. Try changing use CGI qw( fatalsToBrowser ); to use CGI::Carp qw( fatalsToBrowser );

      While not critical if you serer isn't exposed on the Internet, it's probably best as december says to upgrade Apache when you get a chance.

      UPDATE: Long paragraph broken in two to clarify statment.

Re: Problem with CGI script.
by graff (Chancellor) on Aug 04, 2002 at 03:17 UTC
    Call me old fashioned, but I think this line:
    my @stuff = @ENV{ @vars };
    might look/work better like this:
    my @stuff = map { $ENV{$_} } @vars;
    (But I'm just not familiar with the "@ENV{@vars}" syntax, so this might not be the issue...)

    update: Okay, so now that I've learned a new wrinkle of Perl syntax ("@ENV{@vars}" works just like the "map{}" construct that I had been comfortable with), I need to bow out of the discussion... I'd recommend some alternate method to verify that your set of ENV keys do in fact have values assigned to them when that line executes, but Windows CGI is another area that is foreign to me, so I wouldn't hazard a guess about the best way to do that...

    And having put "file lock" in the search box, and briefly scanning a few relevant nodes, I don't understand why you're getting that error about flock() not being implemented on your platform... unless you're dealing with some web service provider as your cgi host machine, and they have some deficiently-configured version of Perl to satisfy some sort of "policy".

    another update: Well, december may be right -- there seems to be conflicting info about flock on windows, but it looks like it really isn't there -- but it wouldn't be hard to roll your own substitute for this particular app, using a semaphore file. There was a nice article about this in TPJ back in February, I think -- you can find it here:

Re: Problem with CGI script.
by DigitalKitty (Parson) on Aug 04, 2002 at 04:40 UTC

    Thanks for the help so far. The most puzzling thing about this particular script is each time I execute it, I receive an 'Internal Server Error ( 500 )'. I upgraded the web server to 1.3.24.

    Update: I checked the Apache error log and saw this: Premature end of script headers.

    I cannot see where that error would originate. Any ideas?
      Hmmm... I wonder if "Premature end of script headers" might be what happens when the script dies with an error message before it has written any valid html header or data. Maybe if you make sure to print your header first, in some way, before that bunch of '... or die "Error message\n";' instructions -- or else make up a subroutine that you call instead of "die", so that you generate some sort of sensible output for the browser (including the error message), then exit.
Re: Problem with CGI script.
by zentara (Archbishop) on Aug 04, 2002 at 15:42 UTC
    Just in case you were wondering, your script works fine
    on linux, with apache
Re: Problem with CGI script.
by Anonymous Monk on Aug 04, 2002 at 16:07 UTC
    Comment out the flock parts and see what happens - this is what stops it. You simply cannot use flock on your system. Pat

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://187439]
Approved by graff
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2018-05-22 00:44 GMT
Find Nodes?
    Voting Booth?