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

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



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to