X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Fcurry.pm;h=808e82cbb4e1e1ea006bd623cb2dd155d17b6fc1;hb=51f11f71fa32099ecaa44a91ef4d0ed8f67f0bbe;hp=9fd65593e535109a0fff091b9773a5292d119e3f;hpb=659c1f82a0962a89516344263b6b87990cd11371;p=p5sagit%2Fcurry.git 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