Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Sub-Exporter CPAN distribution.

Report information
The Basics
Id: 70330
Status: resolved
Priority: 0/
Queue: Sub-Exporter

People
Owner: Nobody in particular
Requestors: ether [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.982
Fixed in: (no value)



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__
19:14 <@rjbs> That's how you pass special behavior to S'Ex. 19:15 <@rjbs> like: { importer => method_importer } or { into => $package } 19:15 <@ether> ah I see 19:15 <@rjbs> You have no power to affect this without writing your own &import. 19:15 <@ether> is use Foo {} { my_stuff => }; correct? 19:15 <@ether> that is, {} as the first argument? 19:15 <@rjbs> Yeah. 19:15 <@rjbs> use Foo {}, ... 19:15 <@ether> ok, that's good enough for me 19:15 <@rjbs> But probably you just want: 19:16 <@rjbs> use Foo x => { my_stuff } 19:16 <@rjbs> I don't know enough about your specific uses here, but that just seems a lot easier.