MrNobo1024 has asked for the wisdom of the Perl Monks concerning the following question:

Is not a function or an operator? At first I thought it was an operator, but I just found out it's prototyped, like a function. But it's not in perlfunc. Can someone clear this up for me?

Replies are listed 'Best First'.
Re (tilly) 1: not: function or operator?
by tilly (Archbishop) on Mar 03, 2001 at 10:25 UTC
    Good question.

    It is documented in perlop, so it theoretically should be an operator. OTOH it certainly works like a function.

    It is supposed to be lower precedence than a list operator. At least it is documented that way. So at one point someone had in mind what that was supposed to mean. But I am having some trouble (OK, a lot of trouble) figuring out how to write code that could test for that and demonstrate a difference...

    Very specifically, could anyone figure out some sample code that distinguishes between not and my_not after the following code? (Other than the name and games with overrides.)

    sub my_not ($) { shift ? "" : 1; }
    I certainly can't...
      "print not 0, 1" dosen't print anything, but "print my_not 0, 1" prints 11.
        Ah, then it must really be an operator. As you notice, it manages to force its arguments into boolean context. But it does that and takes a whole list of arguments quite cheerfully. How would a function do that?
        "not 0 | 1" is an empty string, but "my_not 0 | 1" is 1.
Re: not: function or operator?
by sierrathedog04 (Hermit) on Mar 03, 2001 at 14:28 UTC
    not is an operation. overload - Package for overloading perl operations in the Perl core documentation states that:
    Autogenerated method substitutions are possible for the following operations: ...

    ! and not can be expressed in terms of boolean conversion, or string or numerical conversion.

    Similarly, perlfunc - Perl builtin functions includes a list of Perl's builtin functions, and not is not included.

    So I would say that based upon the Perl 5.6 core documentation not is an operator.

      not behaves differently in pre-5.6.0 than in 5.6.0.
      # under 5.005_02 DB<1> x not(1) || 2 0 '' DB<2> x not 1 || 2 0 '' # under 5.6.0 DB<1> x not(1) || 2 0 2 DB<2> x not 1 || 2 0 ''
      This is because in 5.6.0 they decided "if it looks like a function, it is a function". Because, you see, in 5.005_02, the code not(1) || 2 is just like not +(1) || 2 -- which means that (1) is not an "argument" to not, since not isn't a function. In 5.6.0, they decided that this looked too odd, and therefore, if it looks like a function, it is a function.

      japhy -- Perl and Regex Hacker