Click here to get back home

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

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
empty variables - getting rid of "uninitialized value" warnings? Tomasz Chmielewski 03-28-2008
Posted by Thrill5 on March 29, 2008, 3:29 am
Please log in for more thread options

>I have perl code which should do some action only if:
>
> - the variable does not begin with "#" (commented out),
> - the variable is not empty
>
>
>
> use strict;
> use warnings;
>
> my @array = ("# Comment", "/usr/bin/binary --test", "");
>
> foreach my $var (@array) {
>
> my @execargs = split(/#/, $var);
>
> if ( $execargs[0] ne '' ) { print "$var 0: |$execargs[0]|\n" }
>
> }
>
>
> Unfortunately, it shows uninitialized value warnings for the empty
> variable (""):
>
> $ perl test.pl
> /usr/bin/binary --test 0: |/usr/bin/binary --test|
> Use of uninitialized value $execargs[0] in string ne at test.pl line 14.
>
>
>
> Using:
>
> if ( defined $execargs[0] ) { print "$var 0: |$execargs[0]|\n" }
>
> is not a solution either, because $execargs[0] will be defined for a case
> with "# Comment" and an undesired action will be made for this element:
>
>
> $ perl test.pl
> # Comment 0: ||
> /usr/bin/binary --test 0: |/usr/bin/binary --test|
>
>
>
> How can I get rid of warnings if I make tests with "if" and some variables
> are empty?
>
> Should I just ignore it? Or use "no warnings" just for that piece of code
> throwing a warning?
>
>
>
> --
> Tomasz Chmielewski
> http://wpkg.org

Try:
use strict;
use warnings;
no warnings 'uninitialized';

This will turn off only the uninitialized variable warnings, and keep all
the other warnings. I find those warnings are more trouble to get rid of
then the value that the warning provides.



Posted by Gunnar Hjalmarsson on March 29, 2008, 8:35 am
Please log in for more thread options
Thrill5 wrote:
>> How can I get rid of warnings if I make tests with "if" and some variables
>> are empty?
>>
>> Should I just ignore it? Or use "no warnings" just for that piece of code
>> throwing a warning?
>
> Try:
> use strict;
> use warnings;
> no warnings 'uninitialized';
>
> This will turn off only the uninitialized variable warnings, and keep all
> the other warnings. I find those warnings are more trouble to get rid of
> then the value that the warning provides.

Even if that may be true in some cases, it's not true in this case IMO.
If you write code that does not generate such warnings, you increase the
chance that there are no bugs. The OP has already received a few
suggestions.

Also, if you want to disable 'uninitialized' warnings, you'd better do
so in the block(s) where it's needed, and not disable them for the whole
program.

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

Posted by Tomasz Chmielewski on March 29, 2008, 11:41 am
Please log in for more thread options
Gunnar Hjalmarsson schrieb:
> Thrill5 wrote:
>>> How can I get rid of warnings if I make tests with "if" and some
>>> variables are empty?
>>>
>>> Should I just ignore it? Or use "no warnings" just for that piece of
>>> code throwing a warning?
>>
>> Try:
>> use strict;
>> use warnings;
>> no warnings 'uninitialized';
>>
>> This will turn off only the uninitialized variable warnings, and keep
>> all the other warnings. I find those warnings are more trouble to get
>> rid of then the value that the warning provides.
>
> Even if that may be true in some cases, it's not true in this case IMO.
> If you write code that does not generate such warnings, you increase the
> chance that there are no bugs. The OP has already received a few
> suggestions.
>
> Also, if you want to disable 'uninitialized' warnings, you'd better do
> so in the block(s) where it's needed, and not disable them for the whole
> program.

Thanks all in this thread for useful suggestions!


--
Tomasz Chmielewski
http://wpkg.org



Posted by Joe Smith on March 31, 2008, 4:14 am
Please log in for more thread options
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

Posted by Peter Scott on March 31, 2008, 8:17 am
Please log in for more thread options
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/


Similar ThreadsPosted
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

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap