http://qs1969.pair.com?node_id=479170

MistaMuShu has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm starting to learn about Perl objects and having trouble understanding the internal workings. As I understand it, instance variables are stored in the "referenced thingy". But how does Perl do class variables? Since a 'class' is a package, is it correct to say that variables local to a package are "class variables"?

package Thing; my $class_var = 1; sub new { return bless {}, "Thing"; } sub set_class_var { $class_var = $_[0]; } 1;

I tried this out and it seems to make sense, but I hope someone can rephrase this more articulately. The other question I have concerns how class variables are stored. I want to have a single "set" method to change class variables rather than one per variable. Something along the lines of:

sub set { my $self = shift; my %args = @_; while( my ($var, $val) = each %args ) { #if $var is defined #set class_variable var to $val } }

In Python, I could use hasattr, getattr, and setattr to accomplish this. What's the Perl style to do it?

I apologize in advance if I blurted out any rubbish or missed something searching around. Any interesting beginner links to closures, Perl object internals, and symbol tables would also be cool. I'm in the process of reading the ones in the tutorial section, but still struggling through it somewhat. Thanks all in advance.