[tpm] Understanding 'use base ...'

Uri Guttman uri at stemsystems.com
Mon Jul 14 15:34:49 PDT 2008


>>>>> "SF" == Shaun Fryer <sfryer at sourcery.ca> writes:

  SF> I see you already answered your own question, but I'd like to add
  SF> something which may not be documented (haven't checked). Unlike
  SF> @ISA, "use base" does not respect order of inheritance. While
  SF> generally multiple inheritance is a bad idea, it's difficult to
  SF> get around at times due to the widespread use in many common Perl
  SF> modules of "implementation inheritance", versus the much better
  SF> and more flexible "interface inheritance" pattern. So while, @ISA
  SF> stores the inheritance chain in a fixed "ordered list", "use base"
  SF> does not respect the order of inheritance, which can pose problems
  SF> and produce many intermittent so called "heisenbugs".

that sounds wrong to me. use base just sets @ISA for you. and it will do
so in the order you put the use base lines. i am not sure if it does
unshift or push onto @ISA but that is easily determined. why do you
think that use base does something other than mung @ISA? @ISA is the
only way perl inheritance works so it has to be munged by use base.

and that is trivial to show by running a couple of use base calls.

perl -le 'use base "LWP::Simple" ; use base "Scalar::Util"; print "@ISA\n" '
LWP::Simple Scalar::Util

perl -le 'use base "Scalar::Util"; use base "LWP::Simple" ; print "@ISA\n" '
Scalar::Util LWP::Simple

so it looks like the first use base is higher priority in @ISA which
means it does a push.

uri

-- 
Uri Guttman  ------  uri at stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


More information about the toronto-pm mailing list