BEGIN not safe after errors--compilation aborted

D

daz9643

I've got 2 perl scripts, both are code for ads. One script is admob,
the other is google. The code provided has been cut and pasted without
any mods. The admob code works, the google code doesn't and i get the
error,
syntax error at googleperlcode.cgi line 9, near "sub
google_append_color "
Can't use global @_ in "my" at googleperlcode.cgi line 10, near ", $_"
syntax error at googleperlcode.cgi line 12, near "}"
Execution of googleperlcode.cgi aborted due to compilation errors.

This is the actual google code,
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
ul class="simpleblue"
sub google_append_color {
my @color_array = split(/,/, $_[0]);
return $color_array[$_[1] % @color_array];
}

Anyone know why the errors ? Both files end in .cgi and both have the
permissions set the same, i.e 755. Both are in the same directory on
the server.
 
J

J. Gleixner

daz9643 said:
I've got 2 perl scripts, both are code for ads. One script is admob,
the other is google. The code provided has been cut and pasted without
any mods. The admob code works, the google code doesn't and i get the
error,
syntax error at googleperlcode.cgi line 9, near "sub
google_append_color "
Can't use global @_ in "my" at googleperlcode.cgi line 10, near ", $_"
syntax error at googleperlcode.cgi line 12, near "}"
Execution of googleperlcode.cgi aborted due to compilation errors.

This is the actual google code,
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
ul class="simpleblue"
^^^^^^^^^^^^^^^^^^^^^^
Does that look at all like it's valid syntax?
sub google_append_color {
my @color_array = split(/,/, $_[0]);
return $color_array[$_[1] % @color_array];
}

Anyone know why the errors ? Both files end in .cgi and both have the
permissions set the same, i.e 755. Both are in the same directory on
the server.
 
T

Tad J McClellan

syntax error at googleperlcode.cgi line 9, near "sub ^^^^^^
^^^^^^
This is the actual google code,


I do not believe you.

#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
ul class="simpleblue"
sub google_append_color {
my @color_array = split(/,/, $_[0]);
return $color_array[$_[1] % @color_array];
}


There is no line 9 in your 7 line program...
 
D

daz9643

ok ,the actual google code does not include the lines
use CGI::Carp qw(fatalsToBrowser);
ul class="simpleblue"

these to lines were just added to help with debugging. Without these 2
lines i just get the error 500.
You're right about line 9, there were a few blank lines in the code
which i've deleted as they weren't necessary. If i remove the
offending line of code or rem it out the error just points to the next
line of code and so on.
 
R

Ron Bergin

ok ,the actual google code does not include the lines
use CGI::Carp qw(fatalsToBrowser);
ul class="simpleblue"

these to lines were just added to help with debugging. Without these 2
lines i just get the error 500.
You're right about line 9, there were a few blank lines in the code
which i've deleted as they weren't necessary. If i remove the
offending line of code or rem it out the error just points to the next
line of code and so on.

Well, it's pretty obvious that this line:
ul class="simpleblue"

will generate an error.

Comment out that line and retest. If if fails, then post the exact
error message and the EXACT code that you tested, but only upto a
couple lines after the reported line in the error messages.
 
D

daz9643

With ul class="simpleblue" removed all i get is,


Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.
Please contact the server administrator,
(e-mail address removed) 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.


#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
use LWP::UserAgent;
use Time::HiRes qw(gettimeofday);
use URI::Escape;
 
D

daz9643

This is what i've tried to follow to sort the problem, cut & pasted
from 123-reg.co.uk online support,

<p>If you encounter an error "500 Internal Server Error" whilst
running your CGI perl scripts, try the following in order:</p>

<ul class="simpleblue">
<li>Ensure you have uploaded the script through FTP using ASCII mode,
or that it has Unix-style carriage returns.</li>
<li>Check the perl interpreter on the first line of the script. It
should always be "#!/usr/bin/perl".</li>
<li>Make sure the script filename ends in either ".pl" or ".cgi".</li>
<li>Make sure the script can execute. It should have mode "0755". You
can change this in your file manager - set the mode to user read+write
+execute, group read+execute, other read+execute. Most FTP clients
also support this.</li>
<li>Put "use CGI::Carp qw(fatalsToBrowser);" into the second line of
your script. This will make perl print the error instead of the "500
Internal Server Error" page, and will allow you to find out where your
script is breaking.</li>
</ul>
 
D

Dave Weaver

daz9643 said:
This is what i've tried to follow to sort the problem, cut & pasted
from 123-reg.co.uk online support,

<li>Put "use CGI::Carp qw(fatalsToBrowser);" into the second line of
your script. This will make perl print the error instead of the "500
Internal Server Error" page, and will allow you to find out where your
script is breaking.</li>
</ul>

So it tells you to add "use CGI::Carp qw(fatalsToBrowser);"

Where did the 'ul class="simpleblue"' come from? This is not valid perl code.

Also, your initial program:

#!/usr/bin/perl
sub google_append_color {
my @color_array = split(/,/, $_[0]);
return $color_array[$_[1] % @color_array];
}

is no good as a stand-alone CGI script, which is what you appear to be using it
as. The web server is expecting some output from your CGI script, yet it does
nothing. Hence the "500 Internal server error".

It is not clear what you are tying to acheive.
 
D

daz9643

Hi Dave, all i want to do is put google ads on my mobile website. The
code options given by Google are PHP which my host does not support,
CGI/Perl, JSP and ASP. My site is written in XHTML and is saved as
a .shtml file. I thought the simplest solution would be to copy the
Google Perl ad code and save it as a .cgi file. The place where i want
the ads to appear executes the Perl file using this line of code,

<!--#exec cgi="/cgi-bin/googleperlcode.cgi" --><br />

I've done exactly the same thing using ad code from Admob so i can't
understand why this doesn't work as as i know nothing about Perl i've
come to a dead end.

The entire Google ad code is below,
#!/usr/bin/perl

use LWP::UserAgent;
use Time::HiRes qw(gettimeofday);
use URI::Escape;

sub google_append_color {
my @color_array = split(/,/, $_[0]);
return $color_array[$_[1] % @color_array];
}

sub google_append_screen_res {
my $screen_res = $ENV{"HTTP_UA_PIXELS"};
my $delimiter = "x";
if ($screen_res == "") {
$screen_res = $ENV{"HTTP_X_UP_DEVCAP_SCREENPIXELS"};
$delimiter = ",";
}
my @res_array = split($delimiter, $screen_res);
if (@res_array == 2) {
return "&u_w=" . $res_array[0] . "&u_h=" . $res_array[1];
}
}

my $google_dt = sprintf("%.0f", 1000 * gettimeofday());
my $google_scheme = ($ENV{"HTTPS"} eq "on") ? "https://" : "http://";
my $google_host = uri_escape($google_scheme . $ENV{"HTTP_HOST"});

my $google_ad_url = "http://pagead2.googlesyndication.com/pagead/
ads?" .
"ad_type=text" .
"&channel=" .
"&client=ca-mb-pub-5303496068311064" .
"&dt=" . $google_dt .
"&format=mobile_single" .
"&host=" . $google_host .
"&ip=" . uri_escape($ENV{"REMOTE_ADDR"}) .
"&markup=xhtml" .
"&oe=utf8" .
"&output=xhtml" .
"&ref=" . uri_escape($ENV{"HTTP_REFERER"}) .
"&url=" . $google_host . uri_escape($ENV{"REQUEST_URI"}) .
"&useragent=" . uri_escape($ENV{"HTTP_USER_AGENT"}) .
google_append_screen_res();

my $google_ua = LWP::UserAgent->new;
my $google_ad_output = $google_ua->get($google_ad_url);
if ($google_ad_output->is_success) {
print $google_ad_output->content;
}
 
D

Dave Weaver

my $google_ua = LWP::UserAgent->new;
my $google_ad_output = $google_ua->get($google_ad_url);
if ($google_ad_output->is_success) {
print $google_ad_output->content;
}


If the get() call fails, your program outputs nothing, and
you will get a "500 internal server error", or similar.

Change that to:

if ($google_ad_output->is_success) {
print $google_ad_output->content;
}
else {
print "failed to fetch ad:\n"
. "URL = $google_ad_url\n"
. "status = " . $google_ad_output->status_line
. "\n";
}

Then run the script by hand from a command prompt:
perl googleperlcode.cgi

to see what output you get. I suspect there is a problem
fetching whatever URL it builds.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top