469,903 Members | 1,637 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

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(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
Jul 19 '05 #1
2 10602
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" <bi*********@hotmail.com> wrote in message
news:fa*************************@posting.google.co m...
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

Jul 19 '05 #2
bi*********@hotmail.com (Chris) wrote in message news:<fa*************************@posting.google.c om>...
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.
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

38 posts views Thread by Grant Edwards | last post: by
9 posts views Thread by AngleWyrm | last post: by
2 posts views Thread by wenmang | last post: by
4 posts views Thread by JR | last post: by
3 posts views Thread by Beta What | last post: by
19 posts views Thread by DarelRex | last post: by
4 posts views Thread by coder_lol | last post: by
22 posts views Thread by arnuld | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.