Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dash (or minus or subtraction) followed by letter (or function name) causing 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 was expecting it to perform subtraction. Adding a space made it work okay again:

sub f{1};

my $answer = 1- f(1);

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

like image 361
mareoraft Avatar asked Jul 17 '15 15:07

mareoraft


People also ask

What is S in Perl regex?

The Substitution Operator The substitution operator, s///, is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is − s/PATTERN/REPLACEMENT/;

What does !~ Mean in Perl?

!~ is the negation of the binding operator =~ , like != is the negation of the operator == . The expression $foo !~ /bar/ is equivalent, but more concise, and sometimes more expressive, than the expression !($foo =~ /bar/)

What is the meaning of $1 in Perl regex?

$1 equals the text " brown ".

What is syntax error in expression?

Syntax errors are mistakes in the source code, such as spelling and punctuation errors, incorrect labels, and so on, which cause an error message to be generated by the compiler. These appear in a separate error window, with the error type and line number indicated so that it can be corrected in the edit window.


1 Answers

-f tests whether a file is a plain file, so it does not make sense to perl as it does not see - sign. That is why this works as you expect, as there is no -fo test.

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

(nor there is +f test)

sub f{1} my $answer = 1+f(1);  
like image 165
mpapec Avatar answered Sep 21 '22 10:09

mpapec