Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Answer: How can I use AUTOLOAD to magically define get and set methods for my member data?

( #444212=categorized answer: print w/ replies, xml ) Need Help??

Q&A > object-oriented programming > How can I use AUTOLOAD to magically define get and set methods for my member data? - Answer contributed by jeteve

Ok, for the non french reader, this is the AUTOLOAD method generating code :
sub AUTOLOAD{ my ($self,$value)= @_ ; return if $AUTOLOAD =~ /::DESTROY$/ ; my $attname = $AUTOLOAD; $attname =~ s/.*::// ; if(! exists $self->{$attname}){ Carp::confess("Attribute $attname does not exists in $self"); } my $pkg = ref($self ) ; my $code = qq{ package $pkg ; sub $attname { my \$self = shift ; \@_ ? \$self->{$attname} = shift : \$self->{$attname} ; } }; eval $code ; if( $@ ){ Carp::confess("Failed to create method $AUTOLOAD : $@"); } goto &$AUTOLOAD ; }

  • Comment on Answer: How can I use AUTOLOAD to magically define get and set methods for my member data?
  • Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2016-07-23 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?


















    Results (221 votes). Check out past polls.