Support curry::_ and curry::weak::_
[p5sagit/curry.git] / lib / curry.pm
index 7f35f6c..808e82c 100644 (file)
@@ -1,8 +1,16 @@
 package curry;
 
-our $VERSION = '1.0';
+our $VERSION = '1.001000';
 $VERSION = eval $VERSION;
 
+our $curry = sub {
+  my ($invocant, $code) = splice @_, 0, 2;
+  my @args = @_;
+  sub { $invocant->$code(@args => @_) }
+};
+
+sub curry::_ { &$curry }
+
 sub AUTOLOAD {
   my $invocant = shift;
   my ($method) = our $AUTOLOAD =~ /^curry::(.+)$/;
@@ -16,13 +24,25 @@ package curry::weak;
 
 use Scalar::Util ();
 
+$curry::weak = sub {
+  my ($invocant, $code) = splice @_, 0, 2;
+  Scalar::Util::weaken($invocant) if length ref $invocant;
+  my @args = @_;
+  sub {
+    return unless defined $invocant;
+    $invocant->$code(@args => @_)
+  }
+};
+
+sub curry::_ { &$curry::weak }
+
 sub AUTOLOAD {
   my $invocant = shift;
-  Scalar::Util::weaken($invocant) if Scalar::Util::blessed($invocant);
+  Scalar::Util::weaken($invocant) if length ref $invocant;
   my ($method) = our $AUTOLOAD =~ /^curry::weak::(.+)$/;
   my @args = @_;
   return sub {
-    return unless $invocant;
+    return unless defined $invocant;
     $invocant->$method(@args => @_);
   }
 }
@@ -43,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;
@@ -59,6 +83,15 @@ is equivalent to:
     };
   };
 
+Similarly, given a method name or coderef (as of version 2):
+
+  my $code = $obj->curry::weak::_($method => 'foo');
+
+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
 
 How many times have you written