in reply to Are we seeing syntax inconsistency?
As others have said, your first example uses a closure, which implicitly creates a copy of the variable when the anonymous sub is created, so the sub refers back to that variable. Closures keep lexically-scoped variables (those created with my), but $_ isn't lexically scoped. That's the key to the difference between the two.
You could also solve the problem by forcing the anonymous sub to resolve $_ when it's created, using eval:
(n.b. I haven't tested this and it might not be exactly right). But using closures is much easier, faster, and more elegant.-command => eval "sub {print $_}"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Are we seeing syntax inconsistency?
by Aristotle (Chancellor) on Nov 11, 2005 at 17:56 UTC | |
by sgifford (Prior) on Nov 11, 2005 at 18:22 UTC | |
by Aristotle (Chancellor) on Nov 11, 2005 at 18:29 UTC | |
by sgifford (Prior) on Nov 11, 2005 at 18:56 UTC |