I was hoping to use lexical scope or something to avoid manually setting the value back to its original value:
but I didnt like the idea of using package variables to achieve it. Is there some way to create a scope and change an object and then have it pop back to the initial settings?sub mylol { my $self=shift; { local $self; $self->{data} = {newdata => 1}; $lol = super(); } }
package L; use Moose; has 'data' => ( is => 'rw', trigger => \&maybe_morph ); sub maybe_morph { my ($self) = @_; if ( $self->can('morph') ) { $self->morph; } } sub lol { my $self=shift; [ lol => 'haha' ]; } sub tree { warn 'tree'; $_[0]; } sub prune { warn 'prune'; } after 'tree' => sub { warn 'aftertree'; }; 1; package LM; use Moose; extends qw(L); override 'lol' => \&mylol; sub mylol { my ($self) = @_; # get data from object my $original_data = $self->data; my $lol; # set data to something superclass can use, # avoid $self->data($val) because that will # cause the trigger to fire $self->{data} = { newdataset => 1 }; $lol = super(); # now that superclass has been called # set $self->{data} $self->{data} = $original_data; use Data::Dumper; warn Dumper( $self->{data} ); $lol; } package main; my $b = LM->new( data => { m => 'em' } ); $b->mylol; 1;
-- Terence Parr, "Enforcing Strict Model View Separation in Template Engines"
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |