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