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

you can, try this

package A::B; sub meth { warn "meth $_[0]", "\n"; } my $meth='meth'; A::B->$meth(25); # succeeds...not really what you are likely expecting &$meth(26); # succeeds in calling my $foo="A::B::meth"; &$foo(27); # also succeeds

outputs

meth A::B meth 26 meth 27

I'd rather leave it up to more elequent monks seeking high xp to explain this one. (better to remain silent and be thought an idiot that to speak and remove all doubt) :-)

Replies are listed 'Best First'.
RE: RE: Calling a class method name in a scalar using :: syntax
by Fastolfe (Vicar) on Nov 15, 2000 at 20:39 UTC
    "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.

RE: RE: Calling a class method name in a scalar using :: syntax
by japhy (Canon) on Nov 15, 2000 at 20:19 UTC
    It works because it's a soft reference. You shouldn't use soft references except in ONE specific application (importing symbols from one package to another.

    $_="goto+F.print+chop;\n=yhpaj";F1:eval