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.

  • Comment on Re: Perl's equivalent to Python's hasattr and getattr? (class variable details)
  • Download Code