Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > defined() confusion

Reply
Thread Tools

defined() confusion

 
 
Chris
Guest
Posts: n/a
 
      12-11-2003
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
 
Reply With Quote
 
 
 
 
Alex Zeng
Guest
Posts: n/a
 
      12-11-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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



 
Reply With Quote
 
 
 
 
nobull@mail.com
Guest
Posts: n/a
 
      12-18-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Chris) wrote in message news:<(E-Mail Removed) 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.
 
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
runtime confusion Jerome ASP .Net 0 12-03-2005 09:11 PM
Mozilla & Firefox Confusion listsubscriber@hotmail.com Firefox 9 01-20-2005 05:09 PM
Confusion about location of Mozilla files (Mandrake Linux 10.0) Hallvard Tangeraas Firefox 0 09-14-2004 09:46 AM
Procedures in testbench confusion Peter Hermansson VHDL 2 08-25-2004 02:15 PM
confusion when resetting registers martin f. krafft VHDL 2 08-19-2004 06:29 AM



Advertisments