From: Matt S Trout Date: Mon, 30 Jul 2012 11:35:21 +0000 (+0000) Subject: initial import of code for curry X-Git-Tag: v1.000000~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=11d28ce06a6a4dc20c0a22e663a88dcd66f408a9;p=p5sagit%2Fcurry.git initial import of code for curry --- 11d28ce06a6a4dc20c0a22e663a88dcd66f408a9 diff --git a/lib/curry.pm b/lib/curry.pm new file mode 100644 index 0000000..c98eab5 --- /dev/null +++ b/lib/curry.pm @@ -0,0 +1,85 @@ +package curry; + +our $VERSION = '1.0'; +$VERSION = eval $VERSION; + +sub AUTOLOAD { + my $invocant = shift; + my ($method) = our $AUTOLOAD =~ /^curry::(.+)$/; + my @args = @_; + return sub { + $invocant->$method(@args => @_); + } +} + +package curry::weak; + +use Scalar::Util (); + +sub AUTOLOAD { + my $invocant = shift; + Scalar::Util::weaken($invocant) if Scalar::Util::blessed($invocant); + my ($method) = our $AUTOLOAD =~ /^curry::(.+)$/; + my @args = @_; + return sub { + $invocant->$method(@args => @_); + } +} + +1; + +=head1 NAME + +curry - Create automatic curried method call closures for any class or object + +=head1 SYNOPSIS + + use curry; + + my $code = $obj->curry::frobnicate('foo'); + +is equivalent to: + + my $code = sub { $obj->frobnicate(foo => @_) }; + +Additionally, + + use curry::weak; + + my $code = $obj->curry::weak::frobnicate('foo'); + +is equivalent to: + + my $code = do { + Scalar::Util::weaken(my $weak_obj = $obj); + sub { $weak_obj->frobnicate(foo => @_) }; + }; + +=head1 RATIONALE + +How many times have you written + + sub { $obj->something($some, $args, @_) } + +or worse still needed to weaken it and had to check and re-check your code +to be sure you weren't closing over things the wrong way? + +Right. That's why I wrote this. + +=head1 AUTHOR + +mst - Matt S. Trout (cpan:MSTROUT) + +=head1 CONTRIBUTORS + +None yet - maybe this software is perfect! (ahahahahahahahahaha) + +=head1 COPYRIGHT + +Copyright (c) 2012 the Import::Into L and L +as listed above. + +=head1 LICENSE + +This library is free software and may be distributed under the same terms +as perl itself. diff --git a/lib/curry/weak.pm b/lib/curry/weak.pm new file mode 100644 index 0000000..4f41a7e --- /dev/null +++ b/lib/curry/weak.pm @@ -0,0 +1,5 @@ +package curry::weak; + +use curry; + +1;