Skip Menu |

This queue is for tickets about the Module-Pluggable CPAN distribution.

Report information
The Basics
Id: 13882
Status: resolved
Priority: 0/
Queue: Module-Pluggable

People
Owner: simonw [...] cpan.org
Requestors: claco [...] chrislaco.com
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 2.9
Fixed in: (no value)



Subject: only/except subs
I recently had the need to set only/except programatically after use-ing Module::Pluggable. This quick patch seemed to do the trick. I haven't had time to patch the *only.t/*except.t files yet.
--- Pluggable.pm.orig Sat Jul 09 08:20:38 2005 +++ Pluggable.pm Tue Jul 26 20:40:17 2005 @@ -417,11 +417,28 @@ return $opts{'search_path'}; }; + my $onlysub = sub { + my ($self, $only) = @_; + + if (defined $only) { + $opts{'only'} = $only; + }; + }; + + my $exceptsub = sub { + my ($self, $except) = @_; + + if (defined $except) { + $opts{'except'} = $except; + }; + }; no strict 'refs'; no warnings 'redefine'; *{"$pkg\::$sub"} = $subroutine; *{"$pkg\::search_path"} = $searchsub; + *{"$pkg\::only"} = $onlysub; + *{"$pkg\::except"} = $exceptsub; }
Date: Tue, 26 Jul 2005 21:29:18 -0400
From: "Christopher H. Laco" <claco [...] chrislaco.com>
To: bug-Module-Pluggable [...] rt.cpan.org
Subject: Re: [cpan #13882] AutoReply: only/except subs
RT-Send-Cc:
Download smime.p7s
application/x-pkcs7-signature 3.1k

Message body not shown because it is not plain text.

Just to say it works, here are the modified test files. For the sake of not having to rename all of the lexicals to make sense, I just wrapped them up in closures and duplicated the tests again using the extra sub... I imagine you'll want to rename things to suit your preferences and such. -=Chris
#!perl-wT use strict; use lib 't/lib'; use Test::More tests => 10; { my $foo; ok($foo = MyTest->new()); my @plugins; my @expected = qw(MyTest::Plugin::Bar MyTest::Plugin::Quux::Foo); ok(@plugins = sort $foo->plugins); is_deeply(\@plugins, \@expected); @plugins = (); ok(@plugins = sort MyTest->plugins); is_deeply(\@plugins, \@expected); } { my $foo; ok($foo = MyTestSub->new()); my @plugins; my @expected = qw(MyTest::Plugin::Bar MyTest::Plugin::Quux::Foo); ok(@plugins = sort $foo->plugins); is_deeply(\@plugins, \@expected); @plugins = (); ok(@plugins = sort MyTestSub->plugins); is_deeply(\@plugins, \@expected); } package MyTest; use strict; use Module::Pluggable except => qr/MyTest::Plugin::Foo/; sub new { my $class = shift; return bless {}, $class; } package MyTestSub; use strict; use Module::Pluggable search_path => "MyTest::Plugin"; sub new { my $class = shift; my $self = bless {}, $class; $self->except(qr/MyTest::Plugin::Foo/); return $self; } 1;
#!perl-wT use strict; use lib 't/lib'; use Test::More tests => 10; { my $foo; ok($foo = MyTest->new()); my @plugins; my @expected = qw(MyTest::Plugin::Bar MyTest::Plugin::Quux::Foo); ok(@plugins = sort $foo->plugins); is_deeply(\@plugins, \@expected); @plugins = (); ok(@plugins = sort MyTest->plugins); is_deeply(\@plugins, \@expected); } { my $foo; ok($foo = MyTestSub->new()); my @plugins; my @expected = qw(MyTest::Plugin::Bar MyTest::Plugin::Quux::Foo); ok(@plugins = sort $foo->plugins); is_deeply(\@plugins, \@expected); @plugins = (); ok(@plugins = sort MyTestSub->plugins); is_deeply(\@plugins, \@expected); } package MyTest; use strict; use Module::Pluggable except => [ "MyTest::Plugin::Foo" ]; sub new { my $class = shift; return bless {}, $class; } package MyTestSub; use strict; use Module::Pluggable search_path => "MyTest::Plugin"; sub new { my $class = shift; my $self = bless {}, $class; $self->except(["MyTest::Plugin::Foo"]); return $self; } 1;
#!perl-wT use strict; use lib 't/lib'; use Test::More tests => 10; { my $foo; ok($foo = MyTest->new()); my @plugins; my @expected = qw(MyTest::Plugin::Bar MyTest::Plugin::Quux::Foo); ok(@plugins = sort $foo->plugins); is_deeply(\@plugins, \@expected); @plugins = (); ok(@plugins = sort MyTest->plugins); is_deeply(\@plugins, \@expected); } { my $foo; ok($foo = MyTestSub->new()); my @plugins; my @expected = qw(MyTest::Plugin::Bar MyTest::Plugin::Quux::Foo); ok(@plugins = sort $foo->plugins); is_deeply(\@plugins, \@expected); @plugins = (); ok(@plugins = sort MyTestSub->plugins); is_deeply(\@plugins, \@expected); } package MyTest; use strict; use Module::Pluggable except => "MyTest::Plugin::Foo"; sub new { my $class = shift; return bless {}, $class; } package MyTestSub; use strict; use Module::Pluggable search_path => "MyTest::Plugin"; sub new { my $class = shift; my $self = bless {}, $class; $self->except("MyTest::Plugin::Foo"); return $self; } 1;
#!perl-wT use strict; use lib 't/lib'; use Test::More tests => 10; { my $foo; ok($foo = MyTest->new()); my @plugins; my @expected = qw(MyTest::Plugin::Foo); ok(@plugins = sort $foo->plugins); is_deeply(\@plugins, \@expected); @plugins = (); ok(@plugins = sort MyTest->plugins); is_deeply(\@plugins, \@expected); } { my $foo; ok($foo = MyTestSub->new()); my @plugins; my @expected = qw(MyTest::Plugin::Foo); ok(@plugins = sort $foo->plugins); is_deeply(\@plugins, \@expected); @plugins = (); ok(@plugins = sort MyTestSub->plugins); is_deeply(\@plugins, \@expected); } package MyTest; use strict; use Module::Pluggable only => qr/MyTest::Plugin::Foo$/; sub new { my $class = shift; return bless {}, $class; } package MyTestSub; use strict; use Module::Pluggable search_path => "MyTest::Plugin"; sub new { my $class = shift; my $self = bless {}, $class; $self->only(qr/MyTest::Plugin::Foo$/); return $self; } 1;
#!perl-wT use strict; use lib 't/lib'; use Test::More tests => 10; { my $foo; ok($foo = MyTest->new()); my @plugins; my @expected = qw(MyTest::Plugin::Foo); ok(@plugins = sort $foo->plugins); is_deeply(\@plugins, \@expected); @plugins = (); ok(@plugins = sort MyTest->plugins); is_deeply(\@plugins, \@expected); } { my $foo; ok($foo = MyTestSub->new()); my @plugins; my @expected = qw(MyTest::Plugin::Foo); ok(@plugins = sort $foo->plugins); is_deeply(\@plugins, \@expected); @plugins = (); ok(@plugins = sort MyTestSub->plugins); is_deeply(\@plugins, \@expected); } package MyTest; use strict; use Module::Pluggable only => [ "MyTest::Plugin::Foo" ]; sub new { my $class = shift; return bless {}, $class; } package MyTestSub; use strict; use Module::Pluggable search_path => "MyTest::Plugin"; sub new { my $class = shift; my $self = bless {}, $class; $self->only(["MyTest::Plugin::Foo"]); return $self; } 1;
#!perl-wT use strict; use lib 't/lib'; use Test::More tests => 10; { my $foo; ok($foo = MyTest->new()); my @plugins; my @expected = qw(MyTest::Plugin::Foo); ok(@plugins = sort $foo->plugins); is_deeply(\@plugins, \@expected); @plugins = (); ok(@plugins = sort MyTest->plugins); is_deeply(\@plugins, \@expected); } { my $foo; ok($foo = MyTestSub->new()); my @plugins; my @expected = qw(MyTest::Plugin::Foo); ok(@plugins = sort $foo->plugins); is_deeply(\@plugins, \@expected); @plugins = (); ok(@plugins = sort MyTestSub->plugins); is_deeply(\@plugins, \@expected); } package MyTest; use strict; use Module::Pluggable only => "MyTest::Plugin::Foo"; sub new { my $class = shift; return bless {}, $class; } package MyTestSub; use strict; use Module::Pluggable search_path => "MyTest::Plugin"; sub new { my $class = shift; my $self = bless {}, $class; $self->only("MyTest::Plugin::Foo"); return $self; } 1;
From: claco [...] chrislaco.com
[CLACO - Tue Jul 26 21:11:19 2005]: Show quoted text
> I recently had the need to set only/except programatically after use- > ing Module::Pluggable. This quick patch seemed to do the trick. I > haven't had time to patch the *only.t/*except.t files yet.
Here's an updated patch to do like search_path, and return only/except values from those subs. This brings up another problem that also effects search_path which I will post seperately.
--- Pluggable.pm.orig Sat Jul 09 08:20:38 2005 +++ Pluggable.pm Wed Jul 27 08:33:18 2005 @@ -417,11 +417,32 @@ return $opts{'search_path'}; }; + my $onlysub = sub { + my ($self, $only) = @_; + + if (defined $only) { + $opts{'only'} = $only; + }; + + return $opts{'only'}; + }; + + my $exceptsub = sub { + my ($self, $except) = @_; + + if (defined $except) { + $opts{'except'} = $except; + }; + + return $opts{'except'}; + }; no strict 'refs'; no warnings 'redefine'; *{"$pkg\::$sub"} = $subroutine; *{"$pkg\::search_path"} = $searchsub; + *{"$pkg\::only"} = $onlysub; + *{"$pkg\::except"} = $exceptsub; }
Fixed in 2.95