APM: chmod doesn't die

Mike South msouth at shodor.org
Mon Apr 28 15:06:12 CDT 2003


>From: Ren Maddox <renm at iname.com>
>Date: 28 Apr 2003 14:33:30 -0500
>
>On Mon, 2003-04-28 at 11:36, Goldilox wrote:
>[...]
>> chmod 0766,$thumbsfile || no_way();#die "Can't chmod 766 $thumbsfile: $!";
>[...]
>
>As long as $thumbsfile has a (true) value, no_way() will never be
>called.  This is because "||" binds more tightly than ",".  Instead, use
>either of:
>
>chmod(0766, $thumbsfile) || no_way();
>chmod 0766, $thumbsfile or no_way();

Ken is correct.  Just in case anyone is not familiar with the
phrasing "binds more tightly", here is a little more explanation.

The way it was written originally:

>> chmod 0766,$thumbsfile || no_way();#die "Can't chmod 766 $thumbsfile: $!";

is essentially equivalent to this, where I have added "virual parentheses" to 
show what Perl thinks you mean:

	chmod 0766, ( $thumbsfile || no_way() );  # "virtual parentheses" _toward_ the '||'

with "or" instead of "||", as Ken suggested, it is interpreted like this:

	( chmod 0766,  $thumbsfile ) or no_way() );# "virtual parentheses" _away_ from the 'or'

the "or" is "weak binding", which means it doesn't grab stuff next to
it and put it in virtual parentheses, while the normal || does.  

As a basic rule of thumb, if you are making an execution path decision,
like "open this file or die", or "do this or run this subroutine",
you want "or", so that you don't accidentally enclose the last part in
virtual parentheses.

Another good rule of thumb is "when in doubt, parenthesize".  That is, if you
are coding along and you aren't sure how something is going to be imterpreted,
force the interpretation that you mean by explicitly putting the parentheses
in.

mike




More information about the Austin mailing list