sman has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone, A simple question again. I am wondering we can use the methods provided by Catalyst::Log via $c->log(...) in our modules due to the following line defined in Catalyst.pm.

use Catalyst::Log;

Does that mean log() become a method of Catalyst context and that's what we call inheritance in Perl?

Replies are listed 'Best First'.
Re: Inheritance in Perl
by Your Mother (Archbishop) on Jan 19, 2010 at 00:22 UTC

    Not really. log is a method of the context/app object but you don't call its methods on the context object, right. You call them on the log object you access through the context object. E.g.-

    $c->log->warn("oh hai!!!");

    You're not calling $c->warn, you're doing the equivalent of-

    my $logger = $c->log; $logger->warn("oh hai!!!");

    So, there is no inheritance. There are two objects. One contains the other (and in cases where they both contain each other you'll likely need Scalar::Util::weaken).

Re: Inheritance in Perl
by biohisham (Priest) on Jan 19, 2010 at 00:31 UTC
    the "use" let you import into the current namespace some the semantics that are in the module you provide to it, these semantics include the methods and variables that would be available to the current namespace ..

    Inheritance on the other hand comes into play when you are writing a class (derived) that would use methods from another class (base), so the derived class inherits from the base class, this would be provided in the @ISA of the derived class, so when Perl can not find a method or a class in your current package it would check the @ISA for a class that has that method and subsequently execute it..

    "Does that mean log() become a method of Catalyst context and that's what we call inheritance in Perl?"

    It is a method invoked by the object used in your program, which is an object of Catalyst but it doesn't necessarily need to be inherited by Catalyst or taken as a representer of what inheritance is unless Catalyst itself has inherited this particular method from another base class, check the documentation..

    An example of Inheritance, I would have two classes, a base CLASS1.pm and a derived CLASS2.pm, the second class would inherit the method declared in the first one and hence calling the second class and using it would call that method from CLASS1 but as a method of CLASS2..

    The above code is just a proof of principle that illustrates what inheritance is, I hope you find it instructive and criticism is accepted with an open heart...but the subject is a vast one...You might also be interested in checking:


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
      Thanks a lot for such insightful comments.
Re: Inheritance in Perl
by Anonymous Monk on Jan 19, 2010 at 00:17 UTC
    Not unless Catalyst::Log exports that function into your namespace.

    In essence, if you declare that your package is Catalyst::Log, eg our @ISA = qw(Catalyst::Log); or use parent qw(Catalyst::Log) or use base qw(Catalyst::Log), then all methods of that class will exist in instances of your class.

    Be careful if autloading with multiple inheritance, because it's not as straightforward as it may seem.

    See perlboot and perltoot.