Reputation: 417
I am writing code for work and have run into an interesting issue. I have searched my go-to resources on the topic and have not found a clear answer.
When writing a sub function in a Perl module, is a semicolon required after the closing brace?
As in:
sub printFoo {
print "foo";
};
Is this somehow different than a sub function in a regular Perl script?
When I use the above function in a module without the semicolon I get an error referencing an "undefined subroutine."
I feel confident that in the past I have used similar pieces of code without the semicolon following the closing brace but now I am no longer positive.
A push in the right direction would be great!
Upvotes: 1
Views: 671
Reputation: 129393
Semicolon is not required and won't affect anything.
Are you getting "undefined subroutine" when calling the sub from the same module's code, or from the main program that uses the module?
If the latter, it's likely because you didn't export the sub's name.
Upvotes: 0
Reputation: 50637
If you're writing anonymous function, then you need semicolon, ie.
my $func = sub {
print "foo";
};
As for plain functions, semicolon is not required, in fact parser works with:
perl -MO=Deparse semi
sub printFoo {
print 'foo';
}
-
cat semi
sub printFoo {
print "foo";
};
Upvotes: 5