|
Posted by szr on March 31, 2008, 1:08 pm
Please log in for more thread options
Peter Scott wrote:
> On Mon, 31 Mar 2008 01:14:21 -0700, Joe Smith wrote:
>> Tomasz Chmielewski wrote:
>>> use strict;
>>> use warnings;
>>> ...
>>> if ( $execargs[0] ne '' ) { ..... }
>>
>> Whenever you are using warnings, you should never attempt to use
>> an array element without first testing that it is there.
>>
>> if (@execargs and $execargs[0] ne '') { ... }
>
> Not quite good enough. The element could exist but be undef.
>
> if (defined $execargs[0] && $execargs[0] ne '') { ... }
Instead of checking for definity, you could check for existance too:
if (exists $execargs[0] && $execargs[0] ne '') { ... }
Or even:
if (exists $execargs[0] && !!$execargs[0]) { ... }
--
szr
|
|
Posted by Gunnar Hjalmarsson on March 31, 2008, 1:27 pm
Please log in for more thread options
szr wrote:
> Peter Scott wrote:
>> On Mon, 31 Mar 2008 01:14:21 -0700, Joe Smith wrote:
>>>
>>> if (@execargs and $execargs[0] ne '') { ... }
>>
>> Not quite good enough. The element could exist but be undef.
>>
>> if (defined $execargs[0] && $execargs[0] ne '') { ... }
>
> Instead of checking for definity, you could check for existance too:
>
> if (exists $execargs[0] && $execargs[0] ne '') { ... }
I think you missed the point...
C:\home>type test.pl
use warnings;
@execargs = undef;
if (exists $execargs[0] && $execargs[0] ne '') {
# ...
}
C:\home>perl test.pl
Use of uninitialized value $execargs[0] in string ne at test.pl line 3.
C:\home>
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
|
|
Posted by szr on March 31, 2008, 2:18 pm
Please log in for more thread options Gunnar Hjalmarsson wrote:
> szr wrote:
>> Peter Scott wrote:
>>> On Mon, 31 Mar 2008 01:14:21 -0700, Joe Smith wrote:
>>>>
>>>> if (@execargs and $execargs[0] ne '') { ... }
>>>
>>> Not quite good enough. The element could exist but be undef.
>>>
>>> if (defined $execargs[0] && $execargs[0] ne '') { ... }
>>
>> Instead of checking for definity, you could check for existance too:
>>
>> if (exists $execargs[0] && $execargs[0] ne '') { ... }
>
> I think you missed the point...
>
> C:\home>type test.pl
> use warnings;
> @execargs = undef;
> if (exists $execargs[0] && $execargs[0] ne '') {
> # ...
> }
>
> C:\home>perl test.pl
> Use of uninitialized value $execargs[0] in string ne at test.pl line
> 3.
> C:\home>
I ran the same test and got the same result.
Removing the " && $execargs[0] ne '' " portion prevented the error.
This works too:
if (exists $execargs[0] && !!$execargs[0]) {
(Tested in 5.10.0, 5.8.8, and 5.6.1)
So I wonder, since " exists $execargs[0] " fails, why does it still
evaluate " && $execargs[0] ne '' " , thus causing the error.
This happens in all 3 versions I tested with. Is this a bug? I thought
it is supposed to short-circuit if the first expr fails in an && or AND
?
--
szr
|
|
Posted by Willem on March 31, 2008, 2:23 pm
Please log in for more thread options szr wrote:
) I ran the same test and got the same result.
)
) Removing the " && $execargs[0] ne '' " portion prevented the error.
)
) This works too:
)
) if (exists $execargs[0] && !!$execargs[0]) {
)
) (Tested in 5.10.0, 5.8.8, and 5.6.1)
)
)
) So I wonder, since " exists $execargs[0] " fails, why does it still
) evaluate " && $execargs[0] ne '' " , thus causing the error.
)
) This happens in all 3 versions I tested with. Is this a bug? I thought
) it is supposed to short-circuit if the first expr fails in an && or AND
) ?
It may be an operator precedence issue.
Try 'and' instead of '&&', or use parentheses around the second expression.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
|
|
Posted by Frank Seitz on March 31, 2008, 2:29 pm
Please log in for more thread options szr wrote:
> Gunnar Hjalmarsson wrote:
>>
>>use warnings;
>>@execargs = undef;
Better:
@execargs = (undef);
>>if (exists $execargs[0] && $execargs[0] ne '') {
>> # ...
>>}
>>
>>C:\home>perl test.pl
>>Use of uninitialized value $execargs[0] in string ne at test.pl line
>>3.
>
> I ran the same test and got the same result.
>
> Removing the " && $execargs[0] ne '' " portion prevented the error.
Yes, because this test produces the warning.
> This works too:
>
> if (exists $execargs[0] && !!$execargs[0]) {
>
> (Tested in 5.10.0, 5.8.8, and 5.6.1)
>
> So I wonder, since " exists $execargs[0] " fails,
No, "exists $execargs[0]" succeeds, because there is an element 0.
Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/ Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
|
| Similar Threads | Posted | | portable no warnings "uninitialized" | January 10, 2005, 8:26 am |
| catching "Use of uninitialized value" warnings | September 29, 2006, 7:38 pm |
| uninitialized value warnings and run time | January 4, 2007, 2:45 pm |
| Creating empty variables for input data | March 26, 2007, 4:49 pm |
| use warnings; and use Warnings; give different results | August 30, 2004, 2:09 pm |
| How to construct new variables in a script from other variables and strings. | May 12, 2006, 4:51 am |
| local variables and global variables | June 27, 2006, 8:31 am |
| warnings or -w ? | November 5, 2006, 7:24 am |
| perl warnings | October 10, 2004, 8:36 pm |
| Why aren't 'warnings' on by default? | February 5, 2005, 2:19 am |
|