Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How can I call a Perl sub with an object from a sub name stored in a var?

by perlfan (Vicar)
on Dec 11, 2020 at 19:20 UTC ( #11125028=note: print w/replies, xml ) Need Help??


in reply to How can I call a Perl sub with an object from a sub name stored in a var?

I think this is backwards. If you're thinking of $myargs to literally unpack to conform to caller semantics, then I'd lose that notion. Just unpack the contents of $_[1] as needed in the resolved subroutine that ends up being called.
use strict; use warnings; package Foo; sub new { my $pkg = shift; my $self = {}; return $self, $pkg; } sub dispatch { my ($self, $mysub, $myargs) = @_; $self->$mysub($myargs); return; } sub mysub1 { my ($self, $args) = @_; foreach my $arg (split /,/, $args) { print qq{$arg\n}; } } package main; my $foo = Foo->new; $foo->dispatch('mysub1', '1,2,3');

Output

prompt$ perl test.pl 1 2 3

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11125028]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2021-04-20 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?