in reply to Using eval for

Well, you could put it in its own package and then clear out the package....

for my $plugin (...) { eval 'package Plugin; do $plugin'; # check for $@ here eval { no strict refs; &{'Plugin::func'}() } if ($@) { ...error.handling... } %Plugin:: = () }

The soft reference is required to defeat the compiler's attempt to hold a pointer to *Plugin::func.

    -- Chip Salzenberg, Free-Floating Agent of Chaos

Replies are listed 'Best First'.
Re: Re: Using eval for
by SuperCruncher (Pilgrim) on Dec 28, 2001 at 04:13 UTC
    Thanks for your reply. An eval inside an eval... strange. Let me just make sure I've got my head round that code:

    eval 'package Plugin; do $plugin'; - this creates a package which the 'plugin' is evaled into.

    eval { no strict refs; &{'Plugin::func'}() } - disables strict refs for the enclosing' block', then calls func in the Plugin package?

    %Plugin:: = () - I've never seen that before. I didn't know a package could be viewed in a sort of 'hash context'. So that sort of 'resets' the package to undef?

    BTW, the title of the node should have been "Using eval for 'plugins'" but Everything seems to have removed it in the preview stage (maybe because it didn't escape single quotes properly?).

      Right on all three counts. Good analysis.

      WRT the eval inside the eval: If you don't like that, you can read the file contents and then:

      eval 'package Plugin;' . $file_contents

      I just did what I did to save the trouble of open/read/close. On the other hand, the eval 'do' will hide your lexical vars from the plugin while the eval above will reveal them. Bug or feature? YOU be the judge.

      WRT the packages as hashes: Each element of %pkgname:: is a GLOB. You can do your own glob aliasing that way, and lots of other evil stuff. But beware the bandersnatch, er, beware the compiler's tendency to grab references to GLOBs and not let go even when they've been removed from their original places.

          -- Chip Salzenberg, Free-Floating Agent of Chaos