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

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

am 31.03.2008 10:14:21 von Joe Smith

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 '') { ... }

-Joe

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

am 31.03.2008 14:17:42 von Peter Scott

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 '') { ... }

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/

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

am 31.03.2008 19:08:59 von szr

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