in reply to Why are @_ and $_ not linked?
My best guess is that when I open and read the file using <>, it is remembering the $_ from that??
You hit the nail on the head! $_ is global, and is not reset when you call a subroutine. The parameters to a sub are passed via @_, even if there is only one parameter.
As $_ and @_ are completely different entities, sharing only a name, and that you can access the whatever value $_ had before the sub was called when inside the sub is completely unrelated to value of any parameters passed.
That $_ and $_[n] look similar is perhaps the source of your confusion, but they are not related other than by their name. The first is a scalar, then second an array of scalars.
Or perhaps what is fooling you is that with some of perl's built-ins, if no parameters are passed, they default to using the current value of $_. It is possible to code your own subs to do this also
sub thing{ my( $arg) = @_ ? @_ : $_; ... }
but this is generally frowned upon.
Reading that back, I'm not sure if it clarifies anything much, but maybe it does:)
|
|---|