Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Dispatcher to module's method

by AnomalousMonk (Archbishop)
on Dec 14, 2013 at 04:41 UTC ( [id://1067107]=note: print w/replies, xml ) Need Help??


in reply to Dispatcher to module's method

Every subroutine is defined in the symbol table (or namespace) of a package, the default package being main. A subroutine can always be invoked or referenced using a fully-qualified subroutine name that includes the package. The following example does not cover the complete process of defining a standalone module, which you seem to understand, but should be enough to illustrate the principle of using a fully-qualified subroutine name. See perlmod.

>perl -wMstrict -le "package Module1; ;; sub main { print qq{hi from main($_[0]) in package }, __PACKAGE__; } ;; package Module2; ;; sub main { print qq{hi from main($_[0]) in package }, __PACKAGE__; } ;; package main; ;; my %pages = ( PG_1 => \&Module1::main, PG_2 => \&Module2::main, ); ;; my $pg = 'PG_2'; $pages{$pg}->(42); " hi from main(42) in package Module2

Update: Note that it's usually a Bad Idea to define a function with the same name as a very important global namespace, i.e., main. It is quite possible to keep everything straight, but you risk giving yourself a terrible headache.

Replies are listed 'Best First'.
Re^2: Dispatcher to module's method
by Anonymous Monk on Dec 14, 2013 at 07:06 UTC

    Many thanks for your excellent reply!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-29 13:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found