Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > uninitialized?

Reply
Thread Tools

uninitialized?

 
 
Ken Sington
Guest
Posts: n/a
 
      07-07-2004
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?
 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      07-07-2004

"Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-07-2004
On Tue, 6 Jul 2004, Ken Sington wrote:

> 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 ?)
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      07-07-2004
On Tue, 6 Jul 2004, Matt Garrish wrote:

> "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
> news:(E-Mail Removed)...
> > 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


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
 
Reply With Quote
 
Ken Sington
Guest
Posts: n/a
 
      07-07-2004
Paul Lalli wrote:

> 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?
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      07-07-2004
On Tue, 6 Jul 2004, Ken Sington wrote:

> Paul Lalli 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?


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
 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      07-07-2004

"Paul Lalli" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Tue, 6 Jul 2004, Matt Garrish wrote:
>
> > "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
> > news:(E-Mail Removed)...
> > > 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

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


 
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




Advertisments