From: Robert Sedlacek Date: Mon, 28 Nov 2011 21:59:11 +0000 (+0100) Subject: accept array references for importing X-Git-Tag: v1.000000~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit%2FPackage-Variant.git;a=commitdiff_plain;h=067e51adaaf12b0b8c61096e5e6c65593062eb6e accept array references for importing --- diff --git a/lib/Package/Variant.pm b/lib/Package/Variant.pm index b0c2698..4407397 100644 --- a/lib/Package/Variant.pm +++ b/lib/Package/Variant.pm @@ -1,6 +1,7 @@ package Package::Variant; use strictures 1; +use Carp qw( croak ); our %Variable; @@ -39,15 +40,39 @@ sub import { } } +my $sanitize_importing = sub { + my ($me, $spec) = @_; + return [] + unless defined $spec; + return [map [$_ => $spec->{$_}], keys %$spec] + if ref $spec eq 'HASH'; + croak q{The 'importing' option has to be either a hash or array ref} + unless ref $spec eq 'ARRAY'; + my @specced = @$spec; + my @imports; + while (@specced) { + push @imports, [shift(@specced), shift(@specced)]; + } + return \@imports; +}; + sub build_variant_of { my ($me, $variable, @args) = @_; my $variant_name = "${variable}::_Variant_".++$Variable{$variable}{anon}; - my $import = $Variable{$variable}{args}{importing} || {}; + my $import = $me + ->$sanitize_importing($Variable{$variable}{args}{importing}); my $setup = join("\n", "package ${variant_name};", (map sprintf( - q!use %s @{$import->{'%s'}||[]};!, $_, quotemeta($_), - ), keys %$import), + q!use %s %s;!, + $import->[$_][0], + not(defined $import->[$_][1]) + ? '' + : sprintf( + q!@{$import->[%d][1]}!, + $_, + ), + ), 0..$#$import), "1;", ); eval $setup @@ -208,6 +233,15 @@ containing import arguments. The packages will be Cd with the given arguments by every variation before the L method is asked to create the package. +If import order is important to you, you can also pass the C +arguments as a flag array reference: + + use Package::Variant + importing => [ PackageA => [], PackageB => [] ]; + +If you want to import whatever the package exports by default, you have to +pass C instead of an empty array reference. + =head2 subs An array reference of strings listing the names of subroutines that should