Domanda

Every now and then I see Perl scripts where subroutines are called with a leading '&'. Is this legacy, or does it give any benefit? As calling the subroutine without the ampersand sign works as well.

sub mysub {
        print "mysub\n"; 

}

mysub;
&mysub;

Thx/Hermann

È stato utile?

Soluzione

Calling with & is generally a code smell that somebody doesn't know what they're doing and are in a Perl4 mindset. In your specific example, it works exactly the same. However, calling with & disables function prototypes, so advanced users may use it in certain circumstances. You should expect to see a comment why next to the call in that case.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top