1curry(3) User Contributed Perl Documentation curry(3)
2
3
4
6 curry - Create automatic curried method call closures for any class or
7 object
8
10 use curry;
11
12 my $code = $obj->curry::frobnicate('foo');
13
14 is equivalent to:
15
16 my $code = sub { $obj->frobnicate(foo => @_) };
17
18 Additionally,
19
20 use curry::weak;
21
22 my $code = $obj->curry::weak::frobnicate('foo');
23
24 is equivalent to:
25
26 my $code = do {
27 Scalar::Util::weaken(my $weak_obj = $obj);
28 sub {
29 return unless $weak_obj; # in case it already went away
30 $weak_obj->frobnicate(foo => @_)
31 };
32 };
33
34 If you want to pass a weakened copy of an object to a coderef, use the
35 $weak package variable:
36
37 use curry::weak;
38
39 my $code = $self->$curry::weak(sub {
40 my ($self, @args) = @_;
41 print "$self must still be alive, because we were called (with @args)\n";
42 }, 'xyz');
43
44 which is much the same as:
45
46 my $code = do {
47 my $sub = sub {
48 my ($self, @args) = @_;
49 print "$self must still be alive, because we were called (with @args)\n";
50 };
51 Scalar::Util::weaken(my $weak_obj = $self);
52 sub {
53 return unless $weak_obj; # in case it already went away
54 $sub->($weak_obj, 'xyz', @_);
55 }
56 };
57
58 There's an equivalent - but somewhat less useful - $curry package
59 variable:
60
61 use curry;
62
63 my $code = $self->$curry::curry(sub {
64 my ($self, $var) = @_;
65 print "The stashed value from our ->something method call was $var\n";
66 }, $self->something('complicated'));
67
68 Both of these methods can also be used if your scalar is a method name,
69 rather than a coderef.
70
71 use curry;
72
73 my $code = $self->$curry::curry($methodname, $self->something('complicated'));
74
76 How many times have you written
77
78 sub { $obj->something($some, $args, @_) }
79
80 or worse still needed to weaken it and had to check and re-check your
81 code to be sure you weren't closing over things the wrong way?
82
83 Right. That's why I wrote this.
84
86 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
87
89 None yet - maybe this software is perfect! (ahahahahahahahahaha)
90
92 Copyright (c) 2012 the curry "AUTHOR" and "CONTRIBUTORS" as listed
93 above.
94
96 This library is free software and may be distributed under the same
97 terms as perl itself.
98
99
100
101perl v5.28.1 2017-06-23 curry(3)