On 12/3/10 Fri  Dec 3, 2010  8:30 AM, "Roche, Johnny"
<[email protected]> scribbled:

> To my understanding, "$_" is the default input of the current running
> script/program, hence, if you overwrite $_ in an inner loop, it will overwrite
> $_ for the whole process.  If you do not want to overwrite it, you'll need to
> assign $_ to a scalar before entering the loop that will overwrite $_.
> 
> Does that help or am I way off base?

Only slightly off base. $_ is the default variable for many Perl operators,
like <input>, chomp, split, for, etc. There is no default input for a
running program. 

The <> operator has a set of default files for input: files mentioned on the
command line and placed in the @ARGV array.

$_ is a global variable, and can get over-written by any operation that uses
it. However, some operators like foreach can localize $_ so that the global
value gets restored when they have completed.

You also have to keep in mind that $_ can be an alias to another value, and
changing the value of $_ changes the original value.

Because it is hard to keep track of all of these special cases, it is better
to use explicit variables.



-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to