our %Variable;
+my $sanitize_importing = sub {
+ my ($me, $spec) = @_;
+ return []
+ unless defined $spec;
+ return [map {
+ my $import_args = $spec->{$_};
+ croak sprintf q{Import argument list for '%s' is not an array ref},
+ $_,
+ unless ref($import_args) and ref($import_args) eq 'ARRAY';
+ [$_ => $import_args];
+ } 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) {
+ my $key = shift @specced;
+ push @imports, [
+ $key,
+ (ref($specced[0]) and ref($specced[0]) eq 'ARRAY')
+ ? shift(@specced)
+ : [],
+ ];
+ }
+ return \@imports;
+};
+
sub import {
my $target = caller;
my $me = shift;
no strict 'refs';
$Variable{$variable} = {
anon => $anon,
- args => \%args,
+ args => {
+ %args,
+ importing => $me->$sanitize_importing($args{importing}),
+ },
subs => {
map +($_ => sub {}), @{$args{subs}||[]},
},
}
}
-my $sanitize_importing = sub {
- my ($me, $spec) = @_;
- return []
- unless defined $spec;
- return [map {
- my $import_args = $spec->{$_};
- croak sprintf q{Import argument list for '%s' are is an array ref},
- $_,
- unless ref($import_args) and ref($import_args) eq 'ARRAY';
- [$_ => $import_args];
- } 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) {
- my $key = shift @specced;
- push @imports, [
- $key,
- (ref($specced[0]) and ref($specced[0]) eq 'ARRAY')
- ? 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(
is_deeply [@imported], [qw( TestImportableA TestImportableB )],
'multiple imports in the right order';
+like exception {
+ Package::Variant->import(
+ importing => \'foo', subs => [qw( foo )],
+ );
+}, qr/importing.+option.+hash.+array/i, 'invalid "importing" option';
+
+like exception {
+ Package::Variant->import(
+ importing => { foo => \'bar' }, subs => [qw( bar )],
+ );
+}, qr/import.+argument.+not.+array/i, 'invalid import argument list';
+
done_testing;