Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Perl's equivalent to Python's hasattr and getattr? (class variable details)

by Ovid (Cardinal)
on Jul 28, 2005 at 23:51 UTC ( [id://479179]=note: print w/replies, xml ) Need Help??


in reply to Perl's equivalent to Python's hasattr and getattr? (class variable details)

Regrettably, Perl does not have a clean distinction between classes and instances of said classes. As a result, class data is something that folks tend to forget about. One way of creating your set method is to store your class data in a hash.

my %CLASS_DATA = (...); sub set { my ($self, %args) = @_; while (my ($var, $val) = each %args) { if (exists $CLASS_DATA{$var}) { $CLASS_DATA{$var} = $val; # whoops! No validation } else { # create the var, croak, etc. } } }

If you don't like the validation, you could have each value in %CLASS_DATA be a sub ref that points to the real setter.

Personally, I've enjoyed Class::MethodMaker to handle much of this for me, though the recent test failures are disheartening.

Cheers,
Ovid

New address of my CGI Course.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://479179]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found