For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2005 > CGI.pm









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author CGI.pm
Chris Knipe

2005-03-29, 3:56 pm

Lo all,

Very quickly....

#!/usr/bin/perl
use CGI;
use strict;
use warnings;

my $Query = new CGI;
if (!$Query->request_method() eq "POST") {
exit;
}

Using a GET / HEAD method, the IF statement never executes. I am trying to
verify the user agent, but the above is just for my test - which is failing.

Anyone got some clues?

--
Chris.

I love deadlines. I especially love the whooshing sound they make as they
fly by..." - Douglas Adams, 'Hitchhiker's Guide to the Galaxy'

Chris Heiland

2005-03-29, 3:56 pm



> -----Original Message-----
> From: Chris Knipe [mailto:savage@savage.za.org]
> Sent: Tuesday, March 29, 2005 10:45 AM
> To: beginners@perl.org
> Subject: CGI.pm
>
> Lo all,
>
> Very quickly....
>
> #!/usr/bin/perl
> use CGI;
> use strict;
> use warnings;
>
> my $Query = new CGI;
> if (!$Query->request_method() eq "POST") {
> exit;
> }
>
> Using a GET / HEAD method, the IF statement never executes.
> I am trying to verify the user agent, but the above is just
> for my test - which is failing.
>
> Anyone got some clues?
>
> --
> Chris.
>
> I love deadlines. I especially love the whooshing sound they
> make as they fly by..." - Douglas Adams, 'Hitchhiker's Guide
> to the Galaxy'
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>


Untested:

if ($query->request_method() =~ m/^post$/i) {
do something;
}

Chris
Chris Knipe

2005-03-29, 8:56 pm

Strange indeed.

For further debuging, I did:

print $ENV{REQUEST_METHOD};

This, as expected, returns POST.

print $Query->request-method();

NULL value.

if ($ENV{REQUEST_METHOD} eq "POST")

Does not match....

Another one of those ARGH moments, or am I doing something silly?

--
Chris.



--
Chris.

I love deadlines. I especially love the whooshing sound they make as they
fly by..." - Douglas Adams, 'Hitchhiker's Guide to the Galaxy'

----- Original Message -----
From: "Chris Heiland" <CHeiland@uwb.edu>
To: <beginners@perl.org>
Sent: Tuesday, March 29, 2005 8:52 PM
Subject: RE: CGI.pm


>
>
>
> Untested:
>
> if ($query->request_method() =~ m/^post$/i) {
> do something;
> }
>
> Chris
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


Chris Devers

2005-03-29, 8:56 pm

On Tue, 29 Mar 2005, Chris Knipe wrote:

> if (!$Query->request_method() eq "POST") {
> exit;
> }
>
> Using a GET / HEAD method, the IF statement never executes.


Nor would it -- the exclamation point isn't in the right place.

This statement is saying "if not-$query->request_method() equals POST",
but "not-$query..." makes no sense.

But an exclamation point is the wrong approach here. If you want to say
that $foo should not equal $bar, use the "not equal" condition -- 'ne':

if $Query->request_method() ne "POST" {
exit;
}

That should do what you want.


--
Chris Devers
Chris Knipe

2005-03-29, 8:56 pm

Ok, sorry.

This now, does not make sense AT ALL!!!!

useing == (which is wrong), matches.
useing eq (which is right?) never matches...

Surely, if I match $ENV{REQUEST_METHOD} I am matching a string value....
Why, oh why dear mighty will == match (and give me a warning), and eq never
matches....

--
Chris.

I love deadlines. I especially love the whooshing sound they make as they
fly by..." - Douglas Adams, 'Hitchhiker's Guide to the Galaxy'

----- Original Message -----
From: "Chris Knipe" <savage@savage.za.org>
To: <beginners@perl.org>
Sent: Tuesday, March 29, 2005 9:00 PM
Subject: Re: CGI.pm


> Strange indeed.
>
> For further debuging, I did:
>
> print $ENV{REQUEST_METHOD};
>
> This, as expected, returns POST.
>
> print $Query->request-method();
>
> NULL value.
>
> if ($ENV{REQUEST_METHOD} eq "POST")
>
> Does not match....
>
> Another one of those ARGH moments, or am I doing something silly?
>
> --
> Chris.
>
>
>
> --
> Chris.
>
> I love deadlines. I especially love the whooshing sound they make as they
> fly by..." - Douglas Adams, 'Hitchhiker's Guide to the Galaxy'
>
> ----- Original Message -----
> From: "Chris Heiland" <CHeiland@uwb.edu>
> To: <beginners@perl.org>
> Sent: Tuesday, March 29, 2005 8:52 PM
> Subject: RE: CGI.pm
>
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>


Chris Heiland

2005-03-29, 8:56 pm



> -----Original Message-----
> From: Chris Knipe [mailto:savage@savage.za.org]
> Sent: Tuesday, March 29, 2005 11:01 AM
> To: beginners@perl.org
> Subject: Re: CGI.pm
>
> Strange indeed.
>
> For further debuging, I did:
>
> print $ENV{REQUEST_METHOD};
>
> This, as expected, returns POST.
>
> print $Query->request-method();
>
> NULL value.
>
> if ($ENV{REQUEST_METHOD} eq "POST")
>
> Does not match....
>
> Another one of those ARGH moments, or am I doing something silly?
>
> --
> Chris.
>

[snip][color=darkred]
[snip][color=darkred]

If you are just testing to see if they submitted the form then:

if ($ENV{'REQUEST_METHOD'} =~ m/^post$/i) {
do something;
}

If you are trying to see what is in the variables then I suggest
Data::Dumper:
perldoc Data::Dumper

Then just do a dump of what you think should contain something and it will
show you the contents. But then again it ultimately matters by what you
want to do with 'verify the user agent'.

Chris
Chris Knipe

2005-03-29, 8:56 pm

DOH!

Something Silly(tm)

Thanks ;)

----- Original Message -----
From: "Chris Devers" <cdevers@pobox.com>
To: "Chris Knipe" <savage@savage.za.org>
Cc: "Perl Beginners List" <beginners@perl.org>
Sent: Tuesday, March 29, 2005 9:07 PM
Subject: Re: CGI.pm


> On Tue, 29 Mar 2005, Chris Knipe wrote:
>
>
> Nor would it -- the exclamation point isn't in the right place.
>
> This statement is saying "if not-$query->request_method() equals POST",
> but "not-$query..." makes no sense.
>
> But an exclamation point is the wrong approach here. If you want to say
> that $foo should not equal $bar, use the "not equal" condition -- 'ne':
>
> if $Query->request_method() ne "POST" {
> exit;
> }
>
> That should do what you want.
>
>
> --
> Chris Devers
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>

Chris Devers

2005-03-29, 8:56 pm

On Tue, 29 Mar 2005, Chris Heiland wrote:

> Untested:
>
> if ($query->request_method() =~ m/^post$/i) {
> do something;
> }


If you're matching against a known, fixed pattern, the string comparison
operators will almost always be faster and clearer than the equivalent
regex. In this case, the method is always going to be one of POST, GET,
HEAD, etc, so it's much better to use the string comparison operator.

if $query->request_method() eq 'POST' {
do_something();
}

(And of course, don't use the numerical comparison operators, because
we're dealing here with strings, not numbers.)




--
Chris Devers
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com