add skeleton Makefile.PL and VERSION
[p5sagit/Package-Variant.git] / lib / Package / Variant.pm
index 304e9a5..55dd25c 100644 (file)
@@ -3,8 +3,50 @@ package Package::Variant;
 use strictures 1;
 use Carp qw( croak );
 
+our $VERSION = '1.000000'; # 1.0.0
+
+$VERSION = eval $VERSION;
+
 our %Variable;
 
+my $sanitize_importing = sub {
+  my ($me, $spec) = @_;
+  return []
+    unless defined $spec;
+  my @specced =
+    not(ref $spec)
+      ? ($spec)
+    : (ref($spec) eq 'ARRAY')
+      ? (@$spec)
+    : (ref($spec) eq 'HASH')
+      ? (map {
+          croak qq{The import argument list for '$_' is not an array ref}
+            unless ref($spec->{$_}) eq 'ARRAY';
+          ($_ => $spec->{$_});
+        } sort keys %$spec)
+    : croak q{The 'importing' option has to be either a hash or array ref};
+  my @imports;
+  my $arg_count = 1;
+  while (@specced) {
+    my $key = shift @specced;
+    croak qq{Value $arg_count in 'importing' is not a package string},
+      $arg_count
+      unless defined($key) and not(ref $key);
+    $arg_count++;
+    my $import_args =
+      (not(@specced) or (defined($specced[0]) and not ref($specced[0])))
+        ? []
+      : (ref($specced[0]) eq 'ARRAY')
+        ? do { $arg_count++; shift @specced }
+      : croak(
+            qq{Value $arg_count for package '$key' in 'importing' is not}
+          . qq{ a package string or array ref}
+        );
+    push @imports, [$key, $import_args];
+  }
+  return \@imports;
+};
+
 sub import {
   my $target = caller;
   my $me = shift;
@@ -15,7 +57,10 @@ sub import {
   no strict 'refs';
   $Variable{$variable} = {
     anon => $anon,
-    args => \%args,
+    args => {
+      %args,
+      importing => $me->$sanitize_importing($args{importing}),
+    },
     subs => {
       map +($_ => sub {}), @{$args{subs}||[]},
     },
@@ -40,38 +85,21 @@ 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 = $me
-    ->$sanitize_importing($Variable{$variable}{args}{importing});
+  my $import = $Variable{$variable}{args}{importing};
   my $setup = join("\n",
     "package ${variant_name};",
     (map sprintf(
       q!use %s %s;!,
       $import->[$_][0],
-      not(defined $import->[$_][1])
-        ? ''
-        : sprintf(
+      scalar(@{$import->[$_][1]})
+        ? sprintf(
           q!@{$import->[%d][1]}!,
           $_,
-        ),
+        )
+        : '',
     ), 0..$#$import),
     "1;",
   );
@@ -103,7 +131,7 @@ Package::Variant - Parameterizable packages
   use strictures 1;
   use Package::Variant
     # what modules to 'use'
-    importing => { 'Moo::Role' => undef },
+    importing => ['Moo::Role'],
     # proxied subroutines
     subs => [qw( has around before after extends )],
 
@@ -156,12 +184,13 @@ describe how you intend to build your variations.
     importing => { $package => \@import_arguments, ... },
     subs      => [ @proxied_subroutine_names ];
 
-The L</importing> option needs to be a hash reference with package names
-to be C<use>d as keys, and array references containing the import
-arguments as values. These packages will be imported into every new
+The L</importing> option needs to be a hash or array reference with
+package names to be C<use>d as keys, and array references containing the
+import arguments as values. These packages will be imported into every new
 variant, and need to set up every declarative subroutine you require to
 build your variable package. The next option will allow you to use these
-functions. See L</importing> for more options.
+functions. See L</importing> for more options. You can omit empty import
+argument lists when passing an array reference.
 
 The L</subs> option is an array reference of subroutine names that are
 exported by the packages specified with L</importing>. These subroutines
@@ -234,13 +263,26 @@ arguments by every variation before the L</make_variant> method is asked
 to create the package.
 
 If import order is important to you, you can also pass the C<importing>
-arguments as a flag array reference:
+arguments as a flat array reference:
 
   use Package::Variant
-    importing => [ PackageA => [], PackageB => [] ];
+    importing => [ 'PackageA', 'PackageB' ];
 
-If you want to import whatever the package exports by default, you have to
-pass C<undef> instead of an empty array reference.
+  # same as
+  use Package::Variant
+    importing => [ 'PackageA' => [], 'PackageB' => [] ];
+
+  # or
+  use Package::Variant
+    importing => { 'PackageA' => [], 'PackageB' => [] };
+
+The import method will be called even if the list of import arguments is
+empty or not specified,
+
+If you just want to import a single package's default exports, you can
+also pass a string instead:
+
+  use PAckage::Variant importing => 'Package';
 
 =head2 subs
 
@@ -326,15 +368,16 @@ method.
 
 =head1 AUTHOR
 
-=over
+mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
 
-=item mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
+=head1 CONTRIBUTORS
 
-=back
+phaylon - Robert Sedlacek (cpan:PHAYLON) <r.sedlacek@shadowcat.co.uk>
 
 =head1 COPYRIGHT
 
-Copyright (c) 2010-2011 the C<Package::Stash> L</AUTHOR> as listed above.
+Copyright (c) 2010-2011 the C<Package::Variant> L</AUTHOR> and
+L</CONTRIBUTORS> as listed above.
 
 =head1 LICENSE