.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Role::Commons::Tap 3pm" .TH Role::Commons::Tap 3pm 2024-03-07 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Role::Commons::Tap \- an object method which helps with chaining, inspired by Ruby .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 7 \& # This fails because the "post" method doesn\*(Aqt return \& # $self; it returns an HTTP::Request object. \& # \& LWP::UserAgent \& \-> new \& \-> post(\*(Aqhttp://www.example.com/submit\*(Aq, \e%data) \& \-> get(\*(Aqhttp://www.example.com/status\*(Aq); \& \& # The \*(Aqtap\*(Aq method runs some code and always returns $self. \& # \& LWP::UserAgent \& \-> new \& \-> tap(post => [ \*(Aqhttp://www.example.com/submit\*(Aq, \e%data ]) \& \-> get(\*(Aqhttp://www.example.com/status\*(Aq); \& \& # Or use a coderef... \& # \& LWP::UserAgent \& \-> new \& \-> tap(sub { $_\->post(\*(Aqhttp://www.example.com/submit\*(Aq, \e%data) }) \& \-> get(\*(Aqhttp://www.example.com/status\*(Aq); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" \&\fBDO NOT USE THIS MODULE!\fR Use Object::Tap or Object::Util instead. They are not drop-in replacements, but a far more sensible way to have a \f(CW\*(C`tap\*(C'\fR method. .PP This module has nothing to do with the Test Anything Protocol (TAP, see Test::Harness). .PP This module is a role for your class, providing it with a \f(CW\*(C`tap\*(C'\fR method. The \f(CW\*(C`tap\*(C'\fR method is an aid to chaining. You can do for example: .PP .Vb 4 \& $object \& \->tap( sub{ $_\->foo(1) } ) \& \->tap( sub{ $_\->bar(2) } ) \& \->tap( sub{ $_\->baz(3) } ); .Ve .PP \&... without worrying about what the \f(CW\*(C`foo\*(C'\fR, \f(CW\*(C`bar\*(C'\fR and \f(CW\*(C`baz\*(C'\fR methods return, because \f(CW\*(C`tap\*(C'\fR always returns its invocant. .PP The \f(CW\*(C`tap\*(C'\fR method also provides a few shortcuts, so that the above can actually be written: .PP .Vb 1 \& $object\->tap(foo => [1], bar => [2], baz => [3]); .Ve .PP \&... but more about that later. Anyway, this module provides one method for your class \- \f(CW\*(C`tap\*(C'\fR \- which is described below. .ie n .SS tap(@arguments) .el .SS \f(CWtap(@arguments)\fP .IX Subsection "tap(@arguments)" This can be called as an object or class method, but is usually used as an object method. .PP Each argument is processed in the order given. It is processed differently, depending on the kind of argument it is. .PP \fICoderef arguments\fR .IX Subsection "Coderef arguments" .PP An argument that is a coderef (or a blessed argument that overloads \&\f(CW\*(C`&{}\*(C'\fR \- see overload) will be executed in a context where \&\f(CW$_\fR has been set to the invocant of the tap method \f(CW\*(C`tap\*(C'\fR. The return value of the coderef is ignored. For example: .PP .Vb 7 \& { \& package My::Class; \& use Role::Commons qw(Tap); \& } \& print My::Class\->tap( \& sub { warn uc $_; return \*(AqX\*(Aq }, \& ); .Ve .PP \&... will warn "MY::CLASS" and then print "My::Class". .PP Because each argument to \f(CW\*(C`tap\*(C'\fR is processed in order, you can provide multiple coderefs: .PP .Vb 4 \& print My::Class\->tap( \& sub { warn uc $_; return \*(AqX\*(Aq }, \& sub { warn lc $_; return \*(AqY\*(Aq }, \& ); .Ve .PP \fIString arguments\fR .IX Subsection "String arguments" .PP 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: .PP .Vb 2 \& $object\->tap( sub{$_\->foo(@_)} ); \& $object\->tap( \*(Aqfoo\*(Aq ); .Ve .PP \fIArrayref arguments\fR .IX Subsection "Arrayref arguments" .PP 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: .PP .Vb 10 \& $object\->tap( \& sub { $_\->foo(\*(Aqbar\*(Aq, \*(Aqbaz\*(Aq) }, \& ); \& $object\->tap( \& sub { $_\->foo(@_) }, \& [\*(Aqbar\*(Aq, \*(Aqbaz\*(Aq], \& ); \& $object\->tap( \& foo => [\*(Aqbar\*(Aq, \*(Aqbaz\*(Aq], \& ); .Ve .PP \fIScalar ref arguments\fR .IX Subsection "Scalar ref arguments" .PP There are a handful of special scalar ref arguments that are supported: .ie n .IP """\e""EVAL""""" 4 .el .IP "\f(CW\e""EVAL""\fR" 4 .IX Item """EVAL""" This indicates that you wish for all subsequent coderefs to be wrapped in an \f(CW\*(C`eval\*(C'\fR, making any errors that occur within it non-fatal. .Sp .Vb 1 \& $object\->tap(\e"EVAL", sub {...}); .Ve .ie n .IP """\e""NO_EVAL""""" 4 .el .IP "\f(CW\e""NO_EVAL""\fR" 4 .IX Item """NO_EVAL""" Switches back to the default behaviour of not wrapping coderefs in \&\f(CW\*(C`eval\*(C'\fR. .Sp .Vb 6 \& $object\->tap( \& \e"EVAL", \& sub {...}, # any fatal errors will be caught and ignored \& \e"NO_EVAL", \& sub {...}, # fatal errors are properly fatal again. \& ); .Ve .SH BUGS .IX Header "BUGS" Please report any bugs to . .SH "SEE ALSO" .IX Header "SEE ALSO" Object::Tap, Object::Util. .PP Role::Commons. .PP , . .SH AUTHOR .IX Header "AUTHOR" Toby Inkster . .SH "COPYRIGHT AND LICENCE" .IX Header "COPYRIGHT AND LICENCE" This software is copyright (c) 2012, 2014 by Toby Inkster. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. .SH "DISCLAIMER OF WARRANTIES" .IX Header "DISCLAIMER OF WARRANTIES" THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.