Kevin Scaldeferri on 25 Nov 2003 19:35:38 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [ALACPP] How Perl sucks even worse than C++ when it comes to assoc arrays


Christopher Smith wrote:

Of course, I thought to myself, this problem can all be solved nicely
using a language where assoc arrays are built in to the language. This
made me think of Perl. In Perl doing:

$foo{'bar'};

does not create an entry in %foo with a key of 'bar'. However:

$foo{'bar'} = 'baz';

will. All magical and wonderful right? Well, I discovered this one the
other day:

if (defined($foo{'bar'}->{'baz'})) {
#some stuff
}

...


Autovivification really is your friend 99% of the time. Experienced Perl hackers know to write

if (exists($foo{'bar'})  && defined($foo{'bar'}->{'baz'}) ) {
...
}

Of course, if you're typo prone, this might not help you and you might want to look into pseudohashes (get 'em while you still can!). Or you could go OO.

Supposedly in the grand future of Perl 6, autovivification will be controllable via a pragma.


Kevin

_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp