Home > Archive > PERL Beginners > December 2007 > Use of uninitialized value in numeric eq (==)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Use of uninitialized value in numeric eq (==)
|
|
| Protoplasm 2007-12-12, 7:02 pm |
| I'm messing around with Getopt and have a set of arguments that can be
passed to the program. I am new to perl and am unaware of how to
resolve the error while keeping 'use warnings;' enabled. Any help
would be greatly appreciated.
Code:
========================================
======
#!/opt/local/bin/perl
use Getopt::Long;
#use diagnostics;
use strict;
use warnings;
my $opts_hash;
my %opts_hash = ();
if (@ARGV < 1)
{
print "argv is less than 1!\n";
print "Exiting...\n";
exit;
}
GetOptions( 'all' => \$opts_hash{allTests},
'CbcDec' => \$opts_hash{CbcDec}, 'CbcEnc' => \$opts_hash{CbcEnc},
'CfbDec' => \$opts_hash{CfbDec}, 'CfbEnc' => \$opts_hash{CfbEnc},
'CtrDec' => \$opts_hash{CtrDec}, 'CtrEnc' => \$opts_hash{CtrEnc},
'EcbDec' => \$opts_hash{EcbDec}, 'EcbEnc' => \$opts_hash{EcbEnc},
'OfbDec' => \$opts_hash{OfbDec}, 'OfbEnc' => \$opts_hash{OfbEnc},
'Sha1Test' => \$opts_hash{Sha1Test}
);
foreach (keys %opts_hash)
{
if ( $opts_hash{$_} == 0 )
{
next;
}
elsif ( $opts_hash{$_} == 1 )
{
print "$_ = $opts_hash{$_}\n";
}
}
========================================
======
naiad:~/workspace $ ./debug2.pl --CbcDec
Use of uninitialized value in numeric eq (==) at ./debug2.pl line 32.
Use of uninitialized value in numeric eq (==) at ./debug2.pl line 32.
Use of uninitialized value in numeric eq (==) at ./debug2.pl line 32.
Use of uninitialized value in numeric eq (==) at ./debug2.pl line 32.
Use of uninitialized value in numeric eq (==) at ./debug2.pl line 32.
Use of uninitialized value in numeric eq (==) at ./debug2.pl line 32.
Use of uninitialized value in numeric eq (==) at ./debug2.pl line 32.
Use of uninitialized value in numeric eq (==) at ./debug2.pl line 32.
CbcDec = 1
Use of uninitialized value in numeric eq (==) at ./debug2.pl line 32.
Use of uninitialized value in numeric eq (==) at ./debug2.pl line 32.
Use of uninitialized value in numeric eq (==) at ./debug2.pl line 32.
| |
| Chas. Owens 2007-12-12, 7:02 pm |
| On Dec 11, 2007 7:53 PM, protoplasm <sprotsman@gmail.com> wrote:
snip
> my $opts_hash;
> my %opts_hash = ();
snip
You don't need $opts_hash (you aren't using it). You can get rid of
the undef warnings by making sure that the keys in %opts_hash are used
like this:
#default values for the options
my %opts_hash = (
CbcDec => 0,
CbcEnc => 0,
CfbDec => 0,
);
| |
| John W . Krahn 2007-12-12, 7:02 pm |
| On Tuesday 11 December 2007 16:53, protoplasm wrote:
>
> I'm messing around with Getopt and have a set of arguments that can
> be passed to the program. I am new to perl and am unaware of how to
> resolve the error while keeping 'use warnings;' enabled. Any help
> would be greatly appreciated.
>
> Code:
>
> ========================================
======
> #!/opt/local/bin/perl
>
> use Getopt::Long;
>
> #use diagnostics;
> use strict;
> use warnings;
>
> my $opts_hash;
> my %opts_hash = ();
>
> if (@ARGV < 1)
> {
> print "argv is less than 1!\n";
> print "Exiting...\n";
> exit;
> }
>
> GetOptions( 'all' => \$opts_hash{allTests},
> 'CbcDec' => \$opts_hash{CbcDec}, 'CbcEnc' => \$opts_hash{CbcEnc},
> 'CfbDec' => \$opts_hash{CfbDec}, 'CfbEnc' => \$opts_hash{CfbEnc},
> 'CtrDec' => \$opts_hash{CtrDec}, 'CtrEnc' => \$opts_hash{CtrEnc},
> 'EcbDec' => \$opts_hash{EcbDec}, 'EcbEnc' => \$opts_hash{EcbEnc},
> 'OfbDec' => \$opts_hash{OfbDec}, 'OfbEnc' => \$opts_hash{OfbEnc},
> 'Sha1Test' => \$opts_hash{Sha1Test}
> );
Perl will autovivify the hash keys because you are passing the hash
value references to GetOptions. An alternative way that will not
autovivify all the keys is:
GetOptions( \my %opts_hash,
'allTests', 'CbcDec', 'CbcEnc', 'CfbDec', 'CfbEnc', 'CtrDec',
'CtrEnc', 'EcbDec', 'EcbEnc', 'OfbDec', 'OfbEnc', 'Sha1Test' );
> foreach (keys %opts_hash)
> {
> if ( $opts_hash{$_} == 0 )
perldoc -f defined
> {
> next;
> }
> elsif ( $opts_hash{$_} == 1 )
> {
> print "$_ = $opts_hash{$_}\n";
> }
> }
> ========================================
======
>
> naiad:~/workspace $ ./debug2.pl --CbcDec
> Use of uninitialized value in numeric eq (==) at ./debug2.pl line 32.
John
--
use Perl;
program
fulfillment
| |
| Protoplasm 2007-12-12, 7:02 pm |
| Thanks for the advice. After I posted I tried this and it worked too:
foreach (keys %opts_hash)
{
# This next line of code eliminates the "Use of uninitialized value
in
# numeric eq (==)" error when 'use warnings;' is enabled.
if ( !defined $opts_hash{$_} )
{
next;
}
elsif ( $opts_hash{$_} == 1 )
{
print "$_ = $opts_hash{$_}\n";
}
}
| |
| Uri Guttman 2007-12-12, 7:02 pm |
| >>>>> "P" == Protoplasm <sprotsman@gmail.com> writes:
P> Thanks for the advice. After I posted I tried this and it worked too:
P> foreach (keys %opts_hash)
P> {
P> # This next line of code eliminates the "Use of uninitialized value
P> in
P> # numeric eq (==)" error when 'use warnings;' is enabled.
P> if ( !defined $opts_hash{$_} )
P> {
P> next;
P> }
P> elsif ( $opts_hash{$_} == 1 )
P> {
P> print "$_ = $opts_hash{$_}\n";
P> }
P> }
ewwww!
while( my( $opt, $opt_val ) = each %opts_hash ) {
next unless defined $opt_val ;
print "$opt = $opt_val\n" if $opt_val == 1 ;
}
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
| |
| Dr.Ruud 2007-12-12, 10:02 pm |
| protoplasm schreef:
> foreach (keys %opts_hash)
> {
> if ( !defined $opts_hash{$_} )
> {
> next;
> }
> elsif ( $opts_hash{$_} == 1 )
> {
> print "$_ = $opts_hash{$_}\n";
> }
> }
An alternative way to write that:
for (sort keys %opts_hash) {
next unless defined $opts_hash{$_};
print "$_ = $opts_hash{$_}\n"
if 1 == $opts_hash{$_};
}
I used sort to get them to print in a more predictable order.
--
Affijn, Ruud
"Gewoon is een tijger."
| |
| Paul Lalli 2007-12-13, 7:01 pm |
| On Dec 12, 9:45 pm, rvtol+n...@isolution.nl (Dr.Ruud) wrote:
> protoplasm schreef:
>
>
> An alternative way to write that:
>
> for (sort keys %opts_hash) {
> next unless defined $opts_hash{$_};
Or:
for (grep { defined $opts_hash{$_} } sort keys %opts_hash) {
:-)
Paul Lalli
|
|
|
|
|