Created
June 4, 2012 22:52
-
-
Save preaction/2871329 to your computer and use it in GitHub Desktop.
De-PBP ALL THE Moose-Y THINGS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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