PBP: 069 Necessary Subscripting

In those places you do actually need both the key and value, you will have to go ahead and do the lookup on the key to get the value.  The PBP reminds us to only do that once, instead of many times in the loop.

I’d like to say, “Well, duh!” but I find it easy to forget to do this.  Rather than creating a lexical with the values I need, I’ll reference it multiple times.  It’s duplicate work, and foolish, but I do it thoughtlessly.  Don’t be like me, create a temporary variable!

The PBP also suggests CPAN’s Data::Alias (or the older Lexical::Alias, for really old perls) if we need real alias access to the variable, too.  I didn’t know this was there!

One Response to “PBP: 069 Necessary Subscripting”

  1. Shawn H Corey says:

    If the order doesn’t matter, use `each`. See `perldoc -f each`, http://perldoc.perl.org/functions/each.html

Leave a Reply