in reply to Passing an anonymous block to a method.

The following works, and arguably feels cleaner than a prototyped method would assuming those worked too:
package something; sub new{ bless {}, __PACKAGE__ } sub doit { my ($self, $subref, $key) = @_; local $_ = $self->{ $key }; $subref->(); $self->{ $key } = $_; } package main; my $o = something->new; $o->doit(sub { $_ += 1 }, 'fred'); print "'$o->{fred}'\n"; # Outputs 1

Update: Simplify code per AnomalousMonk's recommend.

Replies are listed 'Best First'.
Re^2: Passing an anonymous block to a method.
by AnomalousMonk (Archbishop) on Mar 10, 2011 at 21:44 UTC

    I don't understand the comment
        # Prevent $subref from playing with vars
    which seems to refer to the lexical variable definition
        my ($self, $key);
    which follows it and effectively masks lexicals of the same name defined in a superior scope. Can you please explain further?

      AnomalousMonk,

      You are correct. I was mistakenly being overprotective since I was treating an anonymous sub invocation like I was doing an eval. There is no need to worry about scoping of anything except global variables when calling the anon sub. Thank you for making me rethink this.

      Nevertheless, if this was anything more than an example, I would also redesign the method so that it doesn't require an explicit assignment to $_ within the anonymous sub but instead does that assignment for you.

      $self->{ $key } = $subref->();

      I chose to add parameter variables as a way of making the method self-documenting, but I didn't want the anonymous subroutine to have access to the deeper mechanisms of the object. Ideally, the only variable within the anonymous sub will be $_. However, to prevent a user from taking advantage of the lexicals $self and $key, I explicitly hid them.

      If this was anything more than an example, I probably would've localized @_. But then again, I would also redesign the method so that it doesn't require an explicit assignment to $_ within the anonymous sub, but instead does that assignment for you.

      $self->{ $key } = $subref->();
Re^2: Passing an anonymous block to a method.
by AnomalousMonk (Archbishop) on Mar 11, 2011 at 06:00 UTC
    ... to prevent a user from taking advantage of the lexicals $self and $key, I explicitly hid them.

    But  $self and  $key are lexicals defined within the scope of the  doit subroutine (in the example given in Re: Passing an anonymous block to a method.) and so could not possibly (without resort to something like PadWalker*) be accessed by any code defined in a block outside of  doit such as the one used to create the subroutine reference in the example. Am I missing something?

    * Actually, AFAIU PadWalker, the lexicals  $self and  $key of  doit would still not be accessible via PadWalker because  doit is not called from the anonymous subroutine, nor are those lexicals in scope at the time the anonymous code executes. Update: On second thought, those lexicals would be accessible because the anonymous subroutine is called from  doit and the lexicals are in scope at the time of the call; further, they would be accessible even in the presence of masking lexicals however, experiment shows they would not be accessible in the presence of masking lexicals.

    Update: Oops: All this mess was actually intended to be a reply to Re^3: Passing an anonymous block to a method..