in reply to Re: Inherit custom sort method from base class
in thread Inherit custom sort method from base class
I can certainly do that.
The base-class:The subclass: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;
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Inherit custom sort method from base class
by vsespb (Chaplain) on Oct 01, 2013 at 22:50 UTC | |
by AnomalousMonk (Archbishop) on Oct 02, 2013 at 00:30 UTC | |
by adaughterson (Initiate) on Oct 01, 2013 at 23:35 UTC | |
by vsespb (Chaplain) on Oct 01, 2013 at 23:54 UTC | |
by AnomalousMonk (Archbishop) on Oct 02, 2013 at 00:11 UTC | |
by Anonymous Monk on Oct 02, 2013 at 10:08 UTC | |
|