What does () execution do in a routine definition in Perl?

The following code is taken directly from the source of the Tie :: File module. What do empty parentheses do in the O_ACCMODE definition in this context? I know what prototype subroutines are used for, but this use does not seem to be related to this.

use Fcntl 'O_CREAT', 'O_RDWR', 'LOCK_EX', 'LOCK_SH', 'O_WRONLY', 'O_RDONLY'; sub O_ACCMODE () { O_RDONLY | O_RDWR | O_WRONLY } 
+4
source share
3 answers

From perlsub on the topic of persistent functions:

Functions with prototype (): potential candidates for inlining

+11
source

It also tells the parser that O_ACCMODE does not accept an argument under any conditions (other than &O_ACCMODE() , which you probably will never have to think about). This makes him act like most people expect constant.

As a quick example, in:

 sub FOO { 1 } sub BAR { 2 } print FOO + BAR; 

the final line is parsed as print FOO(+BAR()) , and the printed value is 1, because when the prototype sub is called without parsers, it tries to act as listop and slurp members as far as possible.

IN:

 sub FOO () { 1 } sub BAR () { 2 } print FOO + BAR; 

The last line parses as print FOO() + BAR() , and the printed value is 3, because the prototype () tells the parser that the FOO arguments are not expected or not valid.

+12
source

The prototype () makes the subroutine suitable for inline. This is used, for example, by the constant pragma.

See Constant Functions in perlsub.

+7
source

All Articles