From: Matt S Trout Date: Thu, 5 Jun 2014 02:03:55 +0000 (+0000) Subject: import basic Object::Tap X-Git-Tag: v0.009001 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9e15d0fb6c450012ea031b97986ff65621c40747;p=p5sagit%2FObject-Tap.git import basic Object::Tap --- 9e15d0fb6c450012ea031b97986ff65621c40747 diff --git a/dist.ini b/dist.ini new file mode 100644 index 0000000..d8667c8 --- /dev/null +++ b/dist.ini @@ -0,0 +1,19 @@ +name = Object-Tap +author = Matt S Trout (mst) +copyright_holder = Matt S Trout +copyright_year = 2014 +license = Perl_5 +version = 0.009001 + +[GatherDir] +[PruneCruft] +[ManifestSkip] +[MetaYAML] +[MetaJSON] +[License] +[MakeMaker::Fallback] +[ModuleBuildTiny::Fallback] +[Manifest] +[TestRelease] +[ConfirmRelease] +[UploadToCPAN] diff --git a/lib/Object/Tap.pm b/lib/Object/Tap.pm new file mode 100644 index 0000000..f1e1479 --- /dev/null +++ b/lib/Object/Tap.pm @@ -0,0 +1,65 @@ +package Object::Tap; + +use strict; +use warnings; +use base qw(Exporter); + +our $VERSION = '0.009001'; # 0.9.1 + +our @EXPORT = qw($_tap); + +our $_tap = sub { my ($obj, $call, @args) = @_; $obj->$call(@args); $obj }; + +1; + +=head1 NAME + +Object::Tap - Tap into a series of method calls to alter an object + +=head1 SYNOPSIS + +Instead of writing - + + my $thing = My::Class->new(...); + + $thing->set_foo(1); + +you can instead write - + + use Object::Tap; + + my $thing = My::Class->new(...)->$_tap(sub { $_[0]->set_foo(1) }); + +To realise why this might be useful, consider instead - + + My::App->new(...)->$_tap(...)->run; + +where a variable is thereby not required at all. + +You can also pass extra args - + + $obj->$_tap(sub { warn "Got arg: $_[1]" }, 'arg'); + +or use a method name instead of a sub name - + + my $thing = My::Class->new(...)->$_tap(set_foo => 1); + +=head1 AUTHOR + +mst - Matt S. Trout (cpan:MSTROUT) + +=head1 CONTRIBUTORS + +None yet. Well volunteered? :) + +=head1 COPYRIGHT + +Copyright (c) 2014 the Object::Tap L and L +as listed above. + +=head1 LICENSE + +This library is free software and may be distributed under the same terms +as perl itself. + +=cut diff --git a/t/01basic.t b/t/01basic.t new file mode 100644 index 0000000..58d2279 --- /dev/null +++ b/t/01basic.t @@ -0,0 +1,16 @@ +use strict; +use warnings FATAL => 'all'; +use Test::More qw(no_plan); +use Object::Tap; + +my $tapped; + +sub Foo::bar { $tapped = join(' ', @_) } + +is(Foo->$_tap(sub { $_[0]->bar($_[1]) }, 'one'), 'Foo', 'invocant returned'); + +is($tapped, 'Foo one', 'subref tap'); + +is(Foo->$_tap(bar => 'two'), 'Foo', 'invocant returned'); + +is($tapped, 'Foo two', 'method name tap');