Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
"One small question: how come the id method isn't expecting the class name as it's first argument? Is it only the first sub that does this? (I will read up anyway, but that question stood out from your code)"

If you look at the usage of the id() method (which is an instance method), you'll see INSTANCE->METHOD() (e.g. $obj->id()). INSTANCE ($obj in that example) is the first argument to METHOD().

If it needs to, an instance method can determine the class of the object with ref (as shown with "print "Class Name:   ", ref $obj;" in my original example).

Similarly, a class method has the form CLASS->METHOD() and the first argument is CLASS.

The order in which instance and class methods are defined has no bearing on the type of method they are nor the arguments they receive. A class can have any number of methods: MyDir::Class has two (one of each); MyDir::Class::SubClass has no methods; some other class may have lots of instance methods but no class methods; and so on, there's no rules stipulating what the number or mix must be.

In many cases, the first interaction you have with a class is to create an instance of that class (i.e. an object to work with). Accordingly, the constructor (a class method) is often the first method defined; however, being defined first doesn't bestow any special characteristics.

[Veering off-topic a bit here but included to clarify a couple of misconceptions I've seen over the years. (1) Constructors are often called new() but they don't have to be. (2) A class can have more than one constructor. As an example of both of these points, take a look at PDF::API2 which has three constructors (new(), open() and openScalar()) all of which return PDF::API2 objects.]

I've added two more methods to the MyDir::Class example to demonstrate all of the above. get_instance_count() is a class method that's not special at all: it gets a class as its sole argument and uses it to generate the return value. DESTROY() is an instance method that's a destructor: this is special; it's not something you'll normally need; perlobj: Destructors has details. I've also changed the order of the methods so you now have instance/class/instance/class: this was really just to make a point rather than suggesting some preferred ordering. Also note that while the three methods in the anonymous block needed to be grouped within that block, their order within the block is not important.

$ perl -Mstrict -Mwarnings -le ' package MyDir::Class; # An instance method sub id { my $self = shift; return $self->{id}; } # Anonymous block: code outside this block cannot see %instance_co +unt { my %instance_count; # Constructor: a class method sub new { my ($class, $args_ref) = @_; ++$instance_count{$class}; return bless $args_ref => $class; } # Destructor: an instance method sub DESTROY { my $self = shift; --$instance_count{ref $self}; return; } # Another class method sub get_instance_count { my $class = shift; return $instance_count{$class} || 0; } } package MyDir::Class::SubClass; use base qw{MyDir::Class}; # no methods: all functionality is inherited package main; sub print_all_instance_counts { print "MyDir::Class instances: ", MyDir::Class::->get_instance_count(); print "MyDir::Class::SubClass instances: ", MyDir::Class::SubClass::->get_instance_count(); } # Starting counts print "*** Counts before any objects are created ***"; print_all_instance_counts(); # Create several objects my @objs = map { MyDir::Class::->new({id => $_}) } "A" .. "D"; my @obj_subs = map { MyDir::Class::SubClass::->new({id => $_}) } " +W" .. "Z"; print "*** Counts after initial objects are created ***"; print_all_instance_counts(); # Destroy some objects undef $objs[-1]; pop @obj_subs; splice @obj_subs, 1, 1; shift @obj_subs; print "*** Counts after some objects are destroyed ***"; print_all_instance_counts(); ' *** Counts before any objects are created *** MyDir::Class instances: 0 MyDir::Class::SubClass instances: 0 *** Counts after initial objects are created *** MyDir::Class instances: 4 MyDir::Class::SubClass instances: 4 *** Counts after some objects are destroyed *** MyDir::Class instances: 3 MyDir::Class::SubClass instances: 1

-- Ken


In reply to Re^3: Basic Class question by kcott
in thread Basic Class question by packetstormer

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-03-28 19:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found