From: Matt S Trout Date: Thu, 13 Jul 2017 18:39:09 +0000 (+0000) Subject: add $_ aliasing X-Git-Tag: v1.000006~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9d1b22e38282b95ee984108bbef40a6284b243a2;p=p5sagit%2FObject-Tap.git add $_ aliasing --- diff --git a/lib/Object/Tap.pm b/lib/Object/Tap.pm index cecdbd2..ccffbcb 100644 --- a/lib/Object/Tap.pm +++ b/lib/Object/Tap.pm @@ -8,7 +8,11 @@ our $VERSION = '1.000006'; # TRIAL our @EXPORT = qw($_tap); -our $_tap = sub { my ($obj, $call, @args) = @_; $obj->$call(@args); $obj }; +our $_tap = sub { + my ($obj, $call, @args) = @_; + $obj->$call(@args) for $obj; + $obj +}; 1; @@ -30,6 +34,12 @@ you can instead write - my $thing = My::Class->new(...)->$_tap(sub { $_[0]->set_foo(1) }); +We also alias $_ to $_[0] within the subroutine so: + + my $thing = My::Class->new(...)->$_tap(sub { $_->set_foo(1) }); + +also works. + To realise why this might be useful, consider instead - My::App->new(...)->$_tap(...)->run; diff --git a/t/01basic.t b/t/01basic.t index 58d2279..f1b138f 100644 --- a/t/01basic.t +++ b/t/01basic.t @@ -14,3 +14,5 @@ is($tapped, 'Foo one', 'subref tap'); is(Foo->$_tap(bar => 'two'), 'Foo', 'invocant returned'); is($tapped, 'Foo two', 'method name tap'); + +Foo->$_tap(sub { is($_[0], $_, '$_ aliased to object') });