Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by saucepan (Scribe)
on Dec 05, 2000 at 06:48 UTC ( #44911=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 saucepan

If you do this, you may want to provide a new can() method for people to use:

# can - Help UNIVERSAL::can() cope with our AUTOLOADed methods sub can { my ($self, $method) = @_; my $subref = $self->SUPER::can($method); return $subref if $subref; # can found it; it's a real method # Method doesn't currently exist; should it, though? return unless exists $self->{$method}; # Return an anon sub that will work when it's eventually called sub { my $self = $_[0]; # The method is being called. The real method may have been # created in the meantime; if so, don't call AUTOLOAD again my $subref = $self->SUPER::can($method); goto &$subref if $subref; $AUTOLOAD=$method; goto &AUTOLOAD }; }

(This one assumes that people will often call can() without intending to use the method ref it returns, and so it defers calling AUTOLOAD until someone goes to use it. Because of this, it needs a second check to see if someone else has already AUTOLOADed the desired method in the meantime.)

  • 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?
[Corion]: choroba: Yeah - that's why the "old" airports of Berlin were cool, as they'd been built before ;)

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2017-02-23 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (346 votes). Check out past polls.