PBP: 070 Iterator Variables

Mr. Conway suggests that you give explicit names for all for loops, and avoid the possibly confusing use of $_.  To this I say: Hallelujah!

I consider $_ one of the biggest warts of Perl.  It’s a misfeature, and it leads to overly dense, confusing code.  I think it makes it a lot harder to tell what the functions are doing.  I dislike the use of $_ and consider code that uses it to be lower-quality and written by someone less mature and professional.  It will, in my mind, need cleanup and refactoring to be maintainable.

Part of this is that, as an engineer it makes reading the code a lot harder.  I have to keep a stack in my head of what $_ has in it right now, and that makes reading code harder.

Part of it is the bizarre inconsistency in perl with what works with $_ and what doesn’t – the book has a great question on page 106 – which of those built-ins works with $_?  I know I had the list entirely wrong.

I understand that $_ might be helpful when writing Perl scripts that run right on the command line.  I never do this, so I don’t really know.  In my experience, that’s a much less frequent case than trying to write a usable and maintainable Perl script.

One reason I dislike map and grep is they require the use of $_.  I wish they could get named variables;  map blocks are often some of the densest and hardest to read chunks of Perl out there.

Leave a Reply