hide random home http://www.fmi.uni-passau.de/archive/doc/unix/perl/faq/2.3.html (Einblicke ins Internet, 10/1995)

How come Perl operators have different precedence than C operators?

How come Perl operators have different precedence than C operators?


    Actually, they don't; all C operators have the same precedence in Perl as
    they do in C.  The problem is with a class of functions called list
    operators, e.g. print, chdir, exec, system, and so on.  These are somewhat
    bizarre in that they have different precedence depending on whether you
    look on the left or right of them.  Basically, they gobble up all things
    on their right.  For example,

        unlink $foo, "bar", @names, "others";

    will unlink all those file names.  A common mistake is to write:

        unlink "a_file" || die "snafu";

    The problem is that this gets interpreted as

        unlink("a_file" || die "snafu");

    To avoid this problem, you can always make them look like function calls
    or use an extra level of parentheses:

        (unlink "a_file") || die "snafu";
        unlink("a_file")  || die "snafu";

    Sometimes you actually do care about the return value:

        unless ($io_ok = print("some", "list")) { } 

    Yes, print() return I/O success.  That means

        $io_ok = print(2+4) * 5;

    returns 5 times whether printing (2+4) succeeded, and 
        print(2+4) * 5;
    returns the same 5*io_success value and tosses it.

    See the Perl man page's section on Precedence for more gory details,
    and be sure to use the -w flag to catch things like this.