Commits

Toby Inkster committed c6006a7

hook API change; pass arrayref of parameters instead of list

Comments (0)

Files changed (5)

lib/MooX/CaptainHook.pm

 	my %already;
 	sub _fire
 	{
-		my (undef, $callbacks, $key, @args) = @_;
+		my (undef, $callbacks, $key, $args) = @_;
 		return if defined $key && $already{$key}++;
 		return unless $callbacks;
 		for my $cb (@$callbacks)
 		{
-			local $_ = $args[0];
-			$cb->(@args);
+			$cb->($args) for $args->[0];  # local $_ aliasing
 		}
 	}
 }
 		'MooX::CaptainHook'->_fire(
 			$on_application{$role},
 			"OnApplication: $package $role",
-			$package,
-			$role,
+			[ $package, $role ],
 		);
 		
 		# This stuff is for internals...
 # 
 sub _inflated
 {
-	my $meta = shift;
+	my $args = shift;
+	my $meta = $args->[0];
 	return unless $meta->isa('Moose::Meta::Role');
 	require Moose::Util::MetaRole;
-	Moose::Util::MetaRole::apply_metaroles(
+	$args->[0] = $meta = Moose::Util::MetaRole::apply_metaroles(
 		for            => $meta->name,
 		role_metaroles => {
 			role => eval q{
 					'MooX::CaptainHook'->_fire(
 						$on_application{$role},
 						"OnApplication: $package $role",
-						$package,
-						$role,
+						[ $package, $role ],
 					);
 					
 					# This stuff is for internals...
 	around inject_real_metaclass_for => sub
 	{
 		my ($orig, $pkg) = @_;
-		my $meta = $orig->($pkg);
+		my $args = [ scalar $orig->($pkg) ];
 		'MooX::CaptainHook'->_fire(
 			[
 				'MooX::CaptainHook'->can('_inflated'),
 				@{$on_inflation{$pkg}||[]}
 			],
 			undef,
-			$meta,
+			$args,
 		);
-		return $meta;
+		return $args->[0];
 	};
 	__PACKAGE__;
 };
 =item C<< on_application { BLOCK } >>
 
 The C<on_application> hook allows you to run a callback when your role
-is applied to a class or other role. Within the callback C<< $_[0] >>
+is applied to a class or other role. Within the callback C<< $_[0][0] >>
 is set to the name of the package that the role is being applied to.
 
-Also C<< $_[1] >> is set to the name of the role being applied, which
-may not be the same as the role where the hook was initially defined.
-(For example, when role X establishes a hook; role X is consumed by role
-Y; and role Y is consumed by class Z. Then the callback code will run
-twice, once with C<< @_ = qw(Y X) >> and once with C<< @_ = (Z Y) >>.)
+Also C<< $_[0][1] >> is set to the name of the role being applied, which
+may not be the same as the role where the hook was initially defined. (For
+example, when role X establishes a hook; role X is consumed by role Y; and
+role Y is consumed by class Z. Then the callback code will run twice, once
+with C<< $_[0] = [qw(Y X)] >> and once with C<< $_[0] = [qw(Z Y)] >>.)
+
+Altering the C<< $_[0] >> arrayref will alter what is passed to subsequent
+callbacks, so is not recommended.
 
 =item C<< on_inflation { BLOCK } >>
 
 The C<on_inflation> hook runs if your class or role is "inflated" to a
-full Moose class or role. C<< $_[0] >> is the associated metaclass.
+full Moose class or role. C<< $_[0][0] >> is the associated metaclass.
+
+Setting C<< $_[0][0] >> to a new meta object should "work" (whatever that
+means).
 
 =item C<< is_role($package) >>
 
 =back
 
 Within callback codeblocks, C<< $_ >> is also available as a convenient
-alias to C<< $_[0] >>.
+alias to C<< $_[0][0] >>.
 
 =head2 Installing Hooks for Other Packages
 

lib/MooX/ClassAttribute.pm

 {
 	my ($me, $target, $name, $spec) = @_;
 	on_application {
-		my ($applied_to) = @_;
+		my $applied_to = $_;
 		$me
 			-> _class_accessor_maker_for($applied_to)
 			-> generate_method($applied_to, $name, $spec);
 	use MooX::CaptainHook qw(on_application);
 	
 	on_application {
-		push @output, "@_";
+		push @output, "@{$_[0]}";
 	};
 }
 

t/21hook_appl_moose.t

 	use MooX::CaptainHook qw(on_application);
 	
 	on_application {
-		push @output, "@_";
+		push @output, "@{$_[0]}";
 	};
 }
 
 	use Moo;
 	use MooX::CaptainHook qw( on_inflation );
 	on_inflation {
-		push @inflated, sprintf("%s (%s)", $_->name, ref $_);
+		push @inflated, sprintf("%s (%s)", $_->name, $_->isa('Moose::Meta::Role')?'Role':'Class');
 	};
 }
 
 	use Moo::Role;
 	use MooX::CaptainHook qw( on_inflation );	
 	on_inflation {
-		push @inflated, sprintf("%s (%s)", $_->name, ref $_);
+		push @inflated, sprintf("%s (%s)", $_->name, $_->isa('Moose::Meta::Role')?'Role':'Class');
 	};
 }
 
 is_deeply(
 	[ sort @inflated ],
 	[
-		"Boo (Moose::Meta::Role)",
-		"Foo (Moose::Meta::Class)",
+		"Boo (Role)",
+		"Foo (Class)",
 	],
 ) or diag explain \@inflated;
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.