For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2008 > perl warnings









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 perl warnings
Kashif Salman

2008-03-17, 7:08 pm

Hello,
I have a CGI script; when it runs the very first time I define some variables
my $action = $q->param('action');

The first time it runs, parameter 'action' isn't defined so that is
how I check that it is running the first time and do my things

if ($aciton eq "") {...}
elsif ($action eq "submit") {...}

the elsif runs if I hit a button on a form which has a hidden field
that sets action="submit". My question is that the script produces a
warning on the if statement " Use of uninitialized value in string eq
". How can I get rid of that without using "no warnings". I tried 'if
(defined("$action"))' but that still produces a warning.

Regards,
Kash
Yitzle

2008-03-17, 7:08 pm

I think you want:
if( defined $q->param('action') ) {
} else {
}
Gunnar Hjalmarsson

2008-03-17, 7:08 pm

Kashif Salman wrote:
> I have a CGI script; when it runs the very first time I define some variables
> my $action = $q->param('action');
>
> The first time it runs, parameter 'action' isn't defined so that is
> how I check that it is running the first time and do my things
>
> if ($aciton eq "") {...}
> elsif ($action eq "submit") {...}
>
> the elsif runs if I hit a button on a form which has a hidden field
> that sets action="submit". My question is that the script produces a
> warning on the if statement " Use of uninitialized value in string eq
> ". How can I get rid of that without using "no warnings". I tried 'if
> (defined("$action"))' but that still produces a warning.


if ( ! $action ) {...}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Kashif Salman

2008-03-17, 7:08 pm

On Mon, Mar 17, 2008 at 11:46 AM, Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
>
> Kashif Salman wrote:
>
> if ( ! $action ) {...}
>
> --



if( defined $q->param('action') ) #still produces the warning
if ( ! $action ) #worked

thanks
John W. Krahn

2008-03-17, 7:08 pm

Kashif Salman wrote:
> Hello,


Hello,

> I have a CGI script; when it runs the very first time I define some variables
> my $action = $q->param('action');
>
> The first time it runs, parameter 'action' isn't defined so that is
> how I check that it is running the first time and do my things
>
> if ($aciton eq "") {...}
> elsif ($action eq "submit") {...}
>
> the elsif runs if I hit a button on a form which has a hidden field
> that sets action="submit". My question is that the script produces a
> warning on the if statement " Use of uninitialized value in string eq
> ".


$ perl -le'
use warnings;
use strict;

my $action;

if ( $action eq "" ) {
print "\$action is empty";
}
'
Use of uninitialized value in string eq at -e line 7.
$action is empty


> How can I get rid of that without using "no warnings". I tried 'if
> (defined("$action"))' but that still produces a warning.


$ perl -le'
use warnings;
use strict;

my $action;

if ( defined "$action" ) {
print "\$action is empty";
}
'
Use of uninitialized value in string at -e line 7.
$action is empty


You are not testing the variable $action. You are testing a string that
just happens to contain the variable $action. defined() needs to test
the actual variable itself:

if ( defined $action ) {


Also see the FAQ entry:

perldoc -q quoting



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Paul Lalli

2008-03-17, 7:08 pm

On Mar 17, 2:08=A0pm, kashifsal...@gmail.com (Kashif Salman) wrote:
> Hello,
> I have a CGI script; when it runs the very first time I define some variab=

les
> my $action =3D $q->param('action');
>
> The first time it runs, parameter 'action' isn't defined so that is
> how I check that it is running the first time and do my things
>
> if ($aciton eq "") {...}
> elsif ($action eq "submit") {...}
>
> the elsif runs if I hit a button on a form which has a hidden field
> that sets action=3D"submit". My question is that the script produces a
> warning on the if statement " Use of uninitialized value in string eq
> ". How can I get rid of that without using "no warnings". I tried 'if
> (defined("$action"))' but that still produces a warning.


perldoc -q quoting

Stop double quoting your variables. The warning is telling you that
you're using an uninitialized value in a string. That warning is
important and relevant. It's telling you that it's the *STRING*
you're checking for defined'ness, not the variable within the string.
The string will *always* be defined, regardless of whether or not the
variable is.

if (defined($action)) { ... }
not
if(defined("$action")) { ... }


$ perl -wle'
my $foo;
if (defined("$foo")) { print "1 yes"; } else { print "1 no" }
if (defined($foo)) { print "2 yes"; } else { print "2 no" }
'
Use of uninitialized value in string at -e line 3.
1 yes
2 no

Paul Lalli

Paul Lalli

2008-03-17, 7:08 pm

On Mar 17, 2:46=A0pm, nore...@gunnar.cc (Gunnar Hjalmarsson) wrote:
> Kashif Salman wrote:
ables[color=darkred]
>
>
>
>
> =A0 =A0 =A0if ( ! $action ) {...}


That'll work great until some jackass puts "?action=3D0" in the URL.

Using defined() is correct. It's what he's passing to defined()
that's not.

Paul Lalli

Kashif Salman

2008-03-17, 7:08 pm

On Mon, Mar 17, 2008 at 12:04 PM, Paul Lalli <mritty@gmail.com> wrote:
> On Mar 17, 2:46 pm, nore...@gunnar.cc (Gunnar Hjalmarsson) wrote:
>
> That'll work great until some jackass puts "?action=0" in the URL.
>
> Using defined() is correct. It's what he's passing to defined()
> that's not.
>
> Paul Lalli
>
>
>
>
> --



Thank you
Gunnar Hjalmarsson

2008-03-17, 7:08 pm

Paul Lalli wrote:
> On Mar 17, 2:46 pm, nore...@gunnar.cc (Gunnar Hjalmarsson) wrote:
>
> That'll work great until some jackass puts "?action=0" in the URL.


So what? If you put random crap in the URL, you can't reasonably expect
a meaningful response.

In this case, if I understand it correctly, the default version of the
page, with a form, would appear. Why would that be a problem for anybody
but the stupid user? ;-)

That said, I agree that using defined() is more to the point.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Justin Hawkins

2008-03-17, 7:08 pm


On 17 Mar 2008, at 21:58, Gunnar Hjalmarsson wrote:
>
> In this case, if I understand it correctly, the default version of
> the page, with a form, would appear. Why would that be a problem for
> anybody but the stupid user? ;-)


Fundamentally, don't make it possible for your users to do anything to
surprise you.

It would have been a benign surprise this time, once these bad habits
become entrenched it will only be a matter of time before you get a
nasty surprise.

Case in point:

I once wrote some indexing code, things being what they were I had to
make fairly efficient use of disk space. I decided on a simple bit-
packing scheme, so each byte could flag the existence of a word in up
to 8 objects. With sparse files, this was quite efficient indeed.

Somewhere in the code I had a check like this:

if (! $index_byte ) {
[ code to process the next byte ]
}
else {
[ process this byte ]
}

Seems pretty bulletproof doesn't it? If the index byte is empty then
we skip on to the next byte, as we don't have any entries at all for
those 8 objects.

Testing showed a problem.... occasionally index entries were missing.
A lot of digging found the culprit.... if you have a scalar byte
holding 8 bits worth of data, it's not just when it holds 0x00 that
that test is true - it's also when it holds 0x30 - ascii '0'.

ALWAYS make your tests as specific as possible, and care about the
exceptions.

- Justin

--
Justin Hawkins
justin@hawkins.id.au
http://hawkins.id.au/~justin/




Sponsored Links







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

Copyright 2008 codecomments.com