sub { $invocant->$code(@args => @_) }
};
+sub curry::_ { &$curry }
+
sub AUTOLOAD {
my $invocant = shift;
my ($method) = our $AUTOLOAD =~ /^curry::(.+)$/;
}
};
+sub curry::_ { &$curry::weak }
+
sub AUTOLOAD {
my $invocant = shift;
Scalar::Util::weaken($invocant) if length ref $invocant;
my $code = sub { $obj->frobnicate(foo => @_) };
+If you have a method name (or a coderef), you can call (as of version 2):
+
+ my $code = $obj->curry::_($method => 'foo');
+
Additionally,
use curry::weak;
};
};
-If you want to pass a weakened copy of an object to a coderef, use the
-C< $weak > package variable:
-
- use curry::weak;
-
- my $code = $self->$curry::weak(sub {
- my ($self, @args) = @_;
- print "$self must still be alive, because we were called (with @args)\n";
- }, 'xyz');
-
-which is much the same as:
-
- my $code = do {
- my $sub = sub {
- my ($self, @args) = @_;
- print "$self must still be alive, because we were called (with @args)\n";
- };
- Scalar::Util::weaken(my $weak_obj = $self);
- sub {
- return unless $weak_obj; # in case it already went away
- $sub->($weak_obj, 'xyz', @_);
- }
- };
-
-There's an equivalent - but somewhat less useful - C< $curry > package variable:
-
- use curry;
-
- my $code = $self->$curry::curry(sub {
- my ($self, $var) = @_;
- print "The stashed value from our ->something method call was $var\n";
- }, $self->something('complicated'));
-
-Both of these methods can also be used if your scalar is a method name, rather
-than a coderef.
+Similarly, given a method name or coderef (as of version 2):
- use curry;
+ my $code = $obj->curry::weak::_($method => 'foo');
- my $code = $self->$curry::curry($methodname, $self->something('complicated'));
+There are also C<$curry::curry> and C<$curry::weak> globals that work
+equivalently to C<curry::_> and C<curry::weak::_> respectively - you'll
+quite possibly see them in existing code because they were provided in
+pre-2.0 versions but they're unlikely to be the best option for new code.
=head1 RATIONALE
is_deeply($foo->foo(1), [ $foo, 1 ], 'Direct object call');
is_deeply($foo->curry::foo->(1), [ $foo, 1 ], 'Curried object call');
+is_deeply(
+ $foo->curry::_('foo')->(1), [ $foo, 1 ],
+ 'Curried string method call'
+);
weaken(my $weak_foo = $foo);
is_deeply($curry->(1), [ $foo, 1 ], 'Curried weak object call');
+my $curry2 = $foo->curry::weak::_('foo');
+
+is_deeply($curry->(1), [ $foo, 1 ], 'Curried weak string method call');
+
weaken($weak_foo = $foo);
undef($foo);