Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help with uninitialized value warning

Reply
Thread Tools

Help with uninitialized value warning

 
 
Jon
Guest
Posts: n/a
 
      09-17-2005
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.

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      09-17-2005
> 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

 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      09-17-2005

"Jon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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


 
Reply With Quote
 
ced@carios2.ca.boeing.com
Guest
Posts: n/a
 
      09-18-2005

Matt Garrish wrote:
> "Jon" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> >I am running into a problem with the following snippet of code:
> >

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

--
Charles DeRykus

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      09-19-2005
Matt Garrish wrote:

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Avoiding Perl warning "uninitialized value" neilsolent Perl Misc 3 11-26-2009 03:07 PM
Avoiding used uninitialized in this function warning markryde@gmail.com C Programming 1 05-25-2008 12:30 PM
Avoiding 'uninitialized value' warning? dgp@dodgeit.com Perl Misc 4 09-20-2005 09:33 AM
"defined $r_libs->{$name} " triggers warning "Use of uninitialized value" Liang Perl 2 08-11-2004 07:00 AM
Use of uninitialized value in concatenation (.) at register.pl line 38, <STDIN> line 10. G Kannan Perl 1 10-11-2003 11:58 AM



Advertisments