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 Tomasz Chmielewski on March 28, 2008, 5:34 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

Posted by Gunnar Hjalmarsson on March 28, 2008, 6:16 am
Please log in for more thread options
Tomasz Chmielewski wrote:
> 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?

foreach my $var (@array) {
next unless defined $var and length $var;
next if substr($var, 0, 1) eq '#';
print "$var\n";
}

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

Posted by John W. Krahn on March 28, 2008, 8:04 am
Please log in for more thread options
Tomasz Chmielewski wrote:
> 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.

$ perl -le'
use strict;
use warnings;
my @array = ( "# Comment", "/usr/bin/binary --test", "" );
foreach my $var ( @array ) {
next if $var =~ /^(?:#|$)/;
print $var;
}
'
/usr/bin/binary --test




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

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


--


Posted by Willem on March 28, 2008, 3:18 pm
Please log in for more thread options
Tomasz wrote:
) I have perl code which should do some action only if:
)
) - the variable does not begin with "#" (commented out),
) - the variable is not empty

That's not quite what your code indicates is your intention.

It looks more like:
- Print only the part of a line before the '#'
- Don't print of the above is empty.


) 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:

How about just:

if ($execargs[0]) ?

And how about:
my ($execarg) = split(/#/, $var);
and then:
if ($execarg) { ... }


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

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