Re: empty variables - getting rid of "uninitialized value" warnings?

Re: empty variables - getting rid of "uninitialized value" warnings?

am 31.03.2008 19:27:07 von Gunnar Hjalmarsson

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

Re: empty variables - getting rid of "uninitialized value" warnings?

am 31.03.2008 20:18:40 von szr

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

Re: empty variables - getting rid of "uninitialized value" warnings?

am 31.03.2008 20:23:00 von Willem

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

Re: empty variables - getting rid of "uninitialized value" warnings?

am 31.03.2008 20:29:48 von Frank Seitz

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

Re: empty variables - getting rid of "uninitialized value" warnings?

am 01.04.2008 01:24:41 von szr

Frank Seitz wrote:
> szr wrote:
>> Gunnar Hjalmarsson wrote:
>>>
>>> use warnings;
>>> @execargs = undef;
>
> Better:
> @execargs = (undef);

Yes. The effect is the same, but this makes it clearer. Why someone
would do this in practice is another question entirely, though.

>>> 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.

Yes I now understand why it was coming out that way, thanks.

>> 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.

Oh, yeah, because of the assingment of C to the array. My brain,
for whatever reason, translated that into C<@execargs = ()> as if it was
the same.

Thanks.

--
szr