Help - Search - Member List - Calendar
Full Version: When to use defined
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Tielman Koekemoer \
Hi all,

When checking if the hash key exists, should I use defined() or just:

do_something if $hash{val}


I have done some tests and it seems there is no difference in
performance with or without defined(). When should defined() be used
then if it makes no difference?

TIA!


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This e-mail and its contents are subject to the Telkom SA Limited
e-mail legal notice available at

http://www.telkom.co.za/TelkomEMailLegalNotice.PDF
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Xavier Noria
On Jul 5, 2005, at 12:15, Tielman Koekemoer ((TNE)) wrote:

QUOTE
When checking if the hash key exists, should I use defined() or just:

do_something if $hash{val}

I have done some tests and it seems there is no difference in
performance with or without defined(). When should defined() be used
then if it makes no difference?

Performance at that low-level rarely matters, when in doubt use
Benchmark.pm.

Take into account that a key can have undef as associated value in a
hash so those examples are not right indeed. The valid test in Perl is

do_something if exists $hash{key};

Sometimes one knows that the values of some particular hash are true,
in that case one relaxes a bit and writes

do_something if $hash{key};

but that's fragile. Note that I said "true", the value "0" is defined
but false, so that test would fail. Fragile, you see.

Thus, except for really quick and dirty short scripts I use exists()
when I mean exists().

-- fxn

Ing. Branislav Gerzo
Tielman Koekemoer (TNE) [TK], on Tuesday, July 5, 2005 at 12:15
(+0200) wrote these comments:

TK> I have done some tests and it seems there is no difference in
TK> performance with or without defined(). When should defined() be used
TK> then if it makes no difference?

did you tried:
$hash{val} = 0;
do something if $hash{val};

you should come to difference.

--

...m8s, cu l8r, Brano.

["That little droid and I have been through a lot together." - Luke]

John W. Krahn
Tielman Koekemoer (TNE) wrote:
QUOTE
Hi all,

Hello,

QUOTE
When checking if the hash key exists, should I use defined() or just:

do_something if $hash{val}

I have done some tests and it seems there is no difference in
performance with or without defined(). When should defined() be used
then if it makes no difference?

exists() tests whether the *key* exists while everything else tests the
*value* and in some contexts may autovivify the key.

This may help:

perldoc -q "What's the difference between "delete" and "undef" with hashes"


John
--
use Perl;
program
fulfillment


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.