[Melbourne-pm] Perl operations that accept a block, vs coderefs

Toby Wintermute tjc at wintrmute.net
Mon Jul 7 20:05:37 PDT 2014


Hi,
I haven't kept up with new Perl releases as well as I should.. I've
skimmed the deltas, but I wondered if I've missed any improvements
(upcoming or existing) to operators that accept blocks or expressions,
to accept code references?

Eg. It would be nice if things like this simple example worked:

use Method::Signatures;
func multiply_by($amount) {
  func($x) {
    $x * $amount
  }
}

my @input = (1..4);
map multiply_by(2), @input;

# Should return 2, 4, 6, 8
# Unfortunately, it currently returns four copies of the coderef
rather than executing it.


You can kludge it by doing:
  map multiply_by(2)->($_), @input;
(or worse:  map { multiple_by(2)->($_) } @input )

but this is (a) ugly, and (b) unnecessarily re-executes the builder
function (multiply_by) on every iteration :(


-Toby


More information about the Melbourne-pm mailing list