Scott

Before you actually try running running your script, make sure it
"compiles" first.

Perl scripts don't compile in the traditional sense, they are run-time
compiled.

Try 'perl -c script.pl' from a shell to see feedback about what you have
done wrong.

You may also want to use strict, warnings, and diagnostics as the first
lines of your script.
These will catch you doing bad things, and explain ( hopefully ) what
you are doing wrong.

Your ACTUAL problems include assignment ( = ) instead of string equality
( eq ), not quoting
your strings  ( adl becomes 'adl' ), and unless my wrapping is screwed,
you are closing the if block twice, possibly because of a
misunderstaning on how comments work.
You also don't have a HTML body, have illegal text in the head, your
HTTP header is possibly wrong ( missing a space...? ), and you also
arn't indenting ( but that's a style and clarity issue ) ... oh and you
are possibly passing the URI wrong.

For starters, try adding a bit more whitespace to pad it out a bit, and
maybe some comments

Example:

#!/usr/local/bin/perl

use strict;
use warnings;
use diagnostics;

# Print the CGI header
print "Content-Type: text/html\n\n";
print "<html><head></head><body bgcolor='#FFFFFF'>\n";

# Catch bad calls to avoid loops
if ( $ENV{'REQUEST_URI'} eq 'adl' ) {
        print "No Good\n";
} else {
        # Redirect to the other server
        print "<meta http-equiv=Refresh content='2;
URL=http://10.0.12.2$ENV{'REQUEST_URI'}?adl'>\n";
}

print "</body></html";
exit();

That at least has the right syntax...

You may want to consider using the CGI module to generate you're header
and check what looks like a parameter you want, not the entire URI.

So the above becomes

#!/usr/local/bin/perl

use strict;
use warnings;
use diagnostics;
use CGI;

# Print the CGI header
print CGI->header;
print "<html><head></head><body bgcolor='#FFFFFF'>\n";

# Check for the adl parameter to catch bad calls,
# avoiding loops.
my $query = CGI->new();
if ( $query->param( 'adl' ) ) {
        print "No Good\n";
} else {
        # Redirect to the other server
        print "<meta http-equiv=Refresh content='2;
URL=http://10.0.12.2$ENV{'REQUEST_URI'}?adl'>\n";
}

print "</body></html";
exit();



Finally, you might want to consider HTTP level redirecting, rather than
using HTML. And I'll quote all the html in a single block.

This becomes

#!/usr/local/bin/perl

use strict;
use warnings;
use diagnostics;
use CGI;

# Check for the adl parameter to catch bad calls,
# avoiding loops.
my $query = CGI->new();
if ( $query->param( 'adl' ) ) {
        print CGI->header;
        print qq~
                <html>
                <head></head>
                <body bgcolor='#FFFFFF'>
                No Good
                </body>
                </html>
                ~;
} else {
        # Redirect to the other server
        CGI->redirect( 'http://10.0.12.2?adl' );
}

exit();


Finally, there is a slight mistake in there because although you pass an
adl parameter, you don't set it to anything which means that although
the adl param will be defined, there won't be anything in it...

So the 

if ( $query->param( 'adl' ) ) {

line needs to becaome

if ( defined $query->param( 'adl' ) ) {

I hope some of that advice is of use.

Adam K

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of
Scott Ragen
Sent: Friday, 31 May 2002 4:12 PM
To: [EMAIL PROTECTED]
Subject: [SLUG] Perl - CGI: if statement


Hi all,
I have created this piece of CGI code (its probably bloated, but its my
first time!)
I have a few problems with it:

#!/usr/local/bin/perl
print "Content-Type:text/html\n\n";
print "<html><head>\n";
if ($ENV{'REQUEST_URI'} = adl ) {
print "no Good\n";}
#print "<meta http-equiv=Refresh content='2;
URL=http://10.0.12.2$ENV{'REQUEST_URI'}?adl'>\n";}
print "</head></html>"

What I am trying to do is redirect 404 errors to another web server
which in turn goes to another, and could end up looping indefinably.
I want the if statement to say, if REQUEST_URI *CONTAINS* adl then print
"no good" (it will change but this is testing) else continue.

Another problem I have noticed, is it changes the $ENV{REQUEST_URI} to
adl, which is not what I want, I want it to be what it says.

Sorry if this abit hard to understand, but I am confused....

Cheers,

Scott

-- 
Scott Ragen
Roadtech Systems Ltd.
Support Manager
Ph: +61 2 9807 3516 Fax: +61 2 9008 5294
www.roadtechsystems.com.au

-- 
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug
-- 
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug

Reply via email to