F
Francisco
Can someone please explain me what's wrong here? (coments in the code).
Thank you.
#!/usr/bin/perl -w
use strict;
use diagnostics;
my %severity = (
000 => 'emerg',
001 => 'alert',
010 => 'crit',
011 => 'err',
100 => 'warn',
101 => 'notice',
110 => 'info',
111 => 'debug'
);
my $numsev = 123;
my $binsev = dec2bin($numsev);
#it's clear after some experiments that the problem comes from this
conversion
print "binsev: $binsev\n";
#the binary number was converted OK
$binsev =~ /(\d{3})$/;
print "binsev3: $1\n";
#it appears that I get the 3 LSB of the binary number as I wanted
print "sev: $severity{$binsev3}\n";
#but here the hash lookup fails and I can't understand the reason, maybe
something at the end of the string that is wrong?
sub dec2bin { return sprintf "%b", shift }
Perl output:
binsev: 1111011
binsev3: 011
Use of uninitialized value in concatenation (.) or string at ./bla.pl line
24 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl tells you what operation
you used the undefined value in. Note, however, that perl optimizes
your
program and the operation displayed in the warning may not necessarily
appear literally in your program. For example, "that $foo" is
usually optimized into "that " . $foo, and the warning will refer to
the concatenation (.) operator, even though there is no . in your
program.
sev:
Thank you.
#!/usr/bin/perl -w
use strict;
use diagnostics;
my %severity = (
000 => 'emerg',
001 => 'alert',
010 => 'crit',
011 => 'err',
100 => 'warn',
101 => 'notice',
110 => 'info',
111 => 'debug'
);
my $numsev = 123;
my $binsev = dec2bin($numsev);
#it's clear after some experiments that the problem comes from this
conversion
print "binsev: $binsev\n";
#the binary number was converted OK
$binsev =~ /(\d{3})$/;
print "binsev3: $1\n";
#it appears that I get the 3 LSB of the binary number as I wanted
print "sev: $severity{$binsev3}\n";
#but here the hash lookup fails and I can't understand the reason, maybe
something at the end of the string that is wrong?
sub dec2bin { return sprintf "%b", shift }
Perl output:
binsev: 1111011
binsev3: 011
Use of uninitialized value in concatenation (.) or string at ./bla.pl line
24 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl tells you what operation
you used the undefined value in. Note, however, that perl optimizes
your
program and the operation displayed in the warning may not necessarily
appear literally in your program. For example, "that $foo" is
usually optimized into "that " . $foo, and the warning will refer to
the concatenation (.) operator, even though there is no . in your
program.
sev: