On Moose default variables

If you read my last post, I was wondering why Moose wasn't accepting array or hash references as default values that could be cloned and, instead, used a code reference to create a new array/hash.

Decided to benchmark the two approaches. The results were... surprising:

Benchmark: timing 500000 iterations of clone, retfunc...
clone: 34 wallclock secs (32.35 usr + 0.12 sys = 32.47 CPU) @ 15398.83/s (n=500000)
retfunc: 4 wallclock secs ( 3.75 usr + 0.02 sys = 3.77 CPU) @ 132625.99/s (n=500000)

With these results, I think the correct behavior is the one already present on Moose, complaining about the default value being an array reference and suggesting an alternative:

References are not allowed as default values, you must wrap the default of 'a' in a CODE reference (ex: sub { [] } and not [])

1 Comment


Thanks for doing the benchmarking on this. I personally found it useful and of course achieve closure on the questions you previously raised.

John Napiorkowski

Leave a comment

About Alberto Simões

user-pic I blog about Perl. D'uh!