Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Same function multiples unknown modules

by Zaxo (Archbishop)
on Sep 19, 2005 at 13:19 UTC ( #493154=note: print w/ replies, xml ) Need Help??


in reply to Same function multiples unknown modules

The usual way to have several implementations of a function is to place them in a package namespace. If the modules furnish an object constructor, calling the runonce() method will automatically select the correct one.

To call a non-method runonce() from a package, use the fully-qualified name,

Package::Name::runonce();
To call once for each package loaded, you'll need to go through all the loaded packages and try each. Here's an unsophisticated way to do it,
for (keys %INC) { s!/!::!g; s/\.pm$//; eval { $_::runonce() }; }
The eval is used to trap fatal errors from packages which don't define runonce().

After Compline,
Zaxo


Comment on Re: Same function multiples unknown modules
Select or Download Code
Re^2: Same function multiples unknown modules
by Delusional (Beadle) on Sep 19, 2005 at 13:59 UTC
    Thanks. This looks promising. At the moment I have two subdirectories where these .pm files are: Main and Addins. What would need to be changed to use only these two subdirectories. Not the root driectory where the main script is located, or the perl root, only the two subdirectories?

      At the top of the loop, the line,

      next unless m/^Main|Addins/;
      will restrict attention to those two top level namespaces.

      After Compline,
      Zaxo

        Thanks again Zaxo,
        I ran a test using this, however, the $eval fails on me (in a browser I get Error 500)...

        I use logging in the scripting to catch errors as well as MySQL calls (and anything else I choose to log). The log shows the following when the script is executed:

        Sep 26, 2005 (10:49:52) : ERROR SITUATION: Interface Crash, error message:
        syntax error at Main/Menu.pm line 101, near "$_::runonce(" Compilation failed in require at d:\PROGRA~1\APACHE~1\apache\cgi-bin\intern2.cgi line 31.

        In the file Main/Menu.pm I have your code as:
        sub sub_menus { for (keys %INC) { next unless m/^Main|Addins/; s!/!::!g; s/\.pm$//; #print "<font face=\"Arial\" size=\"2\">$_<br></font>\n"; eval { $_::runonce() }; } }
        The function, runonce (sub runonce), is available in exactly one file. Perhaps I shouldn't use sub, rather someother addressing scheme that I'm unawear of?

        If I comment out the eval line and use the print line, execution runs clean (with the exception of runonce not running).

        I assume the problem lye with the logging I have to catch problems and log them:
        $SIG{__DIE__} = \&error_crash_trapper; $SIG{__WARN__} = \&warn_crash_trapper;
        Is there any other way to achieve this? Essentially I'm looking for simplicity and the use of a 'dummy' routine that doesn't rely on hard coded information which then allows for dynamic implementation.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-12-18 02:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls