There's a difference between lexically scoping with
my and localising using
local, although the terminology is one that seems rather contentious. Like the difference between arrays and lists.
There are only a few things you still need to localise with
local, such as the
$" variable. Ever since 5.0 came out, a lot of effort has been put into moving people away from
local and to
my, for example, the way you can use lexically scoped filehandles. Where possible, it's good to use filehandles like that since they can be passed around easily from subroutine to subroutine, or stored in an object's hash easily. In this example, though, I'm using Old-School handles because it's not really an issue.
As for loops, I won't hesitate to use
$_ as long as it doesn't get too complicated. Where it's obvious what's being iterated, that is. For example:
$_->iron() foreach (@shirts);
You could say
my $shirt, but it would be redundant. On the other hand, where there's no hint as to what you're using, a simple
my declaration acts as documentation.
foreach my $shirt ($laundry->contents()) ...
You have to be careful with
$_, just like with
$1 and its relatives. Where there's risk of contamination, I use alternate names or copies. If there's no risk, then it's a matter of preference.
I think my point is really this: Don't use
local $_, instead, use a named lexical variable such as
my $line.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.