4

I am looking at building a dispatch table for calling a number of Perl modules that I wrote.

As an example, if I have a package called Hello.pm with a simple function hello() in it, I would like to get a code reference to this function.

The following does not work:

my $code_ref=\&Hello->hello();
$code_ref->();

But if the function hello is exported from the package, then the following works:

my code_ref=\&hello;
code_ref->();

Does anyone know the correct syntax for the first case? Or is this simply not possible?

In the end, I would like to populate a hash table with all my code references.

##### Thanks for All Answers

The correct invocation as pointed out by several answers is:

my $code_ref=\&Hello::hello;
$code_ref->();

I have some 10 modules in 10 different files that I would like to load in a dispatch table. This makes it easier for me to have the configuration loaded as data, and separate from code. This allows me to load additional modules in a testbench without modifying my code-simply modify the configuration file. Mark Dominus, author of Higher Order Perl, has some nice examples on this.

1
  • Thanks for pointing this out. I finally figured out how to accept.
    – sammy
    Dec 29, 2012 at 20:14

3 Answers 3

9

If you want to refer to the hello sub in the Hello module, then call it, use:

my $code_ref = \&Hello::hello;
$code_ref->();

If you want to call a method named "hello" in Hello, you can do it like this:

my $method = "hello";
Hello->$method();
5
  • The :: did not work. After all, this is equivalent to ->. I am just looking at understanding how to establish a reference to a function in a module when there is no export of the function name.
    – sammy
    Dec 29, 2012 at 4:21
  • The -> in Foo->bar()' is equivalent to Foo::bar('Foo'). THe ->` means that whatever is in front of it (like an object or a class name) will be passed to the sub that is being called as the first argument. With Foo::bar(), that is not the case.
    – simbabque
    Dec 29, 2012 at 10:29
  • $code_ref is a reference to a sub, so the -> here is dereferencing it. It's not the same as the arrow used for method calls. So $code_ref->() is the same as &{$code_ref}(), where the & is being used to dereference it. Dec 29, 2012 at 16:03
  • Okay! I see my typo. I had the () after hello. The parenthesis should not have been there as parenthesis here imply a function call.
    – sammy
    Dec 29, 2012 at 19:30
  • And I cannot use -> as this is a method invocation as pointed. The :: defines the scope and this is the correct way.
    – sammy
    Dec 29, 2012 at 19:45
7

\&NAME takes a reference to a sub. Hello->hello() is not a sub name. As an expression, it would be a method call.

To get a reference to a method, use can.

my $method_ref = Hello->can('hello');

That will search the inheritance tree if necessary. Now that you have a reference to the right method, you can call it:

Hello->$method_ref()
  -or-
$method_ref->('Hello')

If you need a callback that can't call the method properly, you'll need to create a callback that does.

my $code_ref = sub { Hello->hello(@_) };

Here's what it looks like fully dynamic:

my $pkg         = 'Hello';  # Also works with object!
my $method_name = 'hello';
my $method_ref  = $pkg->can($method_name);

my $callback = sub { $pkg->$method_ref(@_) };
3

What you probably want is

my $code_ref = \&hello;
$code_ref->();
2
  • 3
    Other than the missing dollar signs (which must be typos, since the code would not even compile without them), this is exactly what the OP already has. Dec 29, 2012 at 2:26
  • There are no dollar signs needed here. We are simply establishing a reference to a function that was exported by the Hello Module.The above code works. My simple question question is why cannot I establish a reference to Hello->hello() directory.
    – sammy
    Dec 29, 2012 at 4:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.