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

package A::B; sub meth { warn "meth $_[0]"; } my $meth='meth'; A::B->$meth(25); # succeeds... A::B::$meth(25); # fails...

I know that $_[0] contains different things due to the use of the arrow over colons. But I would like to know why I cannot call a class subroutine whose name is specified via a scalar.

Replies are listed 'Best First'.
RE: Calling a class method name in a scalar using :: syntax
by japhy (Canon) on Nov 15, 2000 at 20:16 UTC
    Perl allows you to call a method on an object or class by way of indirection -- storing the method name in a scalar:
    my $foo = Obj->new; for (qw( create init add )) { $foo->$_(); # note -- $foo->$_ will not work, ()'s needed }
    However, this is very different from a qualified name. A qualified name is basically "This::That::Those". They can only contain valid identifiers, which are strings that match /\A(?:[a-zA-Z_]\w*|\d*)\z/ (yes, you can have an identifier of 0 characters).

    To get the behavior you'd like, I suggest using the symbol table:
    my $meth = 'meth'; $A::B::{$meth}->(25); # %A::B:: is a symbol table # $A::B::{$meth} holds the typeglob *A::B::meth # *A::B::meth->(...) is like A::B::meth(...)

    Aside: here's an example of 0-length identifiers that you can use under strict (because they're qualified):
    #!/usr/bin/perl use strict; @:: = qw( this program uses empty variable names ); for $:: (@::) { print $::; } # ::; looks confusing ;) open ::, ".cshrc"; print scalar <::>; # is there no end to this madness? close ::;
    Just avoid screwing with %::, since that's a symbol table.

    $_="goto+F.print+chop;\n=yhpaj";F1:eval
RE: Calling a class method name in a scalar using :: syntax
by wardk (Deacon) on Nov 15, 2000 at 20:16 UTC

    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) :-)

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

      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
RE: Calling a class method name in a scalar using :: syntax
by merlyn (Sage) on Nov 15, 2000 at 21:36 UTC
Re: Calling a class method name in a scalar using :: syntax
by rufus (Initiate) on Sep 04, 2003 at 14:29 UTC

    Hello,

    I have a similar problem. I want to call a class method of a "dynamic" class "dynamically", i.e. I want to make a call like

    my $result = MODULES::$submodule::$method ($data);

    I tried different things like

    my $request = "MODULES::$submodule::$method"; my $result = &$request ($data);

    or

    my $result = eval "$request ($data)";

    but I didn't succeed. The even worst thing is that the program keeps on running without any error, only the method trying to call another method seems to "die" without any hint.
    Actually this problem is common to my current project. Does anybody have a clue about that?

    Thanks.

    Marco

      If it's a static method then you could do this
      my $result = "MODULES::$submodule"->can($method)->($data);
      This works because can is returning a reference to the subroutine in MODULES::$submodule and then that it is being called with $data being passed in.

      Or, simpler still, if it's a class method then you don't need any trickery

      my $result = "MODULES::$submodule"->$method($data);
      That just uses the string "MODULES::$submodule" as the class and then calls the method as designated by $method.
      HTH

      _________
      broquaint

        Excellent, thank you.

        One of my mistakes was to put the method name between the quotation marks like

        my $result = "MODULES::$submodule->$method($data)"