Bumping version to 2.000001
[p5sagit/curry.git] / lib / curry.pm
1 package curry;
2
3 our $VERSION = '2.000001';
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   my $invocant = shift;
16   my ($method) = our $AUTOLOAD =~ /^curry::(.+)$/;
17   my @args = @_;
18   return sub {
19     $invocant->$method(@args => @_);
20   }
21 }
22
23 package curry::weak;
24
25 use Scalar::Util ();
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 sub curry::weak::_ { &$curry::weak }
38
39 sub AUTOLOAD {
40   my $invocant = shift;
41   Scalar::Util::weaken($invocant) if length ref $invocant;
42   my ($method) = our $AUTOLOAD =~ /^curry::weak::(.+)$/;
43   my @args = @_;
44   return sub {
45     return unless defined $invocant;
46     $invocant->$method(@args => @_);
47   }
48 }
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<curry::_> and C<curry::weak::_> 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) <mst@shadowcat.co.uk>
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</AUTHOR> and L</CONTRIBUTORS>
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.