PERL! Help plz :<

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
I have run out of ideas , basically when i run this script from the console it is fine outputs the right html.
But as soon as i run it from a browser i get errors :/
the script is essentially the same as one called Named Artist, with the only difference being the table it refers to in the database.
Now the SQL Query works
the Perl script works in the console
the HTML is identical to a working version bar refering to a different perl script

For the sake of testing i hard coded some data to test if the tables work.

Code for my html page:
Code:
<html>

<head>

<title>Select a player form</title>

</head>

<body>

<h2>Select a player form</h2>

<FORM METHOD="POST" ACTION="http://www2.wmin.ac.uk/~w0402086/cgi-bin/TotalSales.pl">

<p><h3>
Enter the name of a player to obtain their full details
</h3></p>

<center>

<p>Day
<input type="text" name="Day" size="2" maxlength="2"/>
</p>

<p>
Month<input type="text" name="Month" size="2" maxlength="2"/>
</p>

<p>
Year<input type="text" name="Year" size="2" maxlength="2"/>
</p>

<input type="submit" name="Sub" value="Send query"/>

<input type="reset" value="Reset Form"/>

<input type="hidden" name="HiddenName" value="selectform.html"/>

</center>
</form>

</body>

</html>

Code for my perl script
Purposfully left out the stuff to setup the enviroment
Code:
if ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
} else {
$buffer = $ENV{'QUERY_STRING'};
}
@pairs= split(/&/,$buffer);
foreach $pair (@pairs) {                    
   ($name,$value) = split(/=/,$pair);                            
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
    $contents{$name} = $value;
    }
#$Day = $contents{'Day'};
#$Day =~ s/$regexp//g;   #  remove suspect chars
#$Month = $contents{'Month'};
#$Month = =~ s/$regexp//g;   #  remove suspect chars
#$Year = $contents{'Year'};
#$Year =~ s/$regexp//g;   #  remove suspect chars

$Day = '12';
$Month = '12';
$Year = '03';

my $dbh = DBI->connect("dbi:Oracle:ORA8", "username","password")
		or die "Cannot connect: " .  $DBI::errstr;
my $sql = "SELECT SaleDate /*, COUNT(*) Total_Sales*/
FROM Sales
WHERE SaleDate = to_date('$Day/$Month/$Year' ,'DD/MM/YY')
GROUP BY SaleDate";
my $sth = $dbh->prepare($sql) or die "Cannot prepare: " . $dbh->errstr();

if ($sth->execute()) {
my @data;
my $count = 0;
print "Content-Type: text/html;charset=ISO-8859-1\n\n";
print "<html>\n<head>\n<title>Recording Search</title>\n</head>\n";
print "<body bgcolor=\"peachpuff\"><h1>Artist's Recordings</h1>\n";
print "\n<TABLE BORDER=1 ALIGN=CENTER>";
print"<tr><th>Artist's Name</th></tr>\n";
while (@data =$sth->fetchrow_array())
{ ++$count;
    print "<tr bgcolor=ffddbb>\n";
    foreach my $element (@data) {
      print "<TD>$element\n</TD>";
      }
      #print"<br></br>";
      print"</TR>";
 }
   print "</TABLE>"; 
 }
 
 print "</body>\n</html>\n"; 
$sth->finish();
$dbh->disconnect();

Output in console:
Code:
58 tiger% perl TotalSales.pl
Content-Type: text/html;charset=ISO-8859-1

<html>
<head>
<title>Recording Search</title>
</head>
<body bgcolor="peachpuff"><h1>Artist's Recordings</h1>

<TABLE BORDER=1 ALIGN=CENTER><tr><th>Artist's Name</th></tr>
<tr bgcolor=ffddbb>
<TD>12-DEC-03
</TD></TR></TABLE></body>
</html>

Any help would be much appreciated
 

Lamp

Gold Star Holder!!
Joined
Jan 16, 2005
Messages
23,001
Have you tried dry-running it on paper ?
Are you calling wrong subroutines ?
Using invalid variables ?
All your objects correctly utilized ?

I'm not a PERL expert. But - waaaaay back - I used to program in assembly code. Done some VB and c#.net
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
i put in print statements to see what values appeared along the way in the terminal, all are as they should be.
 

Cylian

Fledgling Freddie
Joined
Jan 17, 2004
Messages
2,336
Just guessing a bit here, but

is the Server Application for the script running?
do you run it from the correct URL ?

the browser can't interpret the PERL code if there's no server for it running and properly set up, could be one reason. Do other scripts work ?

for the url, if you just double-click to open the page, your URL is usually something like "http://c:\mydir\whatever\script.pl", but to run scripts you usually need to use something like http: //localhost/whatever/script.pl
Just incase you run it on your local machine.

If you uploaded it to a webserver and it doesn't work there, eventually it's in the wrong directory, or the directory isn't allowed for scripts.


would help if you could tell us in what kind of setup you try to run it (local machine, webserver) and if other scripts work.
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
its a uni setup,
so there are webservers the scripts are run on, and the results are returned in the form of a html page.
I dont have any control of the system whatsoever aside from setting permissions on the files in my home directory.

I have other scripts very similar to this one that work fine done in exactly the same way, i did add a line at the start to pipe any errors into a file called errors.txt
but this file is clean
 

Cylian

Fledgling Freddie
Joined
Jan 17, 2004
Messages
2,336
what happens if you reduce it to the query, with fixed values, and let it run in the browser ?

What do you get back as data ?

Suppose you did set the rights for the files correctly, right?

What does the source look like after hitting submit, or does just nothing happen?
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
with just the query it gives the same error,
here is a working version
HTML:
Code:
<html>

<head>

<title>Select a Artist form</title>

</head>

<body>

<h2>Search for Artist's Recordings</h2>

<FORM METHOD="POST" ACTION="http://www2.wmin.ac.uk/~w0402086/cgi-bin/NamedArtist.pl">

<p><h3>
Enter the name of a Artist to obtain a list of their recording's
</h3></p>

<center>

<p>
Artist_Name<input type="text" name="Artist_Name" size="25" maxlength="16"/>
</p>

<input type="submit" name="Sub" value="Send query"/>

<input type="reset" value="Reset Form"/>

<input type="hidden" name="HiddenName" value="selectform.html"/>

</center>
</form>

</body>

</html>

Perl:
Code:
#!/usr/local/bin/perl -wT
$oracle_home = "/oraclient/product/v8.1.7";

$path = "/oraclient/product/v8.1.7/bin:/usr/bin:.:/usr/dt/bin:/usr/openwin/bin:/bin:/usr/ucb:/usr/local/bin:/usr/bin:/bin:/usr/ccs/bin:/opt/SUNWspro/bin:/opt/SUNWspro_4.0/bin:/usr/openwin/bin:/opt/gnu/bin:/usr/local/bin:/usr/sbin:/usr/local/bin:/usr/bin:/bin:/usr/ccs/bin:/opt/SUNWspro/bin:/opt/SUNWspro_4.0/bin:/usr/openwin/bin:/opt/gnu/bin:/usr/local/bin:/usr/sbin";

$tns_admin = "/oraclient/product/v8.1.7/network/admin";
$two_task = "ORA8";
$oracle_sid    = "ORA8";
use File::Copy;
$ENV{ORACLE_HOME} = "$oracle_home";
$ENV{PATH} = "$path";
$ENV{LD_LIBRARY_PATH} = "$ld_library_path";
$ENV{TNS_ADMIN} = "$tns_admin";
$ENV{TWO_TASK} = "$two_task";
$ENV{ORACLE_SID}  = "$oracle_sid";
use DBI;
use CGI ':standard';
# =============================================
#  Get input from the Web via the content length
# =========================================
if ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
} else {
$buffer = $ENV{'QUERY_STRING'};
}
@pairs= split(/&/,$buffer);
foreach $pair (@pairs) {                    
   ($name,$value) = split(/=/,$pair);                            
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
    $contents{$name} = $value;
    }
$Artist_Name = $contents{'Artist_Name'};
$Artist_Name =~ s/$regexp//g;   #  remove suspect chars
my $dbh = DBI->connect("dbi:Oracle:ORA8", "username,"password")
		or die "Cannot connect: " .  $DBI::errstr;
my $sql = "SELECT A.ArtistName, R.RecordingTitle, R.Type 
FROM Artist A, Recording R
WHERE A.ArtistName LIKE '$Artist_Name'
AND A.ArtistID = R.ArtistID";
my $sth = $dbh->prepare($sql) or die "Cannot prepare: " . $dbh->errstr();

if ($sth->execute()) {
my @data;
my $count = 0;
print "Content-Type: text/html;charset=ISO-8859-1\n\n";
print "<html>\n<head>\n<title>Recording Search</title>\n</head>\n";
print "<body bgcolor=\"peachpuff\"><h1>Artist's Recordings</h1>\n";
print "\n<TABLE BORDER=1 ALIGN=CENTER>";
print"<tr><th>Artist's Name</th><th>Recording Title</th><th>Recording Type</th></tr>\n";
while (@data =$sth->fetchrow_array())
{ ++$count;
    print "<tr bgcolor=ffddbb>\n";
    foreach my $element (@data) {
      print "<TD>$element\n</TD>";
      }
      #print"<br></br>";
      print"</TR>";
 }
   print "</TABLE>"; 
 }
 
 print "</body>\n</html>\n"; 
$sth->finish();
$dbh->disconnect();
 

Ryuno

Can't get enough of FH
Joined
Mar 10, 2004
Messages
434
I might be blind here, but I can't see where you mention what error it is your getting :p
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
when run in a terminal the script works fine,
but when run through a browser (i.e hitting the submit button) i get the error
Code:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, root@wallaby.wmin.ac.uk and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.
 

Cylian

Fledgling Freddie
Joined
Jan 17, 2004
Messages
2,336
So far it looks like your SQL query, or the connection to the Database is the problem.

Would suggest you

a) post the error message (error page, whatever you got)
b) DESCRIBE the table, and what's stored in there at the moment
c) doublecheck the login details

guess you edited out the original details, since in the "working" one it reads

my $dbh = DBI->connect("dbi:Oracle:ORA8", "username,"password")

one " missing there. Make sure you don't have a typo there.

or for another blindshot, try your query with something like "SELECT sysdate FROM dual;"

if that works, you know atleast your connection is correct.
 

Cylian

Fledgling Freddie
Joined
Jan 17, 2004
Messages
2,336
oh, and remove all the fluffy commands out in the script, put one line there like

print "script does get executed" (or Argh! Panic!)

and see what happens. Chop it all down into little bits and try if they do what they're supposed to do. When the most simple things work, add the more difficult parts.
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
I tested the query in oracle and it works fine
I then hard coded the data to be queried ($Day = 12 etc) with data i know is in there.
This all worked fine.
I ran the script in a console and it returned the correct data.

The problem is when i run the script from a browser but get the error :/
this lead me to think maybe it is a permissions issue, so i checked the permissions on unix and it all looks fine there.

I added the lines:
BEGIN {
open STDERR, '>errors.txt';
}
use CGI::Carp qw /fatalsToBrowser/;

to pipe any errors into the browser/errors.txt.
Errors.txt is clean and the browser doesnt give any errors other than the very unhelpful one it gave before.
 

Cylian

Fledgling Freddie
Joined
Jan 17, 2004
Messages
2,336
does a super simple script work ?

just to make sure that the server actually does process the script. If there's a problem with the server itself, there's not much you can do. It's not like a Uni-server never ever encounters a problem ;)
But in that case you can hunt for ages and ages to for an error in your sourcecode :p

write a html page, check if that works
write a Perl script, check if that works in the browser (like the Print "hello") in the html page
write a SQL query in the Perl script and just query for the system date

generally make sure that the basics are available.
 

Steffan-

Fledgling Freddie
Joined
Jan 22, 2004
Messages
644
Sounds strange that it works fine on your own machine, but when uploaded to the server it returns an error. Sure the server aint the problem (setup)? Or am I just not getting it...
 

DocWolfe

Part of the furniture
Joined
Jan 3, 2005
Messages
2,855
are you uploading it in the right format? you have to upload perl scripts as binary iirc.
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
DocWolfe said:
are you uploading it in the right format? you have to upload perl scripts as binary iirc.
yep as my previous scripts work (see above for example),
this is the only one out of 4 that is refusing to work :/
so it must be something in the script doing a funny.

I have mailed the admin to send me the error logs maybe they will shed light on things.
 

GReaper

Part of the furniture
Joined
Dec 22, 2003
Messages
1,983
Check the Apache (I assume it's an Apache webserver) error log, that's what it's there for!
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
they had a problem at uni all seems to be working now :/

Thanks for the help all :)
 

Users who are viewing this thread

Top Bottom