in reply to Is it possible to call a package::main subroutine from a module?
First, a couple of things: Always use warnings;. Avoid using $_[0] etc. directly unless you know why you need to, instead do sub foo { my ($x,$y,$z) = @_; ... }. Don't turn off strict for the whole module, just do it in as small a scope as possible. (And in fact, in this case, you don't even need to turn it off at all, since you're not accessing subroutines via their name, you're accessing them via a code reference, e.g. \&get_locations.)
In general, you probably should study perlreftut and perlref. The way to call a code reference is $coderef->($arg1, $arg2, ...), where $coderef can also be a complex dereference consisting of hashes, arrays, and calls to other code references.
# ---8<--- ProcessData.pm ---8<--- package ProcessData; use warnings; use strict; use Carp; sub new { my ($class) = @_; my $self = { _dispatch_table => {} }; return bless $self, $class; } sub add_subroutine { @_==3 or croak "Insufficient arguments passed to add_subroutine(). +"; my ($self,$name,$code) = @_; croak "The reference passed to add_subroutine() is not a CODE refe +rence." unless ref $code eq "CODE"; $self->{_dispatch_table}{$name} = $code; } sub process_data { @_==2 or croak "Insufficient arguments passed to process_data()."; my ($self,$name) = @_; $self->{_dispatch_table}{$name}->(); } sub process_locations { my ($self) = @_; $self->process_data("locations"); } 1; # ---8<--- test.pl ---8<--- use warnings; use strict; use ProcessData; my $pd = ProcessData->new(); $pd->add_subroutine("locations", \&get_locations); $pd->process_locations; sub get_locations { print "get_locations...\n" }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is it possible to call a package::main subroutine from a module?
by TorontoJim (Beadle) on Mar 21, 2016 at 23:05 UTC | |
by Anonymous Monk on Mar 21, 2016 at 23:12 UTC | |
by TorontoJim (Beadle) on Mar 22, 2016 at 16:25 UTC |