|
Posted by Jim Cochrane on March 28, 2008, 2:57 pm
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" }
>
> }
If I understand what you're trying to do (and perhaps I don't), you want
something like this:
#!/usr/bin/perl
use strict;
use warnings;
my @array = ("# Comment",
"/usr/bin/binary --test1",
"/usr/bin/binary --test2",
"/usr/bin/binary\t--test3",
" /usr/bin/binary --test4",
"");
for my $var (@array) {
if (not $var or $var =~ /^#/) {
next;
}
my @execargs = split(' ', $var);
print 'arg array: ' . join(', ', @execargs) . "\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?
>
>
>
--
|