/usr/share/perl5/Role/Commons/Tap.pm is in librole-commons-perl 0.104-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | use 5.008;
use strict;
use warnings;
package Role::Commons::Tap;
use Carp qw[croak];
BEGIN {
use Moo::Role;
$Role::Commons::Tap::AUTHORITY = 'cpan:TOBYINK';
$Role::Commons::Tap::VERSION = '0.104';
}
our $setup_for_class = sub {
my ($role, $package, %args) = @_;
return 0;
};
sub tap
{
my $self = shift;
my %flags;
PARAM: while (@_)
{
my $next = shift;
if (ref($next) eq 'CODE' or not ref $next)
{
my $args = ref($_[0]) eq 'ARRAY' ? shift : [];
my $code = ref $next ? $next : sub { $self->$next(@_) };
if ($flags{ EVAL })
{
local $_ = $self;
eval { $code->(@$args) }
}
else
{
local $_ = $self;
do { $code->(@$args) }
}
next PARAM;
}
if (ref($next) eq 'SCALAR')
{
if ($$next =~ m{^(no_?)?(.+)$}i)
{
$flags{ uc $2 } = $1 ? 0 : 1;
next PARAM;
}
}
croak qq/Unsupported parameter to tap: $next/;
}
return $self;
}
1;
__END__
=head1 NAME
Role::Commons::Tap - an object method which helps with chaining, inspired by Ruby
=head1 SYNOPSIS
# 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');
=head1 DESCRIPTION
B<< DO NOT USE THIS MODULE! >> Use L<Object::Tap> or L<Object::Util>
instead. They are not drop-in replacements, but a far more sensible way
to have a C<tap> method.
This module has nothing to do with the Test Anything Protocol (TAP, see
L<Test::Harness>).
This module is a role for your class, providing it with a C<tap> method.
The C<tap> 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<bar> and C<baz> methods
return, because C<tap> always returns its invocant.
The C<tap> 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> - which is described below.
=head2 C<< tap(@arguments) >>
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.
=head3 Coderef arguments
An argument that is a coderef (or a blessed argument that overloads
C<< &{} >> - see L<overload>) will be executed in a context where
C<< $_ >> has been set to the invocant of the tap method C<tap>. 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 "MY::CLASS" and then print "My::Class".
Because each argument to C<tap> is processed in order, you can provide
multiple coderefs:
print My::Class->tap(
sub { warn uc $_; return 'X' },
sub { warn lc $_; return 'Y' },
);
=head3 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' );
=head3 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'],
);
=head3 Scalar ref arguments
There are a handful of special scalar ref arguments that are supported:
=over
=item C<< \"EVAL" >>
This indicates that you wish for all subsequent coderefs to be wrapped in
an C<eval>, making any errors that occur within it non-fatal.
$object->tap(\"EVAL", sub {...});
=item C<< \"NO_EVAL" >>
Switches back to the default behaviour of not wrapping coderefs in
C<eval>.
$object->tap(
\"EVAL",
sub {...}, # any fatal errors will be caught and ignored
\"NO_EVAL",
sub {...}, # fatal errors are properly fatal again.
);
=back
=head1 BUGS
Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=Role-Commons>.
=head1 SEE ALSO
L<Object::Tap>, L<Object::Util>.
L<Role::Commons>.
L<http://tea.moertel.com/articles/2007/02/07/ruby-1-9-gets-handy-new-method-object-tap>,
L<http://prepan.org/module/3Yz7PYrBLN>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2012, 2014 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.
=head1 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.
|