X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FPackage%2FVariant.pm;h=3b371d2e049760039148ac727758200a3739e018;hb=2f1d078be2dc2914f11aa66b80604d7abcccc213;hp=06cf0bfc79697e2b553af2d7b34f6712ac3a8277;hpb=6779ecfd9c1a2a51cc5c26596ced7a36e27cf9dd;p=p5sagit%2FPackage-Variant.git diff --git a/lib/Package/Variant.pm b/lib/Package/Variant.pm index 06cf0bf..3b371d2 100644 --- a/lib/Package/Variant.pm +++ b/lib/Package/Variant.pm @@ -1,11 +1,11 @@ package Package::Variant; -use strictures 1; +use strictures 2; use Import::Into; use Module::Runtime qw(require_module); use Carp qw(croak); -our $VERSION = '1.002002'; +our $VERSION = '1.003002'; $VERSION = eval $VERSION; @@ -135,9 +135,11 @@ Package::Variant - Parameterizable packages =head1 SYNOPSIS +Creation of anonymous variants: + # declaring a variable Moo role package My::VariableRole::ObjectAttr; - use strictures 1; + use strictures 2; use Package::Variant # what modules to 'use' importing => ['Moo::Role'], @@ -158,7 +160,7 @@ Package::Variant - Parameterizable packages # using the role package My::Class::WithObjectAttr; - use strictures 1; + use strictures 2; use Moo; use My::VariableRole::ObjectAttr; @@ -168,6 +170,50 @@ Package::Variant - Parameterizable packages my $obj = My::Class::WithObjectAttr->new; $obj->some_obj; # returns a Some::Class instance +And the same thing, only with named variants: + + # declaring a variable Moo role that can be named + package My::VariableRole::ObjectAttrNamed; + use strictures 2; + use Package::Variant importing => ['Moo::Role'], + subs => [ qw(has around before after with) ]; + use Module::Runtime 'module_notional_filename'; # only if you need protection + + # this method is run at variant creation time to determine its custom + # package name. it can use the arguments or do something entirely else. + sub make_variant_package_name { + my ($class, $package, %arguments) = @_; + $package = "Private::$package"; # you can munge the input here if you like + # only if you *need* protection + die "Won't clobber $package" if $INC{module_notional_filename $package}; + return $package; + } + + # same as in the example above, except for the argument list. in this example + # $package is the user input, and + # $target_package is the actual package in which the variant gets installed + sub make_variant { + my ($class, $target_package, $package, %arguments) = @_; + my $name = $arguments{name}; + has $name => (is => 'lazy'); + install "_build_${name}" => sub {return $arguments{class}->new}; + } + + # using the role + package My::Class::WithObjectAttr; + use strictures 2; + use Moo; + use My::VariableRole::ObjectAttrNamed; + + # create the role under a specific name + ObjectAttrNamed "My::Role" => (name => 'some_obj', class => 'Some::Class'); + # and use it + with "Private::My::Role"; + + # using our class + my $obj = My::Class::WithObjectAttr->new; + $obj->some_obj; # returns a Some::Class instance + =head1 DESCRIPTION This module allows you to build a variable package that contains a package