homepage Welcome to WebmasterWorld Guest from 54.227.11.45
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
CGI displays Perl Script
lappert2001




msg:4422789
 7:21 pm on Feb 28, 2012 (gmt 0)

Running Debian Squeeze, Apache 2.2 and Perl 5.10.1. Just migrated from old box running Apache 1.3 and Debian Sarge.

One old html page we have ran a rudimentary pearl search script on the old box. But now whenever it's invoked, we see the text of the perl script.

The files were username:users, but I tried changing them to www-data:www-date. No change. The html file invoking the script is 755, as are the cgi-lib.pl and srch.cgi files. Perl is at #!/usr/bin/perl
Other perl scripts on the system are working.

I'd appreciate any thoughts on how to proceed. Thanks.

 

Key_Master




msg:4422794
 7:43 pm on Feb 28, 2012 (gmt 0)

What do you mean the files were usernames:users? Does cgi-lib.pl and srch.cgi execute if you access them directly?

It may be that without a file extension, Apache has no idea that those (username:users) are perl files.

lappert2001




msg:4422799
 7:58 pm on Feb 28, 2012 (gmt 0)

File owner was username, group was users, as in
"chown username:users *.*"

I changed to www-data:www-data *.* just to see if that might get it working. Sorry I wasn't clear if you thought the actual filenames were username or users.

Executing "perl srch.cgi":

perl srch.cgi
$* is no longer supported at cgi-lib.pl line 97.
Content-type: text/html

<HTML><HEAD>
<TITLE>Page Title</TITLE></head><body BGCOLOR="ffffe7" text="000000">


Same error message executing cgi-lib.pl

As to the error, looking at cgi-lib (line 97 is "$old = $*; $* =1;"

sub PrintVariables {
local (%in) = @_;
local ($old, $out, $output);
$old = $*; $* =1;
$output .= "<DL COMPACT>";
foreach $key (sort keys(%in)) {
foreach (split("\0", $in{$key})) {
($out = $_) =~ s/\n/<BR>/g;
$output .= "<DT><B>$key</B><DD><I>$out</I><BR>";
}
}
$output .= "</DL>";
$* = $old;

Key_Master




msg:4422802
 8:17 pm on Feb 28, 2012 (gmt 0)

Ok. That makes a lot more sense. cgi-lib.pl is a very old script. Instead of trying to make it work, see if you can use CGI.pm instead.

[wrcc.dri.edu...]

lappert2001




msg:4422804
 8:27 pm on Feb 28, 2012 (gmt 0)

Actually I just found that and tried it - unsuccessfully.

See the discussion at [search.cpan.org...] under "COMPATIBILITY WITH CGI-LIB.PL"

They suggest in srch.cgi, change

require("cgi-lib.pl");
to
use CGI;

and substituting

&ReadParse;
to
CGI::ReadParse();

They also suggest substitution of
print "The value of the antique is $in{antique}.\n";
but the new and old text is the same. So I left that alone.

In any case, I made the first two substitutions, and nothing changed.

Key_Master




msg:4422806
 8:32 pm on Feb 28, 2012 (gmt 0)

Did you try:

use CGI qw(:cgi-lib);

or

use CGI qw(:cgi-lib :standard);
lappert2001




msg:4422817
 8:49 pm on Feb 28, 2012 (gmt 0)

I tried both. Here's the top (not all) of srch.cgi:

!/usr/bin/perl
# srch.pl
$|=1;
#require("cgi-lib.pl");
#use CGI;
#use CGI qw(:cgi-lib);
use CGI qw(:cgi-lib :standard);
print &PrintHeader;
# &ReadParse;
CGI::ReadParse();
$delm="\t";
#########enter file names here
@file=("aaa.txt","bbb.txt","ccc.txt","ddd.txt","eee.txt","fff.txt","ggg.txt","hhh.txt");
#########

Key_Master




msg:4422826
 9:04 pm on Feb 28, 2012 (gmt 0)

I think this updated cgi-lib.pl script will solve your problem (cross fingers). It doesn't use the depricated $* special variable.

[cgi-lib.berkeley.edu...]

lappert2001




msg:4422901
 12:29 am on Feb 29, 2012 (gmt 0)

Nope still not working

Key_Master




msg:4422913
 12:58 am on Feb 29, 2012 (gmt 0)

What kind of error are you getting?

lappert2001




msg:4422914
 1:00 am on Feb 29, 2012 (gmt 0)

Backing up... when I execute srch.cgi

# perl srch.cgi
Content-type: text/html

<HTML><HEAD>
<TITLE>Page Title</TITLE></head><body BGCOLOR="ffffe7" text="000000">end
#

no warnings, nothing in error or access logs

Key_Master




msg:4422916
 1:05 am on Feb 29, 2012 (gmt 0)

I noticed that the newer version doesn't have a path to perl at the top of the script. If adding one doesn't work, maybe you can switch out the PrintVariables subroutine in the older script and replace it with the newer one.

lappert2001




msg:4422920
 1:09 am on Feb 29, 2012 (gmt 0)

FYI, here's the text of srch.cgi. It's old but it worked for many years.

#!/usr/bin/perl
# srch.pl
$|=1;
require("cgi-lib.pl");
print &PrintHeader;
&ReadParse;
$delm="\t";
#########enter file names here
@file=("ny.txt","bronx.txt","queens.txt","brooklyn.txt","richmond.txt","west.txt","nassau.txt","rockland.txt");
#########

$whichfile=$file[$in{'sel'}];
print "<HTML><HEAD>\n";
print "<TITLE>Page Title</TITLE></head>";
print '<body BGCOLOR="ffffe7" text="000000">';

########in input is not a null string then search#####
if($in{'query'}){

print '<h1><b> Page text</b></h1>';
print '<i> More Page text</i><br>';
print '<i> Even More Page text.</i><br>';
print ' <b>Page text</b> <a href="misc.html">Misc information</a>';
print '<hr>';

###############
#open online database
##############
open(FILE,"$whichfile");

$found=0;
$count=$tally;
#initialize variable to tally
#loop to look for model by name
#print $in{'query'};

while (<FILE>){
$ct++;
#@field=split($delm,$_);
#if(($_=~/^$in{'query'}/i)&&($in{'query'})){
#####test query#####
if(($_=~/$in{'query'}/i)&&($in{'query'})){

$_=~s/\#/ <b> Page text<\/b> /i;
$_=~s/\*/ <b> More page text<\/b> /i;
print $_,"<br>";
$found++
}
#####################end test query###
}

#print " $ct";

if($found <1){print'<h1>Nothing Matched</H1>'}else{print "$found Matches found<br>"};

close FILE;
}


######################GET TIME############################
($sec,$min,$hour,$monthday,$month,$year,$weekday,$yearday,$isdaylight)=localtime;
if ($min<10){$minute="0$min"}else{$minute=$min;}
##########################################################log activity.
$browser=$ENV{"HTTP_USER_AGENT"};
$host=$ENV{"REMOTE_HOST"};
$addr=$ENV{"REMOTE_ADDR"};

open(LOG,">>logfile.dat");
print LOG $month+1, '/', $monthday, '/', $year, $delm, $hour,':', $minute, $delm, $whichfile, $delm, '"', $in{'query'}, '"', $delm, $found, $delm, "Found", $delm, $browser, $delm, $host, $delm, $addr, $delm, "\n";
close LOG;
#####################

print "end";
__END__;

[edited by: phranque at 4:47 am (utc) on Feb 29, 2012]
[edit reason] edited to fix sidescroll [/edit]

lappert2001




msg:4422921
 1:10 am on Feb 29, 2012 (gmt 0)

I saw that too. Just added #!/usr/bin/perl but still not working.

lappert2001




msg:4422923
 1:15 am on Feb 29, 2012 (gmt 0)

Are you saying leave out the path to perl (on the new cgi-lib.pl), but then take the PrintVariables subroutine (approx. 40 lines to the end of the file) and put it in the old cgi-lib.pl file?

Key_Master




msg:4422925
 1:21 am on Feb 29, 2012 (gmt 0)

After seeing the code for srch.cgi, I don't see any need for that subroutine. If you're not using cgi-lib.pl for any other script, comment out the two lines in the old cgi-lib.pl file that contain $*.

lappert2001




msg:4422926
 1:26 am on Feb 29, 2012 (gmt 0)

I'll try that, but also FYI, here's the old cgi-lib.pl

#!/usr/bin/perl -- -*- C -*-
# Perl Routines to Manipulate CGI input
# S.E.Brenner@bioc.cam.ac.uk
# $Header: /cys/people/seb1005/http/cgi-bin/RCS/cgi-lib.pl,v 1.7 1994/11/04 00:17:17 seb1005 Exp $
#
# Copyright 1994 Steven E. Brenner
# Unpublished work.
# Permission granted to use and modify this library so long as the
# copyright above is maintained, modifications are documented, and
# credit is given for any use of the library.
#
# Thanks are due to many people for reporting bugs and suggestions
# especially Meng Weng Wong, Maki Watanabe, Bo Frese Rasmussen,
# Andrew Dalke, Mark-Jason Dominus and Dave Dittrich.

# see [seas.upenn.edu...] or
# [bio.cam.ac.uk...] for more information

# Minimalist http form and script (http://www.bio.cam.ac.uk/web/minimal.cgi):
# if (&MethGet) {
# print &PrintHeader,
# '<form method=POST><input type="submit">Data: <input name="myfield">';
# } else {
# &ReadParse(*input);
# print &PrintHeader, &PrintVariables(%input);
# }


# MethGet
# Return true if this cgi call was using the GET request, false otherwise
# Now that cgi scripts can be put in the normal file space, it is useful
# to combine both the form and the script in one place with GET used to
# retrieve the form, and POST used to get the result.

sub MethGet {
return ($ENV{'REQUEST_METHOD'} eq "GET");
}

# ReadParse
# Reads in GET or POST data, converts it to unescaped text, and puts
# one key=value in each member of the list "@in"
# Also creates key/value pairs in %in, using '\0' to separate multiple
# selections

# If a variable-glob parameter (e.g., *cgi_input) is passed to ReadParse,
# information is stored there, rather than in $in, @in, and %in.

sub ReadParse {
local (*in) = @_ if @_;

local ($i, $loc, $key, $val);

# Read in text
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$in = $ENV{'QUERY_STRING'};
} elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
}

@in = split(/&/,$in);

foreach $i (0 .. $#in) {
# Convert plus's to spaces
$in[$i] =~ s/\+/ /g;

# Split into key and value.
($key, $val) = split(/=/,$in[$i],2); # splits on the first =.

# Convert %XX from hex numbers to alphanumeric
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;

# Associate key and value
$in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
$in{$key} .= $val;

}

return 1; # just for fun
}

# PrintHeader
# Returns the magic line which tells WWW that we're an HTML document

sub PrintHeader {
return "Content-type: text/html\n\n";
}

# PrintVariables
# Nicely formats variables in an associative array passed as a parameter
# And returns the HTML string.

sub PrintVariables {
local (%in) = @_;
local ($old, $out, $output);
$old = $*; $* =1;
$output .= "<DL COMPACT>";
foreach $key (sort keys(%in)) {
foreach (split("\0", $in{$key})) {
($out = $_) =~ s/\n/<BR>/g;
$output .= "<DT><B>$key</B><DD><I>$out</I><BR>";
}
}
$output .= "</DL>";
$* = $old;

return $output;
}

# PrintVariablesShort
# Nicely formats variables in an associative array passed as a parameter
# Using one line per pair (unless value is multiline)

# Using one line per pair (unless value is multiline)
# And returns the HTML string.


sub PrintVariablesShort {
local (%in) = @_;
local ($old, $out, $output);
$old = $*; $* =1;
foreach $key (sort keys(%in)) {
foreach (split("\0", $in{$key})) {
($out = $_) =~ s/\n/<BR>/g;
$output .= "<B>$key</B> is <I>$out</I><BR>";
}
}
$* = $old;

return $output;
}

1; #return true

Key_Master




msg:4422930
 1:30 am on Feb 29, 2012 (gmt 0)

You'll have to comment out the two lines that use $* in sub PrintVariablesShort as well.

lappert2001




msg:4422931
 1:32 am on Feb 29, 2012 (gmt 0)

So I've put the original cgi-lib.pl back and commented out the two lines

sub PrintVariablesShort {
local (%in) = @_;
local ($old, $out, $output);
# $old = $*; $* =1;
foreach $key (sort keys(%in)) {
foreach (split("\0", $in{$key})) {
($out = $_) =~ s/\n/<BR>/g;
$output .= "<B>$key</B> is <I>$out</I><BR>";
}
}
# $* = $old;

return $output;
}

1; #return true

lappert2001




msg:4422938
 1:39 am on Feb 29, 2012 (gmt 0)

And like a broken record :(, still broken. Maybe I could sleep better if I just scrapped this section of the web site. Could the issue be in the apache config?, the vhost? the .htaccess file? elsewhere?

Key_Master




msg:4422940
 1:54 am on Feb 29, 2012 (gmt 0)

srch.cgi only uses two subroutines from cgi-lib.pl. Cut them out and make a new cgi-lib.pl using this code:

#!/usr/bin/perl

# ReadParse
# Reads in GET or POST data, converts it to unescaped text, and puts
# one key=value in each member of the list "@in"
# Also creates key/value pairs in %in, using '\0' to separate multiple
# selections

# If a variable-glob parameter (e.g., *cgi_input) is passed to ReadParse,
# information is stored there, rather than in $in, @in, and %in.


sub ReadParse {
local (*in) = @_ if @_;

local ($i, $loc, $key, $val);

# Read in text
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$in = $ENV{'QUERY_STRING'};
}
elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
}

@in = split(/&/,$in);

foreach $i (0 .. $#in) {
# Convert plus's to spaces
$in[$i] =~ s/\+/ /g;

# Split into key and value.
($key, $val) = split(/=/,$in[$i],2); # splits on the first =.

# Convert %XX from hex numbers to alphanumeric
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;

# Associate key and value
$in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
$in{$key} .= $val;

}

return 1; # just for fun
}


# PrintHeader
# Returns the magic line which tells WWW that we're an HTML document

sub PrintHeader {
return "Content-type: text/html\n\n";
}

1; #return true


If this doesn't work, I don't know what will.

lappert2001




msg:4422946
 2:05 am on Feb 29, 2012 (gmt 0)

I'm confused when you say "cut them out." Not sure if I understand. The above quoted text is the entire new cgi-lib.pl? If so, I just tried it and it doesn't work.

You've been very helpful and I very much appreciate it ... but it's getting late in the day and I need to walk away for now. (I'm sure you have other things on your plate as well).

Would it help if I sent you the URL (they call it sticky mail on this forum, right?)

Thank you

lappert2001




msg:4422963
 4:09 am on Feb 29, 2012 (gmt 0)

Now I can go to bed.

When success eludes you, always go back and read a tutorial. I did, and it seemed so basic I didn't think much about it. At this point I'm not even sure which version of cgi-lib.pl is there, but it's working.

The fix... Going back to the site's docroot .htaccess file, I noticed that "AddHandler cgi-script .cgi .pl" had been commented out. I'm not even sure it was there on the old server, but I think it had been added when dealing with includes on the home page (some were cgi, some were php). The .htaccess file always did have an "Options +ExecCGI" line and I thought that was sufficient. Obviously not.

So I uncommented the AddHandler and there it was!

Why the logs showed no error on this, I don't know. In any case, even though the solution was outside our discussion, I very much appreciate the time and effort you put in to help. Thanks again.

phranque




msg:4424475
 1:51 pm on Mar 3, 2012 (gmt 0)

i know you solved this already, but 2 things:

- you should have gotten at least an entry in the server access log for that request.
what was the status code?
nothing in the error log either?

- sometimes "hello, world!" is your friend and helps you avoid ratholes:

#! /usr/bin/perl -wT

use strict;

print "Content-type: text/html\n\n";
print <<END_OF_DOC;
<html>
<head>
<title>Hello, World!</title>
</head>
<body>
Hello, World!
</body>
</html>
END_OF_DOC

lappert2001




msg:4424513
 3:37 pm on Mar 3, 2012 (gmt 0)

Thank you for your suggestion. I did try Hello, World. There were no status codes or errors logged other than the initial "$* is no longer supported at cgi-lib.pl line 97." See top of the thread for that. but once fixed I increasingly came to think the problem was not with perl or the script. As it turned out, that was the case. By that time a new cgi-lib was in place. Thanks

lexipixel




msg:4432395
 2:17 am on Mar 23, 2012 (gmt 0)

Why the logs showed no error on this, I don't know.

Likely because without perl executing the code and ignoring the .cgi or .pl file extensions, the server just served up the raw text and figured it did it's job. The web log would probably show 200 codes for the files.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved