An object method which helps with chaining, inspired by ruby
# This fails because the "post" method doesn't return # $self; it returns an HTTP::Request object. # LWP::UserAgent -> new -> post('http://www.example.com/submit', \%data) -> get('http://www.example.com/status'); # The 'tap' method runs some code and always returns $self. # LWP::UserAgent -> new -> tap(post => [ 'http://www.example.com/submit', \%data ]) -> get('http://www.example.com/status'); # Or use a coderef... # LWP::UserAgent -> new -> tap(sub { $_->post('http://www.example.com/submit', \%data) }) -> get('http://www.example.com/status');
This module has nothing to do with the Test Anything Protocol (\s-1TAP\s0, see Test::Harness).
This module is a role for your class, providing it with a \*(C`tap\*(C' method. The \*(C`tap\*(C' method is an aid to chaining. You can do for example:
$object ->tap( sub{ $_->foo(1) } ) ->tap( sub{ $_->bar(2) } ) ->tap( sub{ $_->baz(3) } );
... without worrying about what the \*(C`foo\*(C', \*(C`bar\*(C' and \*(C`baz\*(C' methods return, because \*(C`tap\*(C' always returns its invocant.
The \*(C`tap\*(C' method also provides a few shortcuts, so that the above can actually be written:
$object->tap(foo => [1], bar => [2], baz => [3]);
... but more about that later. Anyway, this module provides one method for your class - \*(C`tap\*(C' - which is described below. This can be called as an object or class method, but is usually used as an object method.
Each argument is processed in the order given. It is processed differently, depending on the kind of argument it is.
Coderef arguments
An argument that is a coderef (or a blessed argument that overloads \*(C`&{}\*(C' - see overload) will be executed in a context where $_ has been set to the invocant of the tap method \*(C`tap\*(C'. The return value of the coderef is ignored. For example:
{ package My::Class; use Role::Commons qw(Tap); } print My::Class->tap( sub { warn uc $_; return 'X' }, );
... will warn \*(L"\s-1MY::CLASS\s0\*(R" and then print \*(L"My::Class\*(R".
Because each argument to \*(C`tap\*(C' is processed in order, you can provide multiple coderefs:
print My::Class->tap( sub { warn uc $_; return 'X' }, sub { warn lc $_; return 'Y' }, );
String arguments
A non-reference argument (i.e. a string) is treated as a shortcut for a method call on the invocant. That is, the following two taps are equivalent:
$object->tap( sub{$_->foo(@_)} ); $object->tap( 'foo' );
Arrayref arguments
An arrayref is dereferenced yielding a list. This list is passed as an argument list when executing the previous coderef argument (or string argument). The following three taps are equivalent:
$object->tap( sub { $_->foo('bar', 'baz') }, ); $object->tap( sub { $_->foo(@_) }, ['bar', 'baz'], ); $object->tap( foo => ['bar', 'baz'], );
Scalar ref arguments
There are a handful of special scalar ref arguments that are supported: This indicates that you wish for all subsequent coderefs to be wrapped in an \*(C`eval\*(C', making any errors that occur within it non-fatal. $object->tap(\"EVAL", sub {...}); Switches back to the default behaviour of not wrapping coderefs in \*(C`eval\*(C'. $object->tap( \"EVAL", sub {...}, # any fatal errors will be caught and ignored \"NO_EVAL", sub {...}, # fatal errors are properly fatal again. );
Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Role-Commons <http://rt.cpan.org/Dist/Display.html?Queue=Role-Commons>.
Role::Commons.
http://tea.moertel.com/articles/2007/02/07/ruby-1-9-gets-handy-new-method-object-tap <http://tea.moertel.com/articles/2007/02/07/ruby-1-9-gets-handy-new-method-object-tap>, <http://prepan.org/module/3Yz7PYrBLN>.
Toby Inkster <[email protected]>.
This software is copyright (c) 2012 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
\s-1THIS\s0 \s-1PACKAGE\s0 \s-1IS\s0 \s-1PROVIDED\s0 \*(L"\s-1AS\s0 \s-1IS\s0\*(R" \s-1AND\s0 \s-1WITHOUT\s0 \s-1ANY\s0 \s-1EXPRESS\s0 \s-1OR\s0 \s-1IMPLIED\s0 \s-1WARRANTIES\s0, \s-1INCLUDING\s0, \s-1WITHOUT\s0 \s-1LIMITATION\s0, \s-1THE\s0 \s-1IMPLIED\s0 \s-1WARRANTIES\s0 \s-1OF\s0 \s-1MERCHANTIBILITY\s0 \s-1AND\s0 \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0.