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