T
Tomasz Chmielewski
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?
- 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?