perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Arnold <>
Subject A script
Date Tue, 15 Jun 2004 03:12:51 GMT


[root@scinux cgi-perl]# /usr/sbin/apachectl -v
Server version: Apache/2.0.40
Server built:   Nov 27 2003 11:04:06


I've added these changes to httpd.conf:

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all

Alias /cgi-perl/ "/var/www/cgi-perl/"
<Location /cgi-perl>
    SetHandler perl-script
    PerlResponseHandler ModPerl::Registry
    PerlOptions +ParseHeaders
    Options ExecCGI

LoadModule perl_module modules/
PerlModule Apache2
PerlModule Apache::compat

The script at the end of this post is a script from Lincoln Stein's
It runs fine when accessed with http://hostname/cgi-bin/
However, when accessed with http://hostname/cgi-perl/, I get:

Server error!
The server encountered an internal error and was unable to complete your
Error message: 
Failed opening session state file ./STATES/259344: No such file or
directory at /var/www/cgi-perl/ line 104. 
If you think this is a server error, please contact the webmaster 
Error 500

The script does try to write a new file in real time. The permissions for
the location are identical. 

For cgi-bin:

drwxr-xr-x    2 apache   apache       1024 Jun 14 20:03 STATES

For cgi-perl:

drwxr-xr-x    2 apache   apache       1024 Jun 14 18:39 STATES

I've pretty much narrowed the error down when the script
/cgi-perl/ attempts to open a nonexistent file for writing in
the STATES folder through the subroutine save_state at the very bottom of
the script. 

My question: Do I need to set some configuration to allow Apache and/or
mod-perl to create or write to files in cgi-perl/STATES/? If so, how do I
do this?

The file

#! /usr/bin/perl -w

# constants

use CGI::Apache qw(:standard);

 $session_key = path_info();
$session_key =~ s|^/||;

unless ( valid($session_key) ) {
    $session_key = generate_session_key();
    print redirect( url() . "/$session_key" );
    exit 0;

# Pull out our current CGI state variables to prevent them from being
# overwritten when we restore our previous state.
 @new_items = param('item');
 $action    = param('action');


# Add new items to the old list of items
if ( $action eq 'ADD' ) {
    @old_items = param('item');
    param( 'item', @old_items, @new_items );
elsif ( $action eq 'CLEAR' ) {

# Save the new list to disk

# Now, at last, generate something for the user to look at
print header,
    start_html("The growing list"),

<h1>The Growing List</h1>
Type a short phrase into the text fields below. Press <i>ADD</i> to
    append it to the history of the phrases that you've typed. The
list is maintained on disk at the server end, so it won't get out of
    order if you press the "back" button. Press <i>CLEAR</i> to clear
    the list and start fresh. Bookmark this page to come back to the
    list later.

print start_form,
    textfield(-name=>'item', -default=>'', -size=>'50', -override=>1),
    submit(-name=>'action', -value=>'CLEAR'),
    submit(-name=>'action', -value=>'ADD'),
    h2('Current List');

if (param('item'))
    my @items=param('item');
    print ol(li(\@items));
} else {
    print em('Empty');
print <<END;
<a href="/">Home Page</a></address>
    print end_html;

# subroutine generate_session_key
sub generate_session_key {
    my $key;
    do {
        $key = int( rand(1000000) );
    } until ( ! -e "STATE_DIR/$key" );
    return $key;
}    # end subroutine generate_session_key

# subroutine valid
sub valid {
    my $key = shift;
    return $key =~ /^\d+$/;

# Open the existing file, if any, and read the current state from
# it. We use the CGI object here, because it's straightforward to do
# so. We don't check for success or the open() call, becuse if there
# is no file yet, the new CGI(FILHANDLE) call will return an empty
# parameter list, which is exactly what we want.
sub fetch_old_state {
    my $session_key = shift;
    open( SAVEDSTATE, "$STATE_DIR/$session_key" ) || return;
    close SAVEDSTATE;

sub save_state {
    my ($session_key) = @_;
    open( SAVEDSTATE, ">$STATE_DIR/$session_key" )
      || die "Failed opening session state file $STATE_DIR/$session_key: $!";
    close SAVEDSTATE;

Report problems:
Mail list info:
List etiquette:

View raw message