Skip to content

Instantly share code, notes, and snippets.

@preaction
Created June 4, 2012 22:52
Show Gist options
  • Select an option

  • Save preaction/2871329 to your computer and use it in GitHub Desktop.

Select an option

Save preaction/2871329 to your computer and use it in GitHub Desktop.
De-PBP ALL THE Moose-Y THINGS
sub de_pbp($) {
my ( $class ) = @_;
my $meta = $class->meta;
for my $attr_name ( $meta->get_attribute_list ) {
my $attr = $meta->get_attribute( $attr_name );
next if $class->can( $attr_name ); # Don't overwrite something already here
if ( $attr->reader eq "get_$attr_name" && $attr->writer eq "set_$attr_name" ) {
# Also install the non-PBP version
# Have to use globref because we're immutable...
no strict 'refs';
*{"${class}::$attr_name"} = sub {
my ( $self ) = @_;
if ( scalar @_ > 1 ) {
return $self->can( "set_$attr_name" )->( @_ );
}
return $self->can( "get_$attr_name" )->( @_ );
};
}
}
return 1; # True for the end of the module
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment