in reply to Re^2: Local for lexicals
in thread Local for lexicals

Seems you just want a complicated way of not writing $_[0] in the sub. That can be done of course. But why would you? And what has this to do with localizing a lexical, which you were asking about in your earlier posts? If you're passing in the parameter to the sub, there's no need to localize anything, is there?
my $f = sub {my $x = shift; sub {$x ** 2}->()}; say $f->(2); # => 4 say $f->(3); # => 9

Replies are listed 'Best First'.
Re^4: Local for lexicals
by LanX (Saint) on Aug 11, 2009 at 00:11 UTC
    because $_[0] is an alias and $x is only a copy, he wants $x to be an alias of the first arg!

    see explanation of second code example in Re^3: Local for lexicals

    JadeNB should really post better test cases to avoid confusion!

    Cheers Rolf

Re^4: Local for lexicals
by JadeNB (Chaplain) on Aug 12, 2009 at 19:26 UTC
    Seems you just want a complicated way of not writing $_[0] in the sub. That can be done of course. But why would you? And what has this to do with localizing a lexical, which you were asking about in your earlier posts? If you're passing in the parameter to the sub, there's no need to localize anything, is there?
    In order:
    1. Yes, that's right (for the lambda example).
    2. Skipped.
    3. Why not?
    4. Only that localising a lexical was my first thought for how to implement it. (Example code is at Local for lexicals.) Once I thought of the problem in that context, it immediately seemed to crop up many other places.
    5. You are certainly right that there are ways to do it without localisation. If I write out the sub—as you've done here:
      my $f = sub {my $x = shift; sub {$x ** 2}->()};
      (formatted, I think, specifically to be faithful to my original attempt), or even more briefly as just:
      my $f = sub { my $x = shift; $x**2 }
      then it certainly works. I would just like to avoid the slight extra verbiage and write instead
      my $f = lambda $x => sub { $x**2 }
      as syntactic sugar (well, for some definition of ‘sugar’) for the same thing.