in reply to Lexical::Alias & subroutines
Update: I'm blind, can't read. (Ignore the contents of this post other than to -- it.). I, as jd points out, completely screwed this one up.
This is just a scoping issue. If you have a lexical at the outer scope and you declare a lexical at the inner scope (as in your example) the inner scope only sees the new version not the old. As you did the aliasing at the outer level and not the inner, the inner lexical isn't aliased. The outer will still be so once you return.
You could either, not declare the inner lexicals of the same name, OR alias the inner copy of $alias to $orig, and you would get the results you are expecting.
Like halley, I'm not quite sure why you would want to do this. $_[0] is already an alias for the first parameter passed. If that parameter is a reference, then using it as an lvalue will always affect the thing it refers to.
If the idea is to give a meaningful name to the parameter without copying the reference (a very cheap operation anyway) then you could go with defining a constant
use constant BYREF=>0; sub something{ $_[BYREF] = 1000 if $_[BYREF] > 1000; ... }
The win is minimal unless the sub is being called zillions of times, but using a defined CONSTANT cost nothing at runtime and is much cheaper than allocating a lexical and then calling a sub to alias it to $_[0], which is much more expensive that just assigning the ref to an local-scope lexical.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Lexical::Alias & subroutines
by jdporter (Paladin) on May 19, 2003 at 13:17 UTC | |
|
Re: Re: Lexical::Alias & subroutines
by Jenda (Abbot) on May 19, 2003 at 15:09 UTC | |
by BrowserUk (Patriarch) on May 19, 2003 at 15:42 UTC | |
by Jenda (Abbot) on May 19, 2003 at 15:56 UTC | |
by BrowserUk (Patriarch) on May 19, 2003 at 17:08 UTC |