| 1 | package curry; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | our $VERSION = '2.000001'; | |||||||||
| 4 | 3 | $VERSION = eval $VERSION; | |||||||||
| 5 | |||||||||||
| 6 | 4 | ▶ | our $curry = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | my ($invocant, $code) = splice @_, 0, 2; | |||||||||
| 8 | 4 | my @args = @_; | |||||||||
| 9 | 7 | ▶ | sub { $invocant->$code(@args => @_) } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | }; | |||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | sub curry::_ { &$curry } | ||||||||
|
Subroutine
_: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 2 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | my $invocant = shift; | |||||||||
| 16 | 2 | my ($method) = our $AUTOLOAD =~ /^curry::(.+)$/; | |||||||||
| 17 | 2 | my @args = @_; | |||||||||
| 18 | 1 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | $invocant->$method(@args => @_); | |||||||||
| 20 | } | ||||||||||
| 21 | 2 | } | |||||||||
| 22 | |||||||||||
| 23 | package curry::weak; | ||||||||||
| 24 | |||||||||||
| 25 | 3 | ▶ | use Scalar::Util (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 3 | ▶ | $curry::weak = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 28 | 3 | my ($invocant, $code) = splice @_, 0, 2; | |||||||||
| 29 | 3 | ▶ | Scalar::Util::weaken($invocant) if length ref $invocant; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 3 | my @args = @_; | |||||||||
| 31 | 5 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 32 | 5 | ▶ | return unless defined $invocant; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 3 | $invocant->$code(@args => @_) | |||||||||
| 34 | } | ||||||||||
| 35 | 3 | }; | |||||||||
| 36 | |||||||||||
| 37 | 1 | ▶ | sub curry::weak::_ { &$curry::weak } | ||||||||
|
Subroutine
_: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | 1 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=6.9
| |||||||||||
| 40 | 1 | my $invocant = shift; | |||||||||
| 41 | 1 | ▶ | Scalar::Util::weaken($invocant) if length ref $invocant; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 1 | my ($method) = our $AUTOLOAD =~ /^curry::weak::(.+)$/; | |||||||||
| 43 | 1 | my @args = @_; | |||||||||
| 44 | 2 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 45 | 2 | ▶ | return unless defined $invocant; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 1 | $invocant->$method(@args => @_); | |||||||||
| 47 | } | ||||||||||
| 48 | 1 | } | |||||||||
| 49 | |||||||||||
| 50 | 3 | 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. | ||||||||||