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