in reply to Using $_ as a temp var, especially in functions
Although I rarely find the need to use $_ as a temporary variable, this idiom makes sense for me. I agree with converter that when doing lots of m//, tr///, s///, and builtins that default to $_, it's a good timesaver.for ($user_input) { s/foo/bar/g; tr/A-Z/a-z/g; print unless /^#/; }
Pros: Any pros of using local($_). Seeing a for() loop (that doesn't have for my $i ()) automatically signals a localized $_ in my brain, so I can interpret the body correctly. It works better for me than just saying local($_) at the top. The indented structure of the for loop is a great visual sign that there are scoping changes going on. Also, since $_ is aliased to $user_input, you are actually performing the same operations as if you'd written everything out like $user_input =~ s/foo/bar/;, etc. This is also a con ;)
Cons: Using a "looping" flow-control structure that will never loop might be confusing. It's like seeing one of these: do { foo; } while (0);, which might (understandably) freak you out. You may also expect the loop to not affect the value of $user_input, since it only modifies $_. Wrong, $user_input gets changed in this code.
blokhead
|
|---|