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?
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/;
!~ 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/)
$1 equals the text " brown ".
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.
-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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With