Ideographic Unary Operators
Most unary operators just have names (see Named Unary and File Test Operators later in this chapter), but some operators are deemed important enough to merit their own special symbolic representation. All of these operators seem to have something to do with negation. Blame the mathematicians.
Unary ! performs logical
negation; that is, “not”. See not for a
lower precedence version of logical negation. The value of a negated
operand is true (1) if the operand is false (numeric 0, string "0", the null string, or undefined), and false
("") if the operand is true.
Unary – performs arithmetic
negation if the operand is numeric. If the operand is an identifier, a
string consisting of a minus sign concatenated with the identifier is
returned. Otherwise, if the string starts with a plus or minus, a string
starting with the opposite sign is returned. One effect of these rules is
that –bareword is equivalent to
"–bareword".[61] If, however, the string begins with a nonalphabetic
character (excluding “+” or “-”), Perl will attempt to convert the string
to a numeric and the arithmetic negation is performed. If the string
cannot be cleanly converted to a numeric, Perl will give the warning
“Argument "the string" isn't numeric in negation
(–)”.
Unary ~ performs bitwise
negation; that is, 1’s complement. For example, 0666 & ~027 is 0640. By definition, this is
somewhat nonportable when limited by the word size of your machine. For
example, on a 32-bit machine, ~123 is ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access