initial import of code for curry
[p5sagit/curry.git] / lib / curry.pm
1 package curry;
2
3 our $VERSION = '1.0';
4 $VERSION = eval $VERSION;
5
6 sub AUTOLOAD {
7   my $invocant = shift;
8   my ($method) = our $AUTOLOAD =~ /^curry::(.+)$/;
9   my @args = @_;
10   return sub {
11     $invocant->$method(@args => @_);
12   }
13 }
14
15 package curry::weak;
16
17 use Scalar::Util ();
18
19 sub AUTOLOAD {
20   my $invocant = shift;
21   Scalar::Util::weaken($invocant) if Scalar::Util::blessed($invocant);
22   my ($method) = our $AUTOLOAD =~ /^curry::(.+)$/;
23   my @args = @_;
24   return sub {
25     $invocant->$method(@args => @_);
26   }
27 }
28
29 1;
30
31 =head1 NAME
32
33 curry - Create automatic curried method call closures for any class or object
34
35 =head1 SYNOPSIS
36
37   use curry;
38
39   my $code = $obj->curry::frobnicate('foo');
40
41 is equivalent to:
42
43   my $code = sub { $obj->frobnicate(foo => @_) };
44
45 Additionally,
46
47   use curry::weak;
48
49   my $code = $obj->curry::weak::frobnicate('foo');
50
51 is equivalent to:
52
53   my $code = do {
54     Scalar::Util::weaken(my $weak_obj = $obj);
55     sub { $weak_obj->frobnicate(foo => @_) };
56   };
57
58 =head1 RATIONALE
59
60 How many times have you written
61
62   sub { $obj->something($some, $args, @_) }
63
64 or worse still needed to weaken it and had to check and re-check your code
65 to be sure you weren't closing over things the wrong way?
66
67 Right. That's why I wrote this.
68
69 =head1 AUTHOR
70
71 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
72
73 =head1 CONTRIBUTORS
74
75 None yet - maybe this software is perfect! (ahahahahahahahahaha)
76
77 =head1 COPYRIGHT
78
79 Copyright (c) 2012 the Import::Into L</AUTHOR> and L</CONTRIBUTORS>
80 as listed above.
81
82 =head1 LICENSE
83
84 This library is free software and may be distributed under the same terms
85 as perl itself.