Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Thorny design problem

by friedo (Prior)
on Sep 07, 2005 at 21:23 UTC ( [id://490002]=note: print w/replies, xml ) Need Help??


in reply to Thorny design problem

I don't see any particular reason to use eval. Why not do something like this?

sub do_em { ... my $module = 'Crunch::' . $company; require $module; $module->do_it( $dir ); } }

Update: Actually that should be:

my $module = 'Crunch/' . $company . '.pm';

...per mpeters' note below.

There are also some CPAN modules for doing plugins which will scan a directory and load each module fitting some criteria, so you don't even have to do the require.

Replies are listed 'Best First'.
Re^2: Thorny design problem
by mpeters (Chaplain) on Sep 07, 2005 at 21:40 UTC
    No, this won't actually work. From perldoc:
    But if you try this:
    $class = ’Foo::Bar’; require $class; # $class is not a bareword #or require "Foo::Bar"; # not a bareword because of the ""
    The require function will look for the "Foo::Bar" file in the @INC array and will complain about not finding "Foo::Bar" there. In this case you can do:
    eval "require $class";
    But you could do:
    my $module = 'Crunch/' . $company .'.pm'; require $module;

    -- More people are killed every year by pigs than by sharks, which shows you how good we are at evaluating risk. -- Bruce Schneier
Re^2: Thorny design problem
by tlm (Prior) on Sep 07, 2005 at 21:36 UTC

    Thanks for the cluebricks on eval, and on the keyword "plugin", which hadn't occurred to me. Searching for "plugin" in CPAN brings down about 600 hits, most of which are not general enough (e.g. Siesta::Plugin, PXP::Plugin, etc.), so I have some wading to do. If you remember any specific names, please let know.

    Update: Thanks to mpeters for reminding me why I was fussing with eval.

    the lowliest monk

      It's actually not so much a 'plugin' architecture, but a 'factory' design patter. Check out Class::Factory.

      -- More people are killed every year by pigs than by sharks, which shows you how good we are at evaluating risk. -- Bruce Schneier

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-19 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found