"Methods" normally act on an object, which passes the object reference as the first argument. Methods can also act on packages (using a similar notation), passing the name of the package as the first argument. The key difference is in how you want to interpret the subroutine. Do you need it to be a simple subroutine (without care as to the object/package context), or do you need it to be a method, with knowledge of the object or package? If you need it to be a simple subroutine, write it as such and either export it, or call it as A::B::sub($arg1, $arg2). If you need it to be a method, that's when you use the arrow notation.
package A::B; sub new { my $caller = shift; my $self = {}; bless $self, ref($caller) || $caller; } sub method { my $self = shift; my @args = @_; print "self=$self, args=@args\n"; } sub function { my @args = @_; print "args=@args\n"; } package main; $self = new A::B; # A::B::new as a package method, # first arg = "A::B"; $self = A::B->new; # A::B::new as a package method, # first arg = "A::B"; $self = $self->new; # A::B::new as an *object* method, # first arg = $self, ref($self) = "A::B" &A::B::function("some", "args"); # don't care about $self or A::B $self->method("some", "args"); # Need to know $self &A::B::method($self, "some", "args"); # Same results, weird methodolog +y A::B->method("some", "args"); # Need to know name of package
Typically package methods are only useful when creating a new object that might rely on inheritance. If you create a descendent object, and simply want to inherit the parent's "new" constructor, simply using bless without a second argument will cause the new object to be blessed into the parent class. Providing that first argument to the 'new' method and passing it as the second argument to bless causes it to be blessed into the class that your code originally used. Similarly, using ref as above allows you to create new objects via a pre-existing object ($self->new).

Other than that, the only reason you might want to use a package method is for readability.


In reply to RE: RE: Calling a class method name in a scalar using :: syntax by Fastolfe
in thread Calling a class method name in a scalar using :: syntax by metaperl

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.