uninitialized?

Discussion in 'Perl Misc' started by Ken Sington, Jul 7, 2004.

  1. Ken Sington

    Ken Sington Guest

    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?
     
    Ken Sington, Jul 7, 2004
    #1
    1. Advertising

  2. Ken Sington

    Matt Garrish Guest

    "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    news:...
    > 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
     
    Matt Garrish, Jul 7, 2004
    #2
    1. Advertising

  3. Ken Sington

    Paul Lalli Guest

    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 ?)
     
    Paul Lalli, Jul 7, 2004
    #3
  4. Ken Sington

    Paul Lalli Guest

    On Tue, 6 Jul 2004, Matt Garrish wrote:

    > "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    > news:...
    > > 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
     
    Paul Lalli, Jul 7, 2004
    #4
  5. Ken Sington

    Ken Sington Guest

    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?
     
    Ken Sington, Jul 7, 2004
    #5
  6. Ken Sington

    Paul Lalli Guest

    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
     
    Paul Lalli, Jul 7, 2004
    #6
  7. Ken Sington

    Matt Garrish Guest

    "Paul Lalli" <> wrote in message
    news:...
    > On Tue, 6 Jul 2004, Matt Garrish wrote:
    >
    > > "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    > > news:...
    > > > 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
     
    Matt Garrish, Jul 7, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Kannan
    Replies:
    1
    Views:
    1,245
    Eric J. Roode
    Oct 11, 2003
  2. rk
    Replies:
    1
    Views:
    6,619
    Gunnar Hjalmarsson
    Dec 7, 2003
  3. Sukhbir Dhillon
    Replies:
    1
    Views:
    6,264
    Joe Smith
    Apr 5, 2004
  4. laurent
    Replies:
    7
    Views:
    1,929
    Joe Smith
    May 27, 2004
  5. smartins68
    Replies:
    1
    Views:
    6,014
    Joe Smith
    Jun 9, 2004
Loading...

Share This Page