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
In reply to Re: Re: Lexical::Alias & subroutines
by Jenda
in thread Lexical::Alias & subroutines
by Jenda
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |