Forum Moderators: coopster & phranque

Message Too Old, No Replies

perl script need help

script help

         

tester121

5:23 pm on Feb 4, 2006 (gmt 0)

10+ Year Member



I'm trying to make a web page showing 2 teams and if you enter the score -based on what you enter it displays a web page showing a link to the winning's team webpage. If it is a tie then it goes to a different web page. Here's what I have but it isn't working.

#!/usr/bin/perl
#sbowl.cgi - saves form data to a file, and creates a dynamic
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);
use strict;

#declare variables
my ($sea,$pitt);

#assign input items to variables
$sea= param('Seahawks');
$pitt = param('Steelers');

#validate input data
if ($sea or $pitt eq "") {
push(@errors, "Enter a score.");
}

#determine size of @errors array
$size = @errors;

#create Web page
if ($size == 0) {
print "<HTML>\n";
print "<HEAD><TITLE>SUPERBOWL XL</TITLE></HEAD>\n";
print "<BODY><H2>Visit your predicted winner's website</H2><BR>\n";
if ($sea < $pitt) {
print "You picked the Seahawks: www.seahawks.com"\n";
}
else
{
if ($sea > $pitt) {
print "You picked the Steelers: www.steelers.com"\n";
}
else
{
if ($sea = $pitt) {
print "You predicted a tie: www.nfl.com\n";

}
else
{
print "You didn't pick a winner";
}
}
}
print "</BODY>\n";
print "</HTML>\n";
}
else {
#display error page
print "<html><head><title></title></head>\n";
print "<body>\n";
print "<h2>Please press your browser's Back button to \n";
print "enter a score.\n";
print "</body></html>\n";
}

perl_diver

7:41 pm on Feb 4, 2006 (gmt 0)

10+ Year Member



first, make sure the syntax is good, you have a couple of syntax errors, like:

print "You picked the Seahawks: www.seahawks.com"\n";
print "You picked the Steelers: www.steelers.com"\n";

there is an extra " before \n at the end that is going to kill the script.

@errors has never been declared with "my":

push(@errors, "Enter a score.");

you can add it to your list of variables:

#declare variables
my ($sea,$pitt,@errors);

and $size has the same problem:

$size = @errors;

change it to:

my $size = @errors;

this section is not correct:

if ($sea or $pitt eq "") {

change it to:

if ($sea eq "" or $pitt eq "") {

OK, so now you see what was wrong with the script, here it is cleaned up and converted to using the CGI module:


#!/usr/bin/perl
#sbowl.cgi - saves form data to a file, and creates a dynamic
use CGI qw(:standard);
use strict;
#assign input items to variables
my $sea= param('Seahawks');
my $pitt = param('Steelers');
print header;
#validate input data
if ($sea eq "" or $pitt eq "") {
print start_html,
h2(q~Please press your browser's Back button to enter a score~),
end_html;
exit;
}
print start_html('SUPERBOWL XL'),
h2(q~Visit your predicted winner's website~);
if ($sea < $pitt) {
print "You picked the Seahawks: www.seahawks.com\n";
}
elsif ($sea > $pitt) {
print "You picked the Steelers: www.steelers.com\n";
}
elsif ($sea == $pitt) {
print "You predicted a tie: www.nfl.com\n";
}
else {
print "You didn't pick a winner";
}
print end_html;

tester121

3:36 am on Feb 5, 2006 (gmt 0)

10+ Year Member



This is working great except I need to make the website show as a hyperlink. I've tried putting in print "<A HREF="http://www.seahawks.com">Seattle Seahawks</A><BR>
but it isn't working. Is there something special to make hyperlinks in perl?

Thanks for your help! I need to keep checking my code, I get so tired of looking at it I forget to look for little mistakes like mistyping or putting in " where I shouldn't.

rocknbil

8:14 am on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



print "<A HREF="http://www.example.com">Seattle Seahawks</A>\n";

Try
print "<A HREF=\"http://www.example.com\">Seattle Seahawks</A>\n";

or better yet, when you get into larger blocks you're going to love qq, which spans multiple lines and will correctly include any characters EXCEPT the delimiter

print qq¦
<A
HREF="http://www.example.com">Seattle
Seahawks</A>
¦;

where ¦ can be any character that is NOT in the string anywhere

print qq^<A HREF="http://www.example.com">Seattle Seahawks</A>\n^;

but () is also legal

print qq(<A HREF="http://www.example.com">Seattle Seahawks</A>\n);

perl_diver

8:15 am on Feb 5, 2006 (gmt 0)

10+ Year Member



you probably need to escape the double-quotes:

print "<A HREF=\"http://www.seahawks.com\">Seattle Seahawks</A><BR>;

ahh...me and rocknbil were answering at the same time :)

tester121

10:37 pm on Feb 5, 2006 (gmt 0)

10+ Year Member



This worked! Thanks for the help

perl_diver

1:04 am on Feb 6, 2006 (gmt 0)

10+ Year Member



you're welcome :)