Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does () mean at the end of Perl subroutine?

Tags:

perl

sub f {
    # some code here
    () 
}

What does () mean in this Perl subroutine?

like image 304
mhd Avatar asked Jun 07 '13 11:06

mhd


2 Answers

The last expression in a sub will be the return value. This ensures that (assuming no previous return statements) the sub returns an empty list (rather then whatever was on the previous line of code).

like image 133
Quentin Avatar answered Oct 15 '22 09:10

Quentin


OK ... so it is perhaps pathological, but this IS Perl we're talking about...

Depending on the actual text of "# some code here", it could conceivably produce a dereferenced CODE reference, in which case the parens would cause the CODE to be invoked with zero arguments, and the return value of that code would be the return value of `f'.

For example, the following will print out a single lowercase "a":

    sub f {
        &{sub { return $_[0] }}
       (@_)
    }

    print f(qw( a b c d e f )), "\n";
like image 20
Mylo Stone Avatar answered Oct 15 '22 09:10

Mylo Stone