in reply to Local for lexicals

A patch to make my do this has been turned down multiple times. It is technically easy to do, but Larry Wall's opinion is that it is too confusing.

But local will work on elements of complex data structures. So you can declare a lexically scoped hash and then do things like local $foo{bar} = "baz";

Replies are listed 'Best First'.
Re^2: Local for lexicals (local $_[...])
by tye (Sage) on Aug 10, 2009 at 18:10 UTC
    sub lambda { my $f = $_[1]; for my $x ( $_[0] ) { return sub { return localize( $x, $_[0], $f ); } } } sub localize { my( $lexical, $val, $f )= @_; local( $_[0] )= $val; $f->(); }

    (Updated order of arguments for no particularly good reason.)

    Update: This, however, doesn't work (despite not generating an error). This is because (I believe) local localizes $_[0] by replacing the alias with a new SV that gets set to the new value. The prior value is restored by restoring the alias back into that spot in the array. Thus, the alias is never modified and thus the value of the lexical is never changed.

    - tye        

Re^2: Local for lexicals
by JadeNB (Chaplain) on Aug 10, 2009 at 16:49 UTC
    Thanks, that's an extremely helpful response! I could probably twist my use cases (not the lambda example, perhaps, but some of the more dastardly ones) to have this approach work, too.

    Is it possible to see the patch?

      I'm sorry, but I'm on vacation and don't have time to search old mailing lists for the patch. Plus the code may have changed since the patch that I saw rejected several years ago.
Re^2: Local for lexicals
by JadeNB (Chaplain) on Oct 02, 2009 at 15:54 UTC
    A patch to make my do this has been turned down multiple times. It is technically easy to do, but Larry Wall's opinion is that it is too confusing.
    Do I read this bit of Apocalypse 6 correctly to indicate that we need only wait until RSN to be able to ‘localise’ whatever we please?