Thread PlugIns mit realisieren...: ins Verzeichnis packen und lööpt... (25 answers)
Opened by Magic at 2004-07-03 01:11

Magic
 2004-07-07 15:57
#83858 #83858
User since
2003-09-30
91 Artikel
BenutzerIn
[Homepage] [default_avatar]
so, da bin ich wieder. Hier mal mein momentaner stand. zuerst das aufrufende programm, das in diesem fall name, version und zur verfügung gestellte funktionen ausgibt:

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/perl -w

use lib 'modules';
use plugin;
use strict;

my $pluginobj = plugin->new;

my %plugins = $pluginobj->PluginGetFromDir( 'plugins', '2' );

for (keys %plugins ){
print $plugins{$_}->{name}," ";
print $plugins{$_}->{version},"\n";
my $func = $plugins{$_}->{functions};
for ( @{ $plugins{$_}->{functions} } ){ print $_, ", " }
print "\n \n";
}


die eigentliche API als modul (lib modules). die plugin-api kann nun einfach mit use eingebunden werden. :

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package plugin; 

use Exporter;

@ISA = qw( Exporter );
@EXPORT = qw( &new &PluginGetFunctions &PluginUseFunction );
$VERSION = 0.1;

use strict;

sub new {
my $class = shift;
my $self = {};
bless( $self, $class );
$self->add( @_ ) if ( @_ );
return $self;
}


sub PluginGetFunctions{
my $self = shift;
my $plugins = $_[0];
my @links;

for ( keys %$plugins ){
my $obj = $plugins->{ $_ };

push @links, '?action='
. $_
. ';plugin='
. $obj->{'class'}
for $obj->{'functions'};
}

return @links;
}

sub PluginUseFunction{
my $self = shift;
my $packagename = $_[0];
my $function = $_[1];

my $obj = eval $packagename . '->new';

return eval $obj . '->' . $function;
}

sub PluginGetFromDir{
my $self = shift;
my $directory = $_[0];
my $tiefe = $_[1];

die 'Not enough arguments to get Plugin.' if ! $directory;

my ( %plugins );

opendir( PLUGINS, $directory ) ||
die 'Can&´t open Plugindirectory: ' . $directory . ' -> ' .$!;

for ( readdir( PLUGINS ) ){
next if $_ =~ m|^\.+|;

my $fullpath = $directory . '/' . $_;

if ( -d $fullpath && $tiefe >= 1 ){
my $resttiefe = $tiefe-1;
my ( $pname, $pobj ) = PluginGetFromDir( '', $fullpath, $resttiefe );
$plugins{ $pname } = $pobj if ( $pname && $pobj );
}

next if $_ !~ m|\.pl$|i;

my ( $pname, $pobj ) = PluginValidate( $fullpath, $_ );
$plugins{ $pname } = $pobj if ( $pname && $pobj );


}
closedir PLUGINS;

return ( %plugins );
}

sub PluginValidate{
my $fullpath = $_[0];
my $filename = $_[1];

my $packagename = $filename;
$packagename =~ s|(.*?)\.pl$|$1|i;

require $fullpath;

my $obj = eval $packagename.'->new()';

return undef if ! $obj->{'name'};
return undef if ! $obj->{'version'};
return undef if ! $obj->{'functions'};

return ( $packagename, $obj );
}

1;


und ein einfaches testplugin (order plugins):
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use strict;

package testen;

my $PLUGINNAME = 'testen';
my $VERSION = '0.1';

sub new{
my $class = shift;

my $self = { 'class' => $class,
'name' => $PLUGINNAME,
'version' => $VERSION,
'tmp_folder' => '',
'functions' => [qw(
TestFunk
MehrTestFunk
)]
};

bless( $self, $class );

return $self;
}


# PLUGIN Funktionen >>

sub TestFunk{
return "Huhn ist nicht Hase";
}
sub MehrTestFunk{
return "Huhn ist nicht Hase";
}

1;




die kommentare habe ich einfach mal weggelassen, damit's nicht so ellen lang ist.

verbesserungsvorschläge oder hinweise??

Gruss
Stefan
Ein Weiser gibt nicht die richtigen Antworten, sondern er stellt die richtigen Fragen.

View full thread PlugIns mit realisieren...: ins Verzeichnis packen und lööpt...