in reply to Passing an anonymous block to a method.
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 | |
by wind (Priest) on Mar 10, 2011 at 22:47 UTC | |
|
Re^2: Passing an anonymous block to a method.
by AnomalousMonk (Archbishop) on Mar 11, 2011 at 06:00 UTC |