Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by chromatic (Archbishop)
on Apr 20, 2000 at 23:20 UTC ( #8248=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 chromatic

Here's a quick and dirty autoloader. It assumes your member data is stored in a hash, and that the get and set functions take the form "get_foo" and "set_bar".
use vars '$AUTOLOAD'; # a while later sub AUTOLOAD { my $self = shift; return if ($AUTOLOAD =~ /DESTROY/); # don't mess with garbage c +ollection if ($AUTOLOAD =~ /.*::set_(\w+)/) and (@_) { return $self->{$1} = shift; } elsif ($AUTOLOAD ~= /.*::get_(\w+)/) { return $self->{$1}; } # handle bad methods here, return undef ? }

  • 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?
[james28909]: politicians making jobs for themselves? that is absurd! we all know how hard those politicians have it, having to take millions from lobbiest while screwing over their citizens. have a little sympathy why dont ya!

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2017-12-15 18:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (441 votes). Check out past polls.

    Notices?