in reply to Re^2: dereferencing question
in thread dereferencing question

The only time I use single character variable names like $k is when the scope is going to be something really small like five to ten lines. For example,

foreach my $k ( keys %DBhandles ) { $DBhandles{ $k }{ sth }->finish; $DBhandles{ $k }{ dbh }->disconnect; }
And yes, I could minimize that to just using $_ instead of $k, but I try to never use that special variable, because it requires extra effort for the reader.

Alex / talexb / Toronto

For a long time, I had a link in my .sig going to Groklaw. I heard that as of December 2024, this link is dead. Still, thanks to PJ for all your work, we owe you so much. RIP Groklaw -- 2003 to 2013.

Replies are listed 'Best First'.
Re^4: dereferencing question
by cavac (Prior) on Jan 23, 2025 at 09:01 UTC

    Yeah, $_ (especially the implicit version) is something to generally avoid. It's not only harder to read and understand, but it's also easy to mess up a program when you have to re-shuffle some logic.

    As for single-character iterators, i mostly use them on C-style for loops. $i, $x, $y are my favourites, with $i for general stuff. $x and $y are always sensible choices when iterating over the pixels of an image, plus there aren't that many other variable names (english words) that start with an "x" or "y" that also make sense in a cash register application.

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
    Also check out my sisters artwork and my weekly webcomics