View on
MetaCPAN
Matt S Trout > curry > curry

Download:
curry-1.001000.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  1
Open  1
View/Report Bugs
Module Version: 1.001000   Source  

NAME ^

curry - Create automatic curried method call closures for any class or object

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 {
      return unless $weak_obj; # in case it already went away
      $weak_obj->frobnicate(foo => @_)
    };
  };

If you want to pass a weakened copy of an object to a coderef, use the $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 - $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.

 use curry;

 my $code = $self->$curry::curry($methodname, $self->something('complicated'));

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.

AUTHOR ^

mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>

CONTRIBUTORS ^

None yet - maybe this software is perfect! (ahahahahahahahahaha)

COPYRIGHT ^

Copyright (c) 2012 the curry "AUTHOR" and "CONTRIBUTORS" as listed above.

LICENSE ^

This library is free software and may be distributed under the same terms as perl itself.

syntax highlighting: