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.


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.