G'day packetstormer,

Take a look at this code:

$ perl -Mstrict -Mwarnings -le ' package MyDir::Class; # A class method sub new { my ($class, $args_ref) = @_; return bless $args_ref => $class; } # An instance method sub id { my $self = shift; return $self->{id}; } package MyDir::Class::SubClass; use base qw{MyDir::Class}; # no methods: all functionality is inherited package main; # Create a MyDir::Class instance my $obj = MyDir::Class::->new({id => "X"}); print "Class object: ", $obj; print "Class Name: ", ref $obj; print "Object ID: ", $obj->id(); # Create a MyDir::Class::SubClass instance my $obj_sub = MyDir::Class::SubClass::->new({id => "Y"}); print "Class object: ", $obj_sub; print "Class Name: ", ref $obj_sub; print "Object ID: ", $obj_sub->id(); ' Class object: MyDir::Class=HASH(0x7ffeda002ee8) Class Name: MyDir::Class Object ID: X Class object: MyDir::Class::SubClass=HASH(0x7ffeda029cb0) Class Name: MyDir::Class::SubClass Object ID: Y

There's a class called MyDir::Class. It has two methods. A class method (new()) which, as you've already noted, has the class name ($class) as its first argument; a common usage for the class name is shown: yes, it's useful. An instance method (id()) which has an object (a class instance) as its first argument ($self); a common usage for the object is shown: again, it's useful. Note: there's no exporting involved!

There's a second class called MyDir::Class::SubClass. It has no methods: all functionality is inherited. Note: there's no importing or exporting involved!

Finally, there's the main package where objects are created (via new() — also called a constructor) and where those objects invoke methods. Note: there's no importing involved!

I've written all that code on the command-line as a single, multi-line string. In a real world situation, the modules would be in separate *.pm files and the script (in yet another file) wouldn't have a package main; statement but would have use statements for the module(s) involved.

I see you've already been provided with the perlootut - Object-Oriented Programming in Perl Tutorial link: that covers the basics. Also take a look at perlobj - Perl object reference and particularly its Invoking Class Methods section which has information specific to your question.

You haven't shown the code where you're exporting functions. While you may have a valid reason to do this, you typically don't: use inheritance instead of export/import mechanisms.

I see you wrote "... &function("data") ...". Don't prepend an ampersand ("&") to your function calls unless you have a specific reason for doing this and understand what that reason is. In general, this isn't what you want and could potentially have unexpected side-effects: see perlsub - Perl subroutines for details. For an OO context, take note of "&NAME(LIST); # Circumvent prototypes." and, further down in the Prototypes section, "Method calls are not influenced by prototypes ...".

-- Ken


In reply to Re: 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":



  • 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.