File Coverage

blib/lib/Object/Tap.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Object::Tap;
2              
3 1     1   24511 use strict;
  1         2  
  1         38  
4 1     1   5 use warnings;
  1         2  
  1         33  
5 1     1   6 use base qw(Exporter);
  1         6  
  1         213  
6              
7             our $VERSION = '1.000003'; # 1.0.3
8              
9             our @EXPORT = qw($_tap);
10              
11             our $_tap = sub { my ($obj, $call, @args) = @_; $obj->$call(@args); $obj };
12              
13             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 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)
55              
56             =head1 CONTRIBUTORS
57              
58             None yet. Well volunteered? :)
59              
60             =head1 COPYRIGHT
61              
62             Copyright (c) 2014 the Object::Tap L and L
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