uninitialized?

K

Ken Sington

uninitialized? Isn't it already?

#!/usr/bin/perl -T

use warnings;
use strict;

my $a=1;

unless ($a eq undef|| $a eq 0){
print "\$a = $a\n";
}


print "END\n";





========output=====================

Use of uninitialized value in string eq at ./test.pl line 8.
$a = 1
END




===============================

it's the undef!
but why?
 
M

Matt Garrish

Ken Sington said:
uninitialized? Isn't it already?

#!/usr/bin/perl -T

use warnings;
use strict;

my $a=1;

unless ($a eq undef|| $a eq 0){
print "\$a = $a\n";
}

Because "undef" is a bareword, not how you check definedness. I suspect you
want the following:

if ( defined($a) && $a != 0 ) {

Matt
 
P

Paul Lalli

uninitialized? Isn't it already?

#!/usr/bin/perl -T

use warnings;
use strict;

my $a=1;

unless ($a eq undef|| $a eq 0){
print "\$a = $a\n";
}


print "END\n";

========output=====================

Use of uninitialized value in string eq at ./test.pl line 8.
$a = 1
END
===============================

it's the undef!
but why?


undef isn't a value. It's an operator. You're comparing $a to the return
value of the undef operator. The return value of undef is always
undefined. You get a warning whenever you use an undefined value in a
comparison like with eq.

The correct way to do what you were trying to do is:

unless (!defined($a) || $a eq 0) {
print "\$a = $a\n";
}

Paul Lalli

P.S. (Are you sure you want $a eq 0 and not $a == 0 ?)
 
P

Paul Lalli

Because "undef" is a bareword

No it's not. No more than print or push or pop are barewords.

Compare the above code with something like

unless ($a eq foobar){
print "\$a = $a\n";
}

and see the difference in warning messages.


Paul Lalli
 
K

Ken Sington

Paul said:
On Tue, 6 Jul 2004, Ken Sington wrote:

....




undef isn't a value. It's an operator. You're comparing $a to the return
value of the undef operator. The return value of undef is always
undefined. You get a warning whenever you use an undefined value in a
comparison like with eq.
but, wait!
undef is a value!
undef = nothing, a special undefined value
yes, no?
 
P

Paul Lalli

but, wait!
undef is a value!
undef = nothing, a special undefined value
yes, no?

Not quite. "undefined" is a special value to which every variable is
assigned before being given a defined value. It is also the value
returned by the undef operator. It is also the value assigned to the
argument passed to the undef operator. But the five-character string
'undef' is not a value in Perl, it's an operator. You don't say

$foo = undef;

(You can, but it's not 'proper'). You instead say

undef $foo;


Any variable which has the undefined value is treated as though it
contains either '' or 0, depending on context. You will never see a
'value' of undef printed out.

undef $foo;
print $foo; #prints the empty string
$a = 5 + $foo; #assigns 5 to $a

Paul Lalli
 
M

Matt Garrish

Paul Lalli said:
No it's not. No more than print or push or pop are barewords.

D'oh! I meant function. Barewords don't cause undefined errors, of course...
: )

Matt
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top