X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FPackage%2FVariant.pm;h=a05af5b129c351385295426e8602cb6a071f7edc;hb=4e3b81776277e0dc30ed03205a9fee166a98d9fa;hp=bc76781b6a049acb41a23912db2d2dc1932bfb7f;hpb=6d22d9387f38269a3e492dd557b7e585651a4d61;p=p5sagit%2FPackage-Variant.git diff --git a/lib/Package/Variant.pm b/lib/Package/Variant.pm index bc76781..a05af5b 100644 --- a/lib/Package/Variant.pm +++ b/lib/Package/Variant.pm @@ -2,10 +2,10 @@ package Package::Variant; use strictures 1; use Import::Into; -use Module::Runtime qw(use_module); +use Module::Runtime qw(require_module); use Carp qw(croak); -our $VERSION = '1.001003'; # 1.1.3 +our $VERSION = '1.002000'; # 1.2.0 $VERSION = eval $VERSION; @@ -54,11 +54,10 @@ my $sub_namer = eval { } || sub { $_[-1] }; sub import { - my $target = caller; + my $variable = caller; my $me = shift; - my $last = (split '::', $target)[-1]; + my $last = (split '::', $variable)[-1]; my $anon = 'A000'; - my $variable = $target; my %args = @_; no strict 'refs'; $Variable{$variable} = { @@ -71,7 +70,7 @@ sub import { map +($_ => sub {}), @{$args{subs}||[]}, }, }; - *{"${target}::import"} = sub { + *{"${variable}::import"} = sub { my $target = caller; my (undef, %arg) = @_; my $as = defined($arg{as}) ? $arg{as} : $last; @@ -82,13 +81,17 @@ sub import { }; my $subs = $Variable{$variable}{subs}; foreach my $name (keys %$subs) { - *{"${target}::${name}"} = sub { + *{"${variable}::${name}"} = sub { goto &{$subs->{$name}} }; } - *{"${target}::install"} = sub { + *{"${variable}::install"} = sub { goto &{$Variable{$variable}{install}}; - } + }; + *{"${variable}::build_variant"} = sub { + shift; + $me->build_variant_of($variable, @_); + }; } sub build_variant_of { @@ -96,7 +99,9 @@ sub build_variant_of { my $variant_name = "${variable}::_Variant_".++$Variable{$variable}{anon}; foreach my $to_import (@{$Variable{$variable}{args}{importing}}) { my ($pkg, $args) = @$to_import; - use_module($pkg)->import::into($variant_name, @{$args}); + require_module $pkg; + eval q{ BEGIN { $pkg->import::into($variant_name, @{$args}) }; 1; } + or die $@; } my $subs = $Variable{$variable}{subs}; local @{$subs}{keys %$subs} = map $variant_name->can($_), keys %$subs; @@ -129,7 +134,7 @@ Package::Variant - Parameterizable packages # what modules to 'use' importing => ['Moo::Role'], # proxied subroutines - subs => [ qw(has around before after with) ], + subs => [ qw(has around before after with) ]; sub make_variant { my ($class, $target_package, %arguments) = @_; @@ -161,7 +166,7 @@ This module allows you to build packages that return different variations depending on what parameters are given. Users of your package will receive a subroutine able to take parameters -and return the name of a suitable variant package. The implmenetation does +and return the name of a suitable variant package. The implementation does not care about what kind of package it builds. =head2 Declaring a variable package @@ -235,7 +240,7 @@ subroutine under a different name by specifying an C argument. For regular uses, the L provides more than enough flexibility. However, if you want to create variations of -dynamically determined packages, you can use the L +dynamically determined packages, you can use the L method. You can use this to create variations of other packages and pass arguments @@ -285,7 +290,7 @@ also pass a string instead: An array reference of strings listing the names of subroutines that should be proxied. These subroutines are expected to be installed into the new variant package by the modules imported with L. Subroutines -with the same name will be availabe in your declaration package, and will +with the same name will be available in your declaration package, and will proxy through to the newly created package when used within L. @@ -327,14 +332,24 @@ Exports the generator subroutine under a different name than the default. =back +=head2 build_variant + + use Some::Variant::Package (); + my $variant_package = Some::Variant::Package->build_variant( @arguments ); + +This method is provided for you. It will generate a variant package +and return its name, just like the generator sub provided by +L. This allows you to avoid importing anything into the +consuming package. + =head1 C METHODS These methods are available on C itself. -=head2 build_variation_of +=head2 build_variant_of my $variant_package = Package::Variant - ->build_variation_of($variable_package, @arguments); + ->build_variant_of($variable_package, @arguments); This is the dynamic method of creating new variants. It takes the C<$variable_package>, which is a pre-declared variable package, and a set @@ -373,6 +388,8 @@ mst - Matt S. Trout (cpan:MSTROUT) phaylon - Robert Sedlacek (cpan:PHAYLON) +haarg - Graham Knop (cpan:HAARG) + =head1 COPYRIGHT Copyright (c) 2010-2012 the C L and