From: Matt S Trout Date: Fri, 3 Sep 2021 10:17:37 +0000 (+0000) Subject: Support curry::_ and curry::weak::_ X-Git-Tag: v2.000000~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=51f11f71fa32099ecaa44a91ef4d0ed8f67f0bbe;p=p5sagit%2Fcurry.git Support curry::_ and curry::weak::_ --- diff --git a/Changes b/Changes index 6b8bab3..da3d8a8 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ Revision history for curry + - Support curry::_ and curry::weak::_ + 1.001000 - 2017-06-23 - Support $curry::curry and $curry::weak diff --git a/lib/curry.pm b/lib/curry.pm index 9fd6559..808e82c 100644 --- a/lib/curry.pm +++ b/lib/curry.pm @@ -9,6 +9,8 @@ our $curry = sub { sub { $invocant->$code(@args => @_) } }; +sub curry::_ { &$curry } + sub AUTOLOAD { my $invocant = shift; my ($method) = our $AUTOLOAD =~ /^curry::(.+)$/; @@ -32,6 +34,8 @@ $curry::weak = sub { } }; +sub curry::_ { &$curry::weak } + sub AUTOLOAD { my $invocant = shift; Scalar::Util::weaken($invocant) if length ref $invocant; @@ -59,6 +63,10 @@ is equivalent to: 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; @@ -75,45 +83,14 @@ is equivalent to: }; }; -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 and C 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 diff --git a/t/curry.t b/t/curry.t index d643f88..88e093e 100644 --- a/t/curry.t +++ b/t/curry.t @@ -16,6 +16,10 @@ my $foo = Foo->new; 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); @@ -35,6 +39,10 @@ $curry = $foo->curry::weak::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);