Consider the following code using "classic" Perl 5 Objects, as shown in perlboot.
use 5.10.1; use strict; use warnings; { package Base; sub new { my $class= shift; return bless ({}, $class); } sub baz { my ($self,$x)= @_; say "Baz called: " . $self . " ($x)"; } } # end of class { package Derived; our @ISA = 'Base'; } # end of class say "I'm running."; my $d= Derived->new; $d->baz(1); Base->baz(2); Derived->baz(3);
As I've come to expect, the arrow syntax on class names will find inherited functions. The function defined in the base class is found and passed the derived class name as the first argument.

However, trying some things with Moose:

use 5.10.1; use MooseX::Declare; class Base { sub baz { my ($self,$x)= @_; say "Baz called: " . $self . " ($x)"; } } #end of Base class Derived { method foo (Num $x) { say "Foo called: " . ref($self) . " ($x)"; } sub bar { my ($self,$x)= @_; say "Bar called: " . ref($self) . " ($x)"; } } #end of Derived say "I'm running."; my $d= Derived->new; $d->foo (1); #Derived->foo(2); #cannot call as static # this one is allowed. $d->bar (1); Derived->bar(2); $d->baz(1); Base->baz(2); Derived->baz(3); # does not find it!
In particular, the last line, Derived->baz(3), shows that it will not find inherited subs called on the Class (not an instance).

Why not?

And how can it it not? If it uses the normal Perl mechanism to locate subs on instances, why doesn't it work on class names too?

Confused as ever,
—John


In reply to Moose and class methods by John M. Dlugosz

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.