Subject: | Difficulty in getting arguments into a collector |
I'm having difficulty getting arguments into a default INIT collector,
when only passing one argument:
(code also in http://paste.scsys.co.uk/133872 )
use Foo 'foo', 'bar'; works properly (with both foo and bar showing up
in $import_args), but use Foo 'foo'; does not - $import_args ends up
with [ '-default', undef ];
use MyApp::CommonIncludes ':modules', { foo => 1 };
results in:
### got import args: $VAR1 = [
[
':modules',
{
'skip_cache_init' => 1
}
]
];
use MyApp::CommonIncludes { foo => 1};
results in:
### got import args: $VAR1 = [
[
'-default',
undef
]
];
...how can I make the latter case work, so that { foo => 1 } shows up in
%extra_args?
code:
package MyApp::CommonIncludes;
use strict;
use warnings;
use Data::Dumper;
use Sub::Exporter -setup => {
exports => [
qw(Dumper),
],
groups => {
always => [ qw(Dumper) ],
},
collectors => [ INIT => \'_init_hook' ],
};
sub _init_hook {
my ($self, undef, $data) = @_;
my $import_args = $data->{import_args};
print "### got import args: ", Dumper($import_args);
# grab anything passed as a hashref, and pass along to db init hooks
my @extra_args = grep { ref eq 'HASH' } map { @$_ } @$import_args;
my %extra_args;
%extra_args = %{$extra_args[0]} if @extra_args;
# do something with %extra_args here.
# avoid loading (and auto-generating) lots of expensive Moose
classes if
# we don't actually need them!
if (my (@i) = grep { $import_args->[$_][0] eq ':modules' } (0 ..
$#$import_args))
{
# do extra stuff here.
splice @$import_args, $_, 1 for reverse @i;
}
# simulate an 'always' group
push @$import_args, [ ':always', undef ];
return 1;
}
1;
__END__