defined() confusion

Discussion in 'Perl' started by Chris, Dec 11, 2003.

  1. Chris

    Chris Guest

    This seems inconsistent:


    #!/usr/bin/perl -w

    use strict;

    my @y;
    if(!defined(@y))
    {
    print "y undefined\n";
    }

    my $z = scalar(@y);
    if(!defined($z))
    {
    print "z undefined\n";
    }

    if(!defined(scalar(@y)))
    {
    print "scalar(\@y) not defined\n";
    }



    This prints

    y undefined
    scalar(@y) not defined

    So, the question is, why does

    my $z = scalar(@y);
    if(!defined($z))

    behave differently from

    if(!defined(scalar(@y)))

    ???

    Thanks,
    Chris
    Chris, Dec 11, 2003
    #1
    1. Advertising

  2. Chris

    Alex Zeng Guest

    When the value is assgined to a variable, it is upgraded, which explain the
    outcome.

    When a variable is created, it has a undef value. It is not "", not 0, not
    (), but undef. When assiging the undef value to another variable, perl pick
    the upgrade according to the context, therefore,
    -------------
    my @y,
    if (@y) {
    }
    @y will be intepreted as 0 instead of undef, a upgrade undef => 0 happens
    since the context asks for a boolean

    my $z = scalar(@y);
    Here z is assigned an upgraded version of the value, that would be "" or 0.
    ------------

    Hope this answers your question

    "Chris" <> wrote in message
    news:...
    > This seems inconsistent:
    >
    >
    > #!/usr/bin/perl -w
    >
    > use strict;
    >
    > my @y;
    > if(!defined(@y))
    > {
    > print "y undefined\n";
    > }
    >
    > my $z = scalar(@y);
    > if(!defined($z))
    > {
    > print "z undefined\n";
    > }
    >
    > if(!defined(scalar(@y)))
    > {
    > print "scalar(\@y) not defined\n";
    > }
    >
    >
    >
    > This prints
    >
    > y undefined
    > scalar(@y) not defined
    >
    > So, the question is, why does
    >
    > my $z = scalar(@y);
    > if(!defined($z))
    >
    > behave differently from
    >
    > if(!defined(scalar(@y)))
    >
    > ???
    >
    > Thanks,
    > Chris
    Alex Zeng, Dec 11, 2003
    #2
    1. Advertising

  3. Chris

    Guest

    (Chris) wrote in message news:<>...
    > This seems inconsistent:


    It is.
    >
    > #!/usr/bin/perl -w
    >
    > use strict;
    >
    > my @y;
    > if(!defined(@y))


    defined() applied to agregate types does not tell you anything useful
    to a Perl programmer. (What it actually tells you is if an AV has been
    assigned. This may be of interest to a perl programmer but is of no
    interest to a Perl programmer).

    > So, the question is, why does
    >
    > my $z = scalar(@y);
    > if(!defined($z))
    >
    > behave differently from
    >
    > if(!defined(scalar(@y)))


    The handling of the scalar() function by the compiler is rather odd.
    It often gets eleminated from the parse-tree in situations where it
    doesn't do anything. Sometimes however the complier is over zealous
    and eleiminates it in places where it doesn't do anything useful but
    should, by rights, do something. This can lead to a number of odd
    effects (including that above).

    Obiously if the compiler were fixed and scalar() was not eliminated
    the defined(scalar(@y)) would always be true so there's not much
    insentive to fix this bug.

    This newsgroup does not exist (see FAQ). Please do not start threads
    here.
    , Dec 18, 2003
    #3
    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. Oodini
    Replies:
    1
    Views:
    1,762
    Keith Thompson
    Sep 27, 2005
  2. Angel Tsankov
    Replies:
    1
    Views:
    863
    Victor Bazarov
    Apr 5, 2006
  3. Replies:
    1
    Views:
    492
    Sion Arrowsmith
    Jul 10, 2008
  4. Keith Thompson

    Re: __STDC_IEC_559__ (defined or !defined ?)

    Keith Thompson, Aug 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    440
    Keith Thompson
    Aug 17, 2010
  5. Alok
    Replies:
    3
    Views:
    246
Loading...

Share This Page