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

sub foo { alias ($_[0] eq 'whatever' ? $_[1] : $_[2]) => my $x; ... }
How's that written with constants?

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:

sub foo { my ($aref) = @_; alias @$aref => my @ary; ... do something with @ary }
You are right about the speed issues, but that's not the point. Readability is the main concern here.

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

    In your first example

    sub foo { my $x = $_[0] eq 'whatever' ? $_[1] : $_[2]; ... }

    Seems just as effective. But in your second, I agree. I've looked enviously at the perlsub examples of using globs to avoid the need to dereference refs and wished for a way to do the same thing with lexicals. Maybe Diotalevi's Lexical::TypeGlob would work for that? I haven't gotten around to playing with it yet.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

      But it is not the same. Your code creates a copy. So the changes to $x do not affect the parameters to foo().

      Lexical::TypeGlob doesn't seem to be related.

      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

        Right you are. I'm not having a good thread am I.

        Maybe this will make up for it -- but then again...

        #! perl -slw use strict; sub doit{ our $scalar; local *scalar = ($_[0] eq 'first' ? \$_[1] : \$_[2]); $scalar = 'new value'; } sub double'em{ our @array; local *array = $_[0]; $_ *= 2 for @array; } my ($x, $y) = ('old value') x 2; doit 'first', $x, $y; print "$x, $y"; my @a = 1 .. 10; double'em \@a; print "@a"; __END__ D:\Perl\test>temp new value, old value 2 4 6 8 10 12 14 16 18 20

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller