Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top