Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Dynamic Tk buttons from init file

by rjray (Chaplain)
on Mar 19, 2003 at 23:42 UTC ( [id://244491]=note: print w/replies, xml ) Need Help??


in reply to Dynamic Tk buttons from init file

Probably the cleanest way of doing this would be to have a more formalized plug-in framework, and the PlugIn.pm file would have not just the new subroutines, but also calls into a "registry" of some sort, to make their presence known.

There are a lot of ways of doing plug-ins, and how you choose to do them is mainly a function of how quickly you want results versus how elegant and maintainable you want the end product to be.

Here's one suggestion: Have each plug-in use its own file, and mandate that each declare a package name, using a common prefix (such as Plugin:: and the file name, just like a "normal" Perl module. We'll call the example one "Print", so the plug-in is in Print.pm and it declares "package Plugin::Print;" near the top. Additionally, require that the plug-in have a common entry-point routine, much like Apache location handlers default to the name "handler". Call this one "run", for example. Now, we have a file with a known package name (it can be derived from the file name) and a known subroutine entry-point (Plugin::Print::run). Your configuration script now needs only to point to the Print.pm file, and you know that (a) you have to load it in, and (b) the button you create should have the "-command" option point to \&Plugin::Print::run.

This is one way. It has benefits and drawbacks. I encourage you to look at other solutions, as well.

--rjray

Replies are listed 'Best First'.
Re: Re: Dynamic Tk buttons from init file
by TunesMan (Novice) on Mar 20, 2003 at 00:33 UTC
    Thanks rjray. Good idea. I think I'll steal it!

    For some reason I could not get the Plugin::Print::run to work.

    Test file "foo.pl"

    #!/usr/local/bin/perl -w Plugin::Print::run;
    Then in the Plugin directory I have Print.pm
    package Plugin::Print; sub run { print "Got print run!\n" } 1;
    When I run this I get Undefined subroutine &Plugin::Print::run called at foo.pl line 3.

    I expermented some and found that I could do this Test file "foo.pl"

    #!/usr/local/bin/perl -w use Plugin; Plugin::Print::run; Plugin::Edit::run;
    Then in Plugin.pm in same directory
    package Plugin::Print; sub run { print "Got print run!\n" } package Plugin::Edit; sub run { print "Got print run!\n" } 1;
    This works well enough but I'm curious why the first one didn't work?

    TunesMan

      For one thing, your first example doesn't seem to "use" the module before trying to call the routine.

      Another thing to watch for, is that if you plan on saying "use Plugin::Print", then Print.pm will have to be in a directory called "Plugin", and that directory in your search path. But there are other ways of reading in the code, besides "use" and "require".

      --rjray

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-26 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found