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

In my textbook the author states that binary operators in most imperative programming languages are "infix". He then goes on to say that "One exception is Perl, which has some operators that are prefix..."

I've been trying to figure out what binary operators are prefix. I'm a novice, perhaps I'm missing something obvious. Could someone please give me an example?

Thanks!

Replies are listed 'Best First'.
(Ovid) Re: Prefix binary operators
by Ovid (Cardinal) on Feb 26, 2002 at 18:10 UTC

    Operators can be prefix, postfix, or infix. A binary operator is always infix (in perl):

    $foo + $bar

    In the above example, the plus sign, in this context, is a binary operator and it's "infix" because it's between the two variables that it operates. Unary operators, on the other hand, are always prefix (before the variable), with the exception of the post increment and post decrement operators:

    ++$x; # prefix $x++; # postfix

    The author of your text book probably was referring to Perl's list operators. These are prefix and can take any number of arguments. These are operators like print and sort:

    print $x, $b, $c; sort $a, $b, $c;

    If there are any binary operators that are prefix in Perl, I confess that I don't know them offhand.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

•Re: Prefix binary operators
by merlyn (Sage) on Feb 26, 2002 at 18:07 UTC
    It all depends on your definition of operator. If you consider rename an operator, then it's definitely a prefix. Or perhaps the starting punctuation of tr or s, which clearly have two trailing operands.

    In short, unless you define some of the words better there (or there's more than you are quoting), it's hard to say precisely what is being referenced.

    -- Randal L. Schwartz, Perl hacker