Search code examples
perlfilehandle

What does exclamation point mean here


What does the exclamation point mean in the this example? The one right before 'open'? The book can't answer my question..

if ( ! open PASSWD, "/etc/passwd") {
    die "How did you get logged in? ($!)";
}

while (<PASSWD>) {
    chomp;
    ...
}

Solution

  • ! is the boolean negation operator.

    • Given a true value, it returns false.
    • Given a false value, it returns true.

    open(...) returns false on error and true on success.

    Therefore, !open(...) returns true on error and false on success.

    The if's block is executed if the expression (!open(...)) is true.

    Therefore, the if's block is executed if open returned an error.

    Perl's symbolic operators are documented in perlop.