File Coverage

blib/lib/curry.pm
Criterion Covered Total %
statement 16 16 100.0
branch 3 4 75.0
condition n/a
subroutine 6 6 100.0
pod n/a
total 25 26 96.1


line stmt bran cond sub pod time code
1             package curry;
2              
3             our $VERSION = '2.000000';
4             $VERSION = eval $VERSION;
5              
6             our $curry = sub {
7             my ($invocant, $code) = splice @_, 0, 2;
8             my @args = @_;
9             sub { $invocant->$code(@args => @_) }
10             };
11              
12             sub curry::_ { &$curry }
13              
14             sub AUTOLOAD {
15 2     2   1744 my $invocant = shift;
16 2         13 my ($method) = our $AUTOLOAD =~ /^curry::(.+)$/;
17 2         5 my @args = @_;
18             return sub {
19 1     1   4 $invocant->$method(@args => @_);
20             }
21 2         12 }
22              
23             package curry::weak;
24              
25 3     3   156048 use Scalar::Util ();
  3         18  
  3         857  
26              
27             $curry::weak = sub {
28             my ($invocant, $code) = splice @_, 0, 2;
29             Scalar::Util::weaken($invocant) if length ref $invocant;
30             my @args = @_;
31             sub {
32             return unless defined $invocant;
33             $invocant->$code(@args => @_)
34             }
35             };
36              
37 1     1   642 sub curry::_ { &$curry::weak }
38              
39             sub AUTOLOAD {
40 2     2   1273 my $invocant = shift;
41 2 50       11 Scalar::Util::weaken($invocant) if length ref $invocant;
42 2         14 my ($method) = our $AUTOLOAD =~ /^curry::weak::(.+)$/;
43 2         5 my @args = @_;
44             return sub {
45 3 100   3   918 return unless defined $invocant;
46 2         8 $invocant->$method(@args => @_);
47             }
48 2         10 }
49              
50             1;
51              
52             =head1 NAME
53              
54             curry - Create automatic curried method call closures for any class or object
55              
56             =head1 SYNOPSIS
57              
58             use curry;
59              
60             my $code = $obj->curry::frobnicate('foo');
61              
62             is equivalent to:
63              
64             my $code = sub { $obj->frobnicate(foo => @_) };
65              
66             If you have a method name (or a coderef), you can call (as of version 2):
67              
68             my $code = $obj->curry::_($method => 'foo');
69              
70             Additionally,
71              
72             use curry::weak;
73              
74             my $code = $obj->curry::weak::frobnicate('foo');
75              
76             is equivalent to:
77              
78             my $code = do {
79             Scalar::Util::weaken(my $weak_obj = $obj);
80             sub {
81             return unless $weak_obj; # in case it already went away
82             $weak_obj->frobnicate(foo => @_)
83             };
84             };
85              
86             Similarly, given a method name or coderef (as of version 2):
87              
88             my $code = $obj->curry::weak::_($method => 'foo');
89              
90             There are also C<$curry::curry> and C<$curry::weak> globals that work
91             equivalently to C and C respectively - you'll
92             quite possibly see them in existing code because they were provided in
93             pre-2.0 versions but they're unlikely to be the best option for new code.
94              
95             =head1 RATIONALE
96              
97             How many times have you written
98              
99             sub { $obj->something($some, $args, @_) }
100              
101             or worse still needed to weaken it and had to check and re-check your code
102             to be sure you weren't closing over things the wrong way?
103              
104             Right. That's why I wrote this.
105              
106             =head1 AUTHOR
107              
108             mst - Matt S. Trout (cpan:MSTROUT)
109              
110             =head1 CONTRIBUTORS
111              
112             None yet - maybe this software is perfect! (ahahahahahahahahaha)
113              
114             =head1 COPYRIGHT
115              
116             Copyright (c) 2012 the curry L and L
117             as listed above.
118              
119             =head1 LICENSE
120              
121             This library is free software and may be distributed under the same terms
122             as perl itself.