defined() confusion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    defined() confusion

    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(sca lar(@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(sca lar(@y)))

    ???

    Thanks,
    Chris
  • Alex Zeng

    #2
    Re: defined() confusion

    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" <bit_bucket5@ho tmail.com> wrote in message
    news:fa7108b0.0 312111014.dbb0a 8b@posting.goog le.com...[color=blue]
    > 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(sca lar(@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(sca lar(@y)))
    >
    > ???
    >
    > Thanks,
    > Chris[/color]


    Comment

    • nobull@mail.com

      #3
      Re: defined() confusion

      bit_bucket5@hot mail.com (Chris) wrote in message news:<fa7108b0. 0312111014.dbb0 a8b@posting.goo gle.com>...[color=blue]
      > This seems inconsistent:[/color]

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

      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).
      [color=blue]
      > So, the question is, why does
      >
      > my $z = scalar(@y);
      > if(!defined($z) )
      >
      > behave differently from
      >
      > if(!defined(sca lar(@y)))[/color]

      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.

      Comment

      Working...