I would make them methods. Reasons for this:

Treat them like other code. There is no reason not to. In a class, letting the routines be methods rather than functions (or whatever you want to call it) are the idiomatic norm, not the exception.

Consistency is good on a more pragmatic level too. By having all routines being methods, you don't need to keep track of whether $self / $class should be shifted off @_ or not. You don't need to worry about which routines are called in a particular way -- they're all $self->whatever(). I find myself making this specific mistake surprisingly often when some things were made subroutines.

The problem with not passing around $self (i.e. treating them like methods) is that sooner or later you'll need it in a routine in order to call a method on $self, and then you'll need to rewrite all of the things that call this routine, and everything that calls those routines, etc.

If a routine is clearly not related to the object instance ($self), make it a class method (shift off $class, call the method like this: My::Class->whatever() ) instead, because...

You almost certainly will want to override one of them in sub classes sooner or later, for the same reason you might want to override the rest of your methods. If you write a CPAN library, this is even more frustrating for other people who have needs to tweak the code that you didn't anticipate at the time.

/J


In reply to Re: Object Oriented packages - classes and subroutines by jplindstrom
in thread Object Oriented packages - classes and subroutines by zby

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.