in reply to eval'ing coderef with ampersand

\q, \q() and \&q() first call the subroutine and then take a reference to its return value while \&q takes a reference to the subroutine itself.

Replies are listed 'Best First'.
Re^2: eval'ing coderef with ampersand
by hbm (Hermit) on Sep 07, 2010 at 13:13 UTC

    That is a subtle and significant difference. What I'm really doing is forking a list of processes; and I was alarmed that one process executed when I defined the list, prior to forking.

    Do you know where this is documented? I haven't seen it in perlref, my Cookbook, or elsewhere.

    Thanks!

      perlop says "Unary '\' creates a reference to whatever follows it." and refers you to perlref.

      perlref shows special that \&handler is the means of getting a reference to a function.

      \handler and \handler() are clearly not special cases, so the \ applies to the value returned by the expressions that follows. They could be written \( handler ) and \( handler() ).

      The behaviour of \&handler() is not documented, but there's only one parsing that wouldn't result in an error: \( &handler() ).