Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: Coding perl a plugin system?

by tachyon (Chancellor)
on Dec 16, 2002 at 00:08 UTC ( [id://220083]=note: print w/replies, xml ) Need Help??


in reply to Re: Coding perl a plugin system?
in thread Coding perl a plugin system?

If you want to do stuff C++ OO style in perl here is one way to do it. The basic API spec is as follows:

A plugin must supply three methods, namely func1(), func2() and func3(). The plugin code is expected to be OO as shown in the sample code and nothing should be exported from it. These methods do foo bar and baz (ie take the following args, set whatever widgets, return whatever) To enable the plugin methods to do foo bar and baz access to the configuration variables $var1, $var2, $var3 is provided via get and set methods. The MP3 package also provides the following utility methods blah....

With the vars in the MP3 package the closures mean using the set and get methods is the only way to set/get these vars. You can thus impose whatever level of sanity checking you like on what happens to them (from your code or the Plugin).

By making the plugin OO you don't have to worry about namespace polution and you can pass initializing config data to it if required

#!/usr/bin/perl -w package MP3; use Plugin; use strict; # get a plugin object so we can access its methods # we could pass config details to the plugin object # here if we want..... my $p = new Plugin( config_var => 'some config var' ); # use closures to make truly private lexical vars that # can only be accessed by the get and set methods { my $var1; sub set_var1 { $var1 = $_[0] } sub get_var1 { return $var1 } } { my $var2; sub set_var2 { $var2 = $_[0] } sub get_var2 { return $var2 } } { my $var3; sub set_var3 { $var3 = $_[0] } sub get_var3 { return $var3 } } sub say_gday { print "G'day World!\n" } set_var1(123456); print 'Get \$var1 ', get_var1(), "\n"; print "\$var1 is very private '$var1'\n"; # now call some of the Plugin functions.... $p->func1(); # sets $var1 to 123 and prints it... $p->func2(); # calls a function in MP3 package print $p->func3(); # return a config var package Plugin; sub new { my ($class, @init) = @_; return bless {@init}, $class; } sub func1 { MP3::set_var1(123); print MP3::get_var1(), "\n"; } sub func2 { MP3::say_gday(); } sub func3 { my $self = shift; return $self->{config_var}; } 1;

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-24 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found