lilphil has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks
In simple terms I'm trying to think of a way to stop required files from being able to access various procedures, and from stomping on my namespace.
Let me explain further.

I'm trying to create a program which has the ability to use perl plugins, I've trawled the monastery and found some posts about the issue, but I couldnt see anything that addresses this particular angle. The main problem is I want the plugins to be as simple, and as transparent to the writer as possible. Currently a directory of plugins is opened and "required". I've just started learning about using oo in perl, and moved the plugin handling to a module, which presented the problem that plugins had to use main::registerplugin() rather than registerplugin(). Ill post some code to show a dirty hack that gets round the problem.

#### test.pl #!/usr/bin/perl -w use strict; use plugins; my $P = new plugins(); $P->loadplugins(); sub registerplugin { $P->registerplugin(@_); } #### end #### plugins.pm package plugins; use wrap; sub new { my $class = shift; my $self = {}; bless($self, $class); return $self; } sub loadplugins { my $self=shift; wrap::requirer('foo.pl'); } sub registerplugin { my $self = shift; my $arg = shift; print $arg; } 1; #### end #### wrap.pm package wrap; sub requirer { require 'foo.pl'; } sub registerplugin { main::registerplugin(@_); } 1; #### end #### foo.pl registerplugin("hi"); #### end

as you can see this is clearly ludicrous, it helps because when the plugin calls a function it will be called in the wrapper, and the only other thing to worry about is "requrer" (i dont mind that). But its daft. There must be a better way?
Perhaps a complete rethink is in order...

janitored by ybiC: Balanced <readmore> tags around long code block

Replies are listed 'Best First'.
Re: quarantining required files
by gmpassos (Priest) on Jan 24, 2004 at 20:05 UTC
    If you want to isolate the plug-in code from your main program, you can take a look at Safe::World and Safe.

    Graciliano M. P.
    "Creativity is the expression of the liberty".

      Thankyou monks, I will experiment with both methods and see what fits best.
      Some time in the (distant) future I think I would like to rewrite the main code as C, but keep the plugins as they are, so perhaps Safe is ideal.
      *goes to check CPAN*
Re: quarantining required files
by Anonymous Monk on Jan 24, 2004 at 19:40 UTC
    1. get rid of warp.pm (useless).
    2. use namespaces (use OO)
    3. define a plugin base class (all plugins must be subclass )
    4. dance
Re: quarantining required files
by theguvnor (Chaplain) on Jan 25, 2004 at 15:04 UTC

    What's so bad about main::registerplugin()? Personally I think you should just "go with it". It is cleaner than far by the convoluted chain of calls that you've created here with this wrap.pm.

    [Jon]

      Yes, i agree its cleaner - and wouldnt have used the wrap in the real situation. It was an attempt at seeing if it would be done, and then used as a way to explain what I was trying to do here.

      Ive discovered Safe is a very pretty module and ended up with something as simple as:
      my $cpt = new Safe; $cpt->share_from('main',[ '&registerplugin' ]); $cpt->rdo('filename');
      and you can still call code from inside the cpt when it passes code refs through registerplugin.
      not sure if I should be deleting the cpt, as its in a foreach reading in files. but as the foreach creates a codeblock i think perl would clean up for me