Named Unary Operators

Named Unary Operators

The various named unary operators are treated as functions with one argument, with optional parentheses.

If any list operator (print(), etc.) or any unary operator (chdir(), etc.) is followed by a left parenthesis as the next token, the operator and arguments within parentheses are taken to be of highest precedence, just like a normal function call. For example, because named unary operators are higher precedence than ||:

chdir $foo    || die;	# (chdir $foo) || die
chdir($foo)   || die;	# (chdir $foo) || die
chdir ($foo)  || die;	# (chdir $foo) || die
chdir +($foo) || die;	# (chdir $foo) || die

but, because "*" is higher precedence than named operators:

chdir $foo * 20;	# chdir ($foo * 20)
chdir($foo) * 20;	# (chdir $foo) * 20
chdir ($foo) * 20;	# (chdir $foo) * 20
chdir +($foo) * 20;	# chdir ($foo * 20)

rand 10 * 20;	# rand (10 * 20)
rand(10) * 20;	# (rand 10) * 20
rand (10) * 20;	# (rand 10) * 20
rand +(10) * 20;	# rand (10 * 20)

Regarding precedence, the filetest operators, like -f , -M , etc. are treated like named unary operators, but they don't follow this functional parenthesis rule. That means, for example, that -f($file).".bak" is equivalent to -f "$file.bak" .

See also Terms and List Operators (Leftward).

doc_perl
2016-12-06 03:21:39
Comments
Leave a Comment

Please login to continue.