difference between defined & exists

P

Palaniappan

hi all,

What is the difference between 'defined' operator
and 'exists' operator?


Thanks & regards,
palani
 
B

Ben Morrow

hi all,

What is the difference between 'defined' operator
and 'exists' operator?

my @a;

$a[0] = 1;

# defined $a[0] is true
# exists $a[0] is true

$a[0] = undef; # or undef $a[0];

# defined $a[0] is false
# exists $a[0] is true

delete $a[0];

# defined $a[0] is false
# exists $a[0] is false

defined applies to any value: it tests if that value is 'undef' or
not. exists applies to array or hash members: it tests whether there
is any value, defined or no, there at all.

Ben
 
B

Brian McCauley

What is the difference between 'defined' operator
and 'exists' operator?

Even if you are a newcommer to the Perl community you should still try
to give as well as take. You have, in fact, already contributed
something my choosing a good subject line for this thread. Well done,
that already puts you above the median.

You could, however, have done more.

If there is something about the explaination of these operators in the
reference manual that is unclear then please tell us exactly what.

Newbies are uniquely qualified to tell us where and how the manual is
unclear and so help the community improve it. This is something that
experienced Perl users are unable to do because we know what the
manual is trying to say already.

Please take the opportunity afforded you by your _inexperience_ to
contribute while you can.

BTW: Don't (yet) bother trying to understand what exists() (and
delete()) mean when applied to array elements - it's not really
useful[1].

The exists() operator is only really useful applied to hash
elements[2].

[1] It has to do with pseudo-hashes which are deprocated anyhow.

[2] And symbolic function references but that's only relevant if you
are writing AUTOLOAD handlers.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
B

Brian McCauley

Ben Morrow said:
exists applies to array or hash members: it tests whether there
is any value, defined or no, there at all.

Someone with an understanding of the difference between exists() and
defined() could see that what you just said is a true description.

On the other hand it probably wouldn't make any sense someone who
didn't already understand.

For hashes is easiest to think of exists() as testing if a given key
is in list of keys of a hash.

For arrays is easiest to not to think about exists() at all as it
doesn't really do anything useful.

To slightly rephrase what Ben said:

For and array element exists() tests whether there is storage space
for a scalar value (SV) allocated for that element. Even if there is
an SV allocated it may still contain undef.

For most arrays you encounter exists($array[$n]) is true for all $n in
(-@array .. $#array) and false otherwise.

exists($array[$n]) can also be false if you've used delete() on array
elements or have extended the array by assigning $#array.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
B

Ben Morrow

Brian McCauley said:
BTW: Don't (yet) bother trying to understand what exists() (and
delete()) mean when applied to array elements - it's not really
useful[1].

[1] It has to do with pseudo-hashes which are deprocated anyhow.

Is this so? I hadn't realised. Apologies if I confused the OP:
pseudohashes are *definitely* something you don't want to know about
yet (or ever, given that they're going away soon).

Is exists() not still useful for an array element, with exists($a[$n])
being equivalent to ($n < @a)? (delete() obviously isn't.)

Ben
 
S

Steve Grazzini

Ben Morrow said:
Is exists() not still useful for an array element, with exists($a[$n])
being equivalent to ($n < @a)?

Those aren't exactly equivalent:

my @x;
$x[1] = undef;
$#x = 2;

for (0..3) {
printf "x[%d] %s\n", $_,
exists $x[$_] ? "exists" : "doesn't"
}
 
B

Ben Morrow

Steve Grazzini said:
Ben Morrow said:
Is exists() not still useful for an array element, with exists($a[$n])
being equivalent to ($n < @a)?

Those aren't exactly equivalent:

No, but they ought to be. The only reason they're not is to make
pseudohashes work.

Ben
 
B

Brian McCauley

Ben Morrow said:
Is exists() not still useful for an array element, with exists($a[$n])
being equivalent to ($n < @a)?

It is not equivalent - see other posts in this thread.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top