Dash (or minus or subtraction) followed by a letter (or function name) causing a syntax error in Perl

I was surprised when

sub f{1}; my $answer = 1-f(1); 

gave me a syntax error in Perl when I expected it to do a subtraction. Adding a space made it work fine:

 sub f{1}; my $answer = 1- f(1); 

Why does this cause a syntax error in Perl? Is there any ambiguity? Is the dash interpreted as part of the function name?

+5
source share
1 answer

-f checks if the file is a regular file, so it does not make sense to perl, because it does not see the sign. This is why it works the way you expect, since there is no -fo test.

 sub fo{1} my $answer = 1-fo(1); 

(and there is no test +f )

 sub f{1} my $answer = 1+f(1); 
+9
source

All Articles