Help with uninitialized value warning

J

Jon

I am running into a problem with the following snippet of code:

my $job = "";
my $db = "";
my @events;
my @pieces;
my $start;
my $running;
my %hash;
my $event;
my @input = `autorep -J PVS%R -d`;

foreach (@input) {
if(/(PV.*)\_([A-Z]?[A-Z0-9])/) {
if($1 ne $job || $2 ne $db) {
undef @events;
}
$job = $1;
$db = $2;
}

When the script first runs, I get the "Use of uninitialized value in
string eq" at the line with the regex. I do know that initially, the
input does not match. However, after the first iteration through the
foreach loop, I get no other warnings even if the input doesn't match.
Can anyone tell me what is uninitialized? Thanks.
 
P

Paul Lalli

I am running into a problem with the following snippet of code:
my $job = "";
my $db = "";
my @events;
my @pieces;
my $start;
my $running;
my %hash;
my $event;
my @input = `autorep -J PVS%R -d`;

foreach (@input) {
if(/(PV.*)\_([A-Z]?[A-Z0-9])/) {
if($1 ne $job || $2 ne $db) {
undef @events;
}
$job = $1;
$db = $2;
}

When the script first runs, I get the "Use of uninitialized value in
string eq" at the line with the regex.

I find that to be astonishingly unlikely. You have no 'eq' operator
anywhere in this code. Please read the posting guidelines for this
group, and then follow their suggestions - post a short but complete
script that *we can run* by copy and pasting. Since it's possible we
don't have the "autorep" program, we have no way of knowing what is
contained in @input. Create a test script that populates @input
directly, and then post it here.
I do know that initially, the
input does not match. However, after the first iteration through the
foreach loop, I get no other warnings even if the input doesn't match.
Can anyone tell me what is uninitialized?

When I run this script, I get no warnings and no output of any kind.
Of course, that's likely because @input is empty for me. Please post a
script we can run.

Paul Lalli
 
M

Matt Garrish

Jon said:
I am running into a problem with the following snippet of code:

my $job = "";
my $db = "";
my @events;
my @pieces;
my $start;
my $running;
my %hash;
my $event;
my @input = `autorep -J PVS%R -d`;

foreach (@input) {
if(/(PV.*)\_([A-Z]?[A-Z0-9])/) {
if($1 ne $job || $2 ne $db) {
undef @events;
}
$job = $1;
$db = $2;
}

When the script first runs, I get the "Use of uninitialized value in
string eq" at the line with the regex.

I see two instances of 'ne', but none of 'eq', so you haven't posted enough
code to find the problem (or worse, you changed your code).

In general, unintialized warnings simply alert you that you're trying to use
a variable that hasn't yet had a value assigned to it (or has been assigned
the undef value), which is often not what you want. For example:

my $job = '';
my $db;

if ($job eq $db) {
print 'the above comparison will generate a warning';
}

If you find it odd that the comparison succeeds, it's because an undef value
is equal to an empty string. (I thought this behaviour was defined in
perlop, but it appears not. Is it just assumed that people will know, or
have I just forgotten where the reference is? I expect the latter...)

Matt
 
C

ced

Matt said:
omitted...

If you find it odd that the comparison succeeds, it's because an undef value
is equal to an empty string. (I thought this behaviour was defined in
perlop, but it appears not. Is it just assumed that people will know, or
have I just forgotten where the reference is? I expect the latter...)

The ancient Camel spells it out in "Gory Details" but, apparently, got
omitted in the latest:

There are actually scalars that have neither a numeric nor a string
value,
and these we call "undefined". An undefined scalar evaluates to 0
in a
numeric context and to the null string in a string context...
 
P

Paul Lalli

Matt said:
an undef value
is equal to an empty string. (I thought this behaviour was defined in
perlop, but it appears not. Is it just assumed that people will know, or
have I just forgotten where the reference is? I expect the latter...)

perldoc perlsyn
A variable holds the undefined
value ("undef") until it has been assigned a defined value,
which is anything other than "undef". When used as a
number, "undef" is treated as "0"; when used as a string, it
is treated the empty string, """"; and when used as a
reference that isn't being assigned to, it is treated as an
error. If you enable warnings, you'll be notified of an
uninitialized value whenever you treat "undef" as a string
or a number. Well, usually. Boolean ("don't-care")
contexts and operators such as "++", "--", "+=", "-=", and
".=" are always exempt from such warnings.

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top