in reply to Re: On quoting the lhs of '=>'
in thread On quoting the lhs of '=>'

Instead of non-working '+' or offending '()', you could use '&'

That's evil, I love you ;-)

Note that this has an interesting side effect that you'll never be able to observe:

use constant foo => 'bar'; sub crack { print &foo, $/; } crack(1, 2, 3);

constant creates a sub foo () { 'bar' }, that is a constant sub with empty prototype.

Using the & in front bypasses the prototype check, and it passes all arguments of sub crack to sub foo.

Now since foo is constant sub, you have no way of observing the difference - except for speed:

use Benchmark qw(cmpthese); cmpthese(-1, { 'ampersand' => 'use constant foo => 2; my $x = 0; for (1..10000) { + $x += &foo } ', 'no_amp' => 'use constant foo => 2; my $x = 0; for (1..10000) { + $x += foo } ', }); __END__ Rate ampersand no_amp ampersand 570/s -- -39% no_amp 939/s 65% --

You can see that number of passed arguments makes a difference with this small benchmark:

cmpthese(-1, { 'ampersand' => ' use constant foo => 2; sub a { my $x = 0; for (1..10000) { $x += &foo } } a(%ENV, %ENV, %ENV) ', 'no_amp' => ' use constant foo => 2; sub a { my $x = 0; for (1..10000) { $x += foo } } a(%ENV, %ENV, %ENV) ', }); __END__ Rate ampersand no_amp ampersand 330/s -- -61% no_amp 849/s 157% --

Passing a relatively large array to the sub increase the run time difference.

Replies are listed 'Best First'.
Re^3: On quoting the lhs of '=>'
by parv (Parson) on Jul 09, 2008 at 12:28 UTC

    Thanks moritz for pointing out the speed difference which I had never considered before (not that I use & for sub calls myself).

    If speed really matters, then only two solutions remain (at least known to me yet) for OP to invoke the sub (created by constant pragma) in presence of left argument quoting comma: P(), (P) (where P is some sub).

    Time passes ... cdarke adds other options for invoking a sub in presence of fat comma.