mhd
mhd

Reputation: 545

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

sub f {
    # some code here
    () 
}

What does () mean in this Perl subroutine?

Upvotes: 9

Views: 269

Answers (2)

Mylo Stone
Mylo Stone

Reputation: 309

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";

Upvotes: 1

Quentin
Quentin

Reputation: 943220

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).

Upvotes: 16

Related Questions