in reply to scoping question
Declaring variables once at the beginning of the code block where they are being first used is the best practice imo. One other alternative (yet poor) approach is to use the special $_ variable.
Update: As ikegami pointed out (++), I stress that the use of $_ is a bad idea. I just wanted to show one alternative, albeit a bad one as a means of warning against such approach. :)
Generally, however, this wouldn't be a very sound approach, as it is usually very easy to loose track of the $_ variable. Although, it does spare you having to declare variables, especially for short code blocks or loops.if ( $_ = $obj->get_id() ) { # use $_ inside this block... $obj->do_this(); } elsif ( $_ = $obj->create_id() ) { # use $_ inside this block $obj->do_that(); } else { $obj->do_somethingelse(); }
Update: As ikegami pointed out (++), I stress that the use of $_ is a bad idea. I just wanted to show one alternative, albeit a bad one as a means of warning against such approach. :)
_____________________
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce
the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true." - Robert Wilensky, University of California
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce
the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true." - Robert Wilensky, University of California
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: scoping question
by ikegami (Patriarch) on Mar 02, 2006 at 16:47 UTC |
In Section
Seekers of Perl Wisdom