The operator ! in Perl

The unary not operator, the exclamation mark ! in the Perl programming language is its logical negation.

The result of the operator ! is always one of two options:

  • it's either the number 1 (if ! applies to lies)
  • either an empty string '' (if ! applies to the truth)

Here's an example:

▶ Run

print !0;

The program will display the text 1. In this example, the operator ! applies to the number 0. The number 0 is the lie and the result of the operator ! — the truth.

Use !!

Sometimes the operator ! used 2 times in a row. In this case, the result of this dual operator the number 1 if these two operators are applied to truth or the empty string if they are used to lies.

Related topics

Other articles