3 our $VERSION = '1.001000';
4 $VERSION = eval $VERSION;
7 my ($invocant, $code) = splice @_, 0, 2;
9 sub { $invocant->$code(@args => @_) }
14 my ($method) = our $AUTOLOAD =~ /^curry::(.+)$/;
17 $invocant->$method(@args => @_);
26 my ($invocant, $code) = splice @_, 0, 2;
27 Scalar::Util::weaken($invocant) if length ref $invocant;
30 return unless defined $invocant;
31 $invocant->$code(@args => @_)
37 Scalar::Util::weaken($invocant) if length ref $invocant;
38 my ($method) = our $AUTOLOAD =~ /^curry::weak::(.+)$/;
41 return unless defined $invocant;
42 $invocant->$method(@args => @_);
50 curry - Create automatic curried method call closures for any class or object
56 my $code = $obj->curry::frobnicate('foo');
60 my $code = sub { $obj->frobnicate(foo => @_) };
66 my $code = $obj->curry::weak::frobnicate('foo');
71 Scalar::Util::weaken(my $weak_obj = $obj);
73 return unless $weak_obj; # in case it already went away
74 $weak_obj->frobnicate(foo => @_)
78 If you want to pass a weakened copy of an object to a coderef, use the
79 C< $weak > package variable:
83 my $code = $self->$curry::weak(sub {
84 my ($self, @args) = @_;
85 print "$self must still be alive, because we were called (with @args)\n";
88 which is much the same as:
92 my ($self, @args) = @_;
93 print "$self must still be alive, because we were called (with @args)\n";
95 Scalar::Util::weaken(my $weak_obj = $self);
97 return unless $weak_obj; # in case it already went away
98 $sub->($weak_obj, 'xyz', @_);
102 There's an equivalent - but somewhat less useful - C< $curry > package variable:
106 my $code = $self->$curry::curry(sub {
107 my ($self, $var) = @_;
108 print "The stashed value from our ->something method call was $var\n";
109 }, $self->something('complicated'));
111 Both of these methods can also be used if your scalar is a method name, rather
116 my $code = $self->$curry::curry($methodname, $self->something('complicated'));
120 How many times have you written
122 sub { $obj->something($some, $args, @_) }
124 or worse still needed to weaken it and had to check and re-check your code
125 to be sure you weren't closing over things the wrong way?
127 Right. That's why I wrote this.
131 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
135 None yet - maybe this software is perfect! (ahahahahahahahahaha)
139 Copyright (c) 2012 the curry L</AUTHOR> and L</CONTRIBUTORS>
144 This library is free software and may be distributed under the same terms