Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

( #444202=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? contributed by jeteve

There's also a module I wrote that generates the accessor methods on demand at run time. You just have to inherit from and you can get/set all your instance variable automagically. It is dynamic and not time consuming since once the accessor method is generated, it's used as a regular perl method. Take a look at: Nice article and perl code It's in french. Enjoy !!

Comment on Answer: How can I use AUTOLOAD to magically define get and set methods for my member data?
Re: Answer: How can I use AUTOLOAD to magically define get and set methods for my member data?
by strat (Canon) on Apr 02, 2005 at 09:03 UTC

    Well, my French is very poor, but I think I understood the essential parts, and if there's written what I understood, I like it.

    There are other interesting possibilities, e.g. creating an anonymous subroutine and write it into symbol table, e.g.

    my $code = sub { # for a better way, see Tanktalus' post below my ($self, $value) = @_; $self->{$attname} = $value if defined $value; return $self->{$attname}; }; no strict 'refs'; *{$attname} = $code; # put it into symbol table use strict 'refs'; return $self->$code(@params); # or: goto &$AUTOLOAD;
    But I prefer - if possible - pregenerating the object interface methods at the startup of the module and not using AUTOLOAD, because I think AUTOLOAD is cool, but not using AUTOLOAD is cooler ;-) e.g.
    my @attributes = qw(color speed); # generate the methods foreach my $attribute (@attributes) { my $code = sub { # for a better way, see Tanktalus' post below my ($self, $value) = @_; $self->{$attribute} = $value if defined $value; return $self->{$attribute}; }; no strict 'refs'; *{ $attribute } = $code; } # foreach

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

      This is where getting perl to do it in perl 6 is going to be cleaner...

      $obj->color(undef);
      Does this set the color to undef? Perhaps you mean:
      my $code = sub { my $self = shift; $self->{$attribute} = shift if @_; $self->{$attribute}; }
      (Falling off rather than returning is just a tiny bit faster according to the benchmarks I've done in the past.) Here, if you pass anything in, it'll get set, but we won't set to undef if you don't pass anything in. Your original is ok if undef is the sole invalid value. That is actually more rare than some people may think - in true perl fashion, by allowing anything, you leave your code more flexible. The primary exceptions are generally external standards, and, even then, should only be when you're outputting them, not interpreting them on input. That is, be conservative in what you send (stick to the letter of the standard), but liberal in what you receive (allow variations on the input). For get/set methods, I find it extremely rare that I don't want the ability to set a variable to undef to signify its invalid or unknown state. For example, setting color to undef simply means that, at this point in the program, I don't know the color, even if I thought I did earlier.

      Yep, this have the advantage to avoid AUTOLOAD. But consider the following point: If you put my AUTOLOAD method in a module called AutoAccess, you just have to write something like
      package Foo ; use base qw/AutoAccess/ ; sub new{ bless { 'a1' => undef , 'a2' =>undef } , shift ; } 1;
      To have the benefits of the auto accessors. The methods 'a1' and 'a2' are implemented 'on demand' to use the nowadays buzzword :) If you'd like to change the implementation and want to keep the behaviour of the class from the user code point of view, you just have to implement by hand the concerned accessor methods. Anyway, I also think that explicitely write which attribute should be concerned by auto accessor generation can be a good thing. Specialy to write code that don't look too magic to new readers :) Is there a way to use the 'my @attributes = qw(color speed);' method in a factorized way ?
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 about the Monastery: (6)
As of 2014-12-28 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (181 votes), past polls