in reply to RE: Calling a class method name in a scalar using :: syntax
in thread Calling a class method name in a scalar using :: syntax

"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.