I can certainly do that.

The base-class:
Package MyBaseClass; sub new { my $class = shift; my $self = {}; bless($self,$class); $self->_init(); return $self; } sub _init { my $self = shift; @{$self->{things}} = qw(thing_0.1 thing_0.2 thing_0.3 thing_11.0 thi +ng_11.1); # etc,etc return $self; } # $a and $b don't seem to exist within this context sub specialSort { my $self = shift; my @aParts = split(/[_.]/, $a); my @bParts = split(/[_.]/, $b); return 1 if (int $a_parts[1] > int $b_parts[1] || ($a_parts[1] eq $b +_parts[1] && (int $a_parts[2]) > (int $b_parts[2]))); return -1 if (int $a_parts[1] < int $b_parts[1] || ($a_parts[1] eq $ +b_parts[1] && (int $a_parts[2]) < (int $b_parts[2]))); return 0 if ($a eq $b); die((caller(0))[3] . " LOGIC error."); } 1;
The subclass:
package MySubclass; require MyBaseClass; our @ISA = qw(MyBaseClass); sub new { my $class = shift; my $self = {}; bless($self,$class); $self->_init(); return $self; } sub _init { my $self = shift; MyBaseClass::_init($self); return $self; } sub doSomething { my $self = shift; # this works foreach my $thing(sort specialSort @{$self->{things}}) { # do something with $thing... } # this doesn't work foreach my $thing(sort $self->specialSort @{$self->{things}}) { # do something with $thing... } } sub specialSort { # Notice that there is no 'my $self = shift' here... my @aParts = split(/[_.]/, $a); my @bParts = split(/[_.]/, $b); return 1 if (int $a_parts[1] > int $b_parts[1] || ($a_parts[1] eq $b +_parts[1] && (int $a_parts[2]) > (int $b_parts[2]))); return -1 if (int $a_parts[1] < int $b_parts[1] || ($a_parts[1] eq $ +b_parts[1] && (int $a_parts[2]) < (int $b_parts[2]))); return 0 if ($a eq $b); die((caller(0))[3] . " LOGIC error."); } 1;

In reply to Re^2: Inherit custom sort method from base class by adaughterson
in thread Inherit custom sort method from base class by adaughterson

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.