Subject: | it would be nice to have a re-default method much like clearer |
With clearer setting everything to undef, you run in to issues with
non-undefable data types. It would be nice to have a re-default so that
you could do a rollback of an object. Currently I just re-set attr to
the default value (example below) but I'm sure that there's a smarter
way to go about it.
#!/usr/bin/perl
use strict;
use warnings;
use Test::Most qw{no_plan};
BEGIN {
package My::Test;
use Moose;
has int => (
is => 'rw',
isa => 'Int',
default => 10,
);
has hash => (
is => 'rw',
isa => 'HashRef',
default => sub{{}},
);
sub redef { my ($s,$n)=@_;
my $d = $s->meta->find_attribute_by_name($n)->default;
$d = $d->() if ref($d) eq 'CODE';
$s->$n( $d );
}
} #END BEGIN
my $t = My::Test->new;
is( $t->int, 10 );
ok( $t->int(100) );
is( $t->int, 100 );
ok( $t->redef('int') );
is( $t->int, 10 );
is_deeply( $t->hash, {} );
ok( $t->hash({ qw{hello world} }));
is_deeply( $t->hash, { qw{hello world} } );
ok( $t->redef('hash') );
is_deeply( $t->hash, {} );
--
benh~