in reply to Re^3: Prototype (_)?
in thread Prototype (_)?
I wanted to write a sub that defaulted to operating upon $_ per chop, chomp and others.
The problem I had was that I tried:
sub x(_) { my $s = shift; ## make changes to $s ... }
but in the calling code, $_ remained unmodified.
As soon as I saw Corion's example above, it became obvious that when assigned to a named var, the named var held a copy rather than an alias; and that to achieve my goal, I would need to use $_[n]; just as with a normal subroutine: but not before seeing it. Silly perhaps, but ...
I think the addition of a hint about aliasing or a short, working example would strengthen the documentation and perhaps save other people the same frustration.
|
|---|