in reply to Re^4: Local for lexicals
in thread Local for lexicals
or if there were a lex_local that could just be dropped in for lexicals as local can for globals.
First of all local is the wrong approach, because local just manipulates already declared pack-vars, so under strict you would be forced to predeclare your variables with "our" (or "my") in the outer scope. (look at first our in the following code) (see update)
I would love to have a lexical local in P5 BUT not with the extra magic of declaring missing variables your assuming.
What you really want is aliasing like $_[ ] variables do.
I know about *, although it doesn't seem to do much for lexicals, but what do you mean by pack-vars?
Aliasing with package-variables
use strict; use warnings; $\="\n"; my $lamb = sub { our ($d,$e); # local doesn't declare!!! local (*d,*e)=\(@_); $d += $e ; }; our $d="D"; our $u=10; $lamb->($u,5); print $u; #> 15 print $d; #> D
However, I don't know how to improve.from your /msg:
What can I do to improve? I try to phrase my questions as (1) an informal description of what I want; (2) imaginary code that'll do what I want; (3) descriptions of what I've tried, and what I don't want to do (ideally, with explanations).
some ideas:
Cheers Rolf
UPDATE: as JavaFan pointed out here you are not forced to use our with full-qualified varnames, so this works
but I don't know if this meets your criteria of being more elegant than using $_[0] += $_[1] and furthermore it's only a pack-var thing, in P5 lex-vars have no namespaces...my $lamb = sub { # our ($d,$e); local (*::d,*::e)=\(@_); $::d += $::e ; };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Local for lexicals
by JavaFan (Canon) on Aug 11, 2009 at 13:39 UTC |