in reply to Re: Lexical::Alias & subroutines
in thread Lexical::Alias & subroutines
Of course it is possible to use the $_[CONST] (I do it myself from time to time), the problem is that the scope of the CONST is too wide. The constant is "global", defined in a package, so all subroutines in that package might use it, even if it doesn't make sense there. And if I had two functions that have the "conceptualy same" parameter on a different place I'd have to define two constants with similar names, which could be very confusing.
Besides with the alias you can/could do something like
How's that written with constants?sub foo { alias ($_[0] eq 'whatever' ? $_[1] : $_[2]) => my $x; ... }
Another case when I'd love to have a lexical alias is when the function gets an array or hash reference, I do want to modify the array/hash, but do not like to have to dereference it all the time:
You are right about the speed issues, but that's not the point. Readability is the main concern here.sub foo { my ($aref) = @_; alias @$aref => my @ary; ... do something with @ary }
P.S.: Thinking about it some more. It's better that the alias() doesn't my() the variable.
Jenda
Always code as if the guy who ends up maintaining your code
will be a violent psychopath who knows where you live.
-- Rick Osborne
Edit by castaway: Closed small tag in signature
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Lexical::Alias & subroutines
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 |