Personally, if an intermediate var is used is in more than one line of code (or possibly two), it no longer warrents the nomenclature "temporary". At some point in the future, someone (possibly you:) is going to come along and insert some code between some of the lines where the 'temporary var' is used and that seperates it from its temporaryness (Is that a word?).
In any other language I've used, my advice would be to just go ahead and use a locally-scoped (lexical in Perl terminology ie. my $tempSomething). However, the propensity (and utility) of Perl's built-ins to use $_ leads to a slightly ambiguous decision, in that I do use $_ where that makes sense because I am going to apply one or more built-in features which default to that.
However, I wouldn't use the for ($thingToAlias) { ... }; as I personally find the one-time-loop construct distracting. In preference I would use a bare block.
{ ## Updated: The next line was (erroneously) local $_ = $thingToAlias, local *_ = \$thingToAlias; ##which [Aristotle] pointed out *doesn't* alias m/regex1/; s/regex2/modification/; #other stuff }
To me (who is still experimenting and evolving my Perl Style), this has all the advantages of the 1-time for with none of it's disadvantages. It will be interesting to see if anyone has any arguments against this.
In reply to Re: Using $_ as a temp var, especially in functions
by BrowserUk
in thread Using $_ as a temp var, especially in functions
by BUU
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |