in reply to Re^3: 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?
In order:
- Yes, that's right (for the lambda example).
- Skipped.
- Why not?
- 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.
- 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.
|