| 1 | package Object::Tap; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use base qw(Exporter); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | our $VERSION = '1.000003'; # 1.0.3 | |
| 8 | |||
| 9 | 1 | our @EXPORT = qw($_tap); | |
| 10 | |||
| 11 | 2 | ▶ | our $_tap = sub { my ($obj, $call, @args) = @_; $obj->$call(@args); $obj }; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 1 | 1; | |
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Object::Tap - Tap into a series of method calls to alter an object | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | Instead of writing - | ||
| 22 | |||
| 23 | my $thing = My::Class->new(...); | ||
| 24 | |||
| 25 | $thing->set_foo(1); | ||
| 26 | |||
| 27 | you can instead write - | ||
| 28 | |||
| 29 | use Object::Tap; | ||
| 30 | |||
| 31 | my $thing = My::Class->new(...)->$_tap(sub { $_[0]->set_foo(1) }); | ||
| 32 | |||
| 33 | To realise why this might be useful, consider instead - | ||
| 34 | |||
| 35 | My::App->new(...)->$_tap(...)->run; | ||
| 36 | |||
| 37 | where a variable is thereby not required at all. | ||
| 38 | |||
| 39 | You can also pass extra args - | ||
| 40 | |||
| 41 | $obj->$_tap(sub { warn "Got arg: $_[1]" }, 'arg'); | ||
| 42 | |||
| 43 | or use a method name instead of a sub ref - | ||
| 44 | |||
| 45 | my $thing = My::Class->new(...)->$_tap(set_foo => 1); | ||
| 46 | |||
| 47 | For a 'real' example of how that might be used, one could create and | ||
| 48 | initialize an L<HTML::TableExtract> object in one go using - | ||
| 49 | |||
| 50 | my $te = HTML::TableExtract->new->$_tap(parse => $html); | ||
| 51 | |||
| 52 | =head1 AUTHOR | ||
| 53 | |||
| 54 | mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk> | ||
| 55 | |||
| 56 | =head1 CONTRIBUTORS | ||
| 57 | |||
| 58 | None yet. Well volunteered? :) | ||
| 59 | |||
| 60 | =head1 COPYRIGHT | ||
| 61 | |||
| 62 | Copyright (c) 2014 the Object::Tap L</AUTHOR> and L</CONTRIBUTORS> | ||
| 63 | as listed above. | ||
| 64 | |||
| 65 | =head1 LICENSE | ||
| 66 | |||
| 67 | This library is free software and may be distributed under the same terms | ||
| 68 | as perl itself. | ||
| 69 | |||
| 70 | =cut | ||