I have inherited some old Perl 5.8 code. It is a straightforward, old-fashioned Perl OO module, more "object based" than "object oriented" in that there is no inheritance.

I noticed some of the "instance methods" are not really instance methods in that though they start with my $self = shift they do not actually use $self in the function body. That is, the class is a mixture of instance methods (related to the class and using object state) and utility functions (not really related to the class and not using object state). The user of the class currently calls both these different types of functions in the same way, via the object handle, for example:

my $obj = MyPackage->new("..."); # ... my @some_list = $obj->some_method("some string");

I seem to have three options:

  1. Leave the utility functions as instance methods
  2. Convert them to class methods
  3. Convert them to standalone utility functions
For option three above, I could leave them in the same package, or move them to a new package consisting of utility functions only. I seek your advice on how best to deal with this.

To illustrate with some sample code, here is MyPackage.pm:

package MyPackage; use strict; use warnings; sub new { my $class = shift; $class eq __PACKAGE__ or die "oops: ctor unexpected '$class'"; my $name = shift; print "in ctor: '$class' (name=$name)\n"; my $self = {}; $self->{NAME} = $name; bless $self, $class; return $self; } sub some_instance_method { my $self = shift; my $param1 = shift; print "in some_instance_method: $self '$param1'\n"; return $self->{NAME}; } sub some_class_method { my $class = shift; my $param1 = shift; print "in some_class_method: '$class' ($param1)\n"; $class eq __PACKAGE__ or die "oops: unexpected '$class'"; return "hello from some class method"; } sub some_utility_method { my $param1 = shift; print "in some_utility_method: ($param1)\n"; return "hello from some utility method"; } 1;
And here is a sample program that calls into this package:
use strict; use warnings; use MyPackage; my $obj = MyPackage->new("John Smith"); my $name = $obj->some_instance_method(69); print "name='$name'\n"; my $cm = MyPackage->some_class_method(42); print "cm='$cm'\n"; my $um = MyPackage::some_utility_method(43); print "um='$um'\n";

Running the sample program above produces:

in ctor: 'MyPackage' (name=John Smith) in some_instance_method: MyPackage=HASH(0x1c905b0) '69' name='John Smith' in some_class_method: 'MyPackage' (42) cm='hello from some class method' in some_utility_method: (43) um='hello from some utility method'


In reply to How to deal with old OO code that mixes instance methods with class methods by eyepopslikeamosquito

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.