precedence

Alan Stewart astewart at spawar.navy.mil
Sat Mar 11 11:49:41 CST 2000


~sdpm~
On 10 Mar 00, at 14:01, C. Abney wrote:

[. . .]

> but I'm so brain dead that knowing 'or' is evaled left to right
> isn't enough for me to be sure that its low precedence won't be
> trumped by that of the 'unless'.  Too, 'unless' isn't listed in
> the list of operators in which their precedence is scaled against
> each others (that's 'perldoc perlop'.)  I had to test it.  I used
> the following script:

unless isn't in the list of operator precedence because it isn't an operator, it is a statement 
modifier.

> 
> 	(2)[2136]$ cat or_unless.pl
> 	#! /usr/bin/perl
> 	$FOO = shift;
> 	$ERR = shift;
> 	$ERR or warn "gah!\n" unless $FOO;
> 
> Which, when executed, prints (or doesn't) the following:
> 
> 	(2)[2136]$ ./or_unless.pl 1 1
> 	(2)[2136]$ ./or_unless.pl 1 0
> 	(2)[2136]$ ./or_unless.pl 0 1
> 	(2)[2136]$ ./or_unless.pl 0 0
> 	gah!
> 	(2)[2137]$ 
> 
> Unless I interpreted it wrong, I would have gotten a warning in the
> second test if the 'unless' took precedence.  And I shouldn't have
> had /any/ warnings in the third and fourth tests.
> 

The unless doesn't bind left or right, it modifies the entire preceeding statement.

   $ERR or warn "gah!\n" unless $FOO;

is the same as

   unless ($FOO) {$ERR or warn "gah!\n"}

which is the same as

   if (!$FOO) {$ERR or warn "gah!\n"}

so only the fourth test should print a warning.
In your other example

	system ( "$cmd" ) == 0
		or warn "failed remote copy: $!" unless $QUIET;

I would expect $QUIET to control the whole statement, as in

     unless ($QUIET) {
	system ( "$cmd" ) == 0
		or warn "failed remote copy: $!";
     }

and Perl does what I expect :)

---------------------------------------------------------------
Alan Stewart          )-[]-(           Electronics Engineer
Code D621           ~        ~         Network Operations
SPAWARSYSCEN       ~          ~  \     Satellite Communications
53560 Hull St   ( ~            ~  )    tel (619)524-3625
San Diego,CA  __|___             /|    fax (619)524-2607
92152-5001   ^\____/^^^^^^\    __| |_  astewart at spawar.navy.mil
------------^^^^^^^^^^^^^^^\__|______|_------------------------
~sdpm~

The posting address is: san-diego-pm-list at hfb.pm.org

List requests should be sent to: majordomo at hfb.pm.org

If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at happyfunball.pm.org> with the following
command in the body of your email message:

    unsubscribe san-diego-pm-list

If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at happyfunball.pm.org> .
This is the general rule for most mailing lists when you need
to contact a human.




More information about the San-Diego-pm mailing list