Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: Inherit custom sort method from base class

by vsespb (Chaplain)
on Oct 01, 2013 at 22:50 UTC ( #1056550=note: print w/replies, xml ) Need Help??


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

Try
sort { $self->specialSort($a, $b) } @{$self->{things}})
and remove specialSort from MySubclass.
you have to use 1st and 2nd arguments instead of $a and $b in base class' specialSort

You cannot use $self->specialSort as subroutine name, so you have to use new "subroutine":
{ $self->specialSort($a, $b) }

Also, technically, you can put
use base qw/Exporter/; @EXPORT = qw/specialSort/;

into base class,
then remove specialSort from MySubclass, and then it will work.
but it's probably bad style to mix inheritance and exported functions.

UPD: fixed $a,$b in code

Replies are listed 'Best First'.
Re^4: Inherit custom sort method from base class
by AnomalousMonk (Canon) on Oct 02, 2013 at 00:30 UTC

    Here's another, slightly different approach to what vsespb is talking about. (But I still don't understand how
        $self->specialSort(my ($a, $b))
    works!)

    >perl -wMstrict -le "{ package Base; ;; sub new { my $class = shift; return bless({} => $class)->init(@_); } ;; sub init { my $self = shift; @{ $self->{things} } = (qw(a E d Bee Z cee d C zee E eks), @_); return $self; } ;; sub print { my $self = shift; CORE::print qq{@{ $self->{things}} }; } ;; sub nocase_descending { my $class_obj = shift; return lc($_[1]) cmp lc($_[0]); } ;; } ;; ;; { package Derived; use parent -norequire, qw(Base); ;; sub sort { my $self = shift; return CORE::sort { $self->nocase_descending($a, $b) } @{ $self->{things} } ; } ;; } ;; ;; my $d = Derived->new(qw(SS r QQ p PP q RR s)); ;; $d->print; ;; my @sorted = $d->sort; print qq{@sorted}; ;; @sorted = sort @{ $d->{things} }; print qq{@sorted}; ;; print $d; " a E d Bee Z cee d C zee E eks SS r QQ p PP q RR s zee Z SS s RR r QQ q PP p eks E E d d cee C Bee a Bee C E E PP QQ RR SS Z a cee d d eks p q r s zee Derived=HASH(0x1e931f4)

    Update: Moved  print method from  Derived class 'up' into  Base class (just because I can).

Re^4: Inherit custom sort method from base class
by adaughterson (Initiate) on Oct 01, 2013 at 23:35 UTC

    That worked great! Thanks for the assist :)

    sort { $self->specialSort(my ($a,$b)) } @{$self->{things}} # ...my, because use strict is in play
      Hm. Strange. It should work without "my", and should not work with "my".
        It should work without "my", and should not work with "my".

        Ditto that.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1056550]
help
Chatterbox?
[1nickt]: jdporter Maybe try IPC::Run3
[LanX]: Sandy Koufax is indeed rude. .. oO
[chacham]: Re: Re: pattern matching -- feeling stoopit a poll idea about centrists, is +8 -5, used to be +6/-6 how apropos :)is +8
[chacham]: ...errr... Re: poll ideas quest 2015 (political centrist)
[chacham]: ooh, late, nighty nite everybody

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (14)
As of 2017-05-24 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?