in reply to Re^2: Dereferencing code refs with & vs. ->()
in thread Dereferencing code refs with & vs. ->()

The use of &foo in \&foo has nothing to do with dereferencing. You're not calling foo.

Also, in goto &$bar; you aren't derefencing - it's the magical goto that jumps to the subroutine pointed to by $bar. If you were derefencing, you'd jump to the label that was returned by &$bar;. And that's what's happening if you do goto $bar->();. Or goto &$bar();.

Another reason to use the ampersand version is to disable prototype checking, for those subroutines that are evil enough to use it without good reason.

But that's a difference between foo() and &foo(). Again, that has nothing to do with dereferencing. It can't even do prototype checking when doing dereferencing, as prototype checking is done at compile time. The following code runs without errors - despite violent prototype violations:

!/usr/bin/perl use strict; use warnings; my $ref = sub ($) {print "Hello, world\n"}; &$ref(); $ref->(); __END__
Neither the &$ref();, nor the $ref->(); is checked for its prototype.

Replies are listed 'Best First'.
Re^4: Dereferencing code refs with & vs. ->()
by ikegami (Patriarch) on Sep 23, 2005 at 15:11 UTC
    He knows they have nothing to do with dereferencing. He was contradicting the following completely false statement (which had nothing to do with dereferencing):
    About the only place you'd need the ampersand version is if you're trying to get the current scope's @_ passed a la &somesub;. But that's really rare.

      However the OP was bout dereferencing and given that context it is about the only place you have to use an ampersand. Taking a reference to a named sub has nothing to do with using & versus -> in dereferencing and calling a coderef. The point about goto &$coderef is correct (and that would be one of the exceptions that I was alluding to by qualifying with about), but as the AM points out prototype checking doesn't apply to coderefs so that "counterexample" is also not really relevant. Given the original context the statement is not "completely false".