In Perl, an object is a reference that's been blessed into a package. You've got parts of the necessary procedure in your new subroutine, but you need the classname (and the two-argument version of bless -- consult perldoc -f bless for more); when you do the call to new, the first argument passed to the sub is the name of the class (in this case, "mytest").

So change your constructor to:

sub new { my $class = shift; my $self = {}; # makes $self a reference to an anon hash bless $self, $class; $self; # returns $self (blessed hashref) }

(The shift acts on the argument list @_, so you get the first argument with the first shift)

Further, a method receives the object as its first argument, so your methods need to know what object they're talking about:

sub get_ foo { my $self = shift; $self->{foo}; } sub put_foo { my $self = shift; $self->{foo} = shift; }

(both of these methods are *skeletal*, but they'll do for testing purposes =)

Note, also, that if you don't want $self to be a hashref, it doesn't *have to be*, but I'd keep it as one for now.

Good references? perldoc perltoot, perldoc perlbot, and the Tutorials section on this here site. And if you like dead-tree stuff, you can't go wrong with Damian Conway's excellent Object Oriented Perl <-- a review

HTH!

Philosophy can be made out of anything. Or less -- Jerry A. Fodor


In reply to Re: Trying to learn how to build a module by arturo
in thread Trying to learn how to build a module by Yoda

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.