fix package name for curry::weak AUTOLOAD
[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);
ee6aa763 22 my ($method) = our $AUTOLOAD =~ /^curry::weak::(.+)$/;
11d28ce0 23 my @args = @_;
24 return sub {
cfc04a37 25 return unless $invocant;
11d28ce0 26 $invocant->$method(@args => @_);
27 }
28}
29
301;
31
32=head1 NAME
33
34curry - Create automatic curried method call closures for any class or object
35
36=head1 SYNOPSIS
37
38 use curry;
39
40 my $code = $obj->curry::frobnicate('foo');
41
42is equivalent to:
43
44 my $code = sub { $obj->frobnicate(foo => @_) };
45
46Additionally,
47
48 use curry::weak;
49
50 my $code = $obj->curry::weak::frobnicate('foo');
51
52is equivalent to:
53
54 my $code = do {
55 Scalar::Util::weaken(my $weak_obj = $obj);
cfc04a37 56 sub {
57 return unless $weak_obj; # in case it already went away
58 $weak_obj->frobnicate(foo => @_)
59 };
11d28ce0 60 };
61
62=head1 RATIONALE
63
64How many times have you written
65
66 sub { $obj->something($some, $args, @_) }
67
68or worse still needed to weaken it and had to check and re-check your code
69to be sure you weren't closing over things the wrong way?
70
71Right. That's why I wrote this.
72
73=head1 AUTHOR
74
75mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
76
77=head1 CONTRIBUTORS
78
79None yet - maybe this software is perfect! (ahahahahahahahahaha)
80
81=head1 COPYRIGHT
82
83Copyright (c) 2012 the Import::Into L</AUTHOR> and L</CONTRIBUTORS>
84as listed above.
85
86=head1 LICENSE
87
88This library is free software and may be distributed under the same terms
89as perl itself.