APM: chmod doesn't die
Mike Stok
mike at stok.co.uk
Tue Apr 29 10:50:30 CDT 2003
On Mon, 28 Apr 2003, Mike South wrote:
[...]
> >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.
[...]
> 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.
Another good rule of thumb is to understand perl's precedence, and failing
that remember that there are modules which let you see "perl's
interpretation" of what you wrote e.g.
[mike at ratdog mike]$ perl -MO=Deparse,-p -e 'chmod 0766,$thumbsfile || no_way()'
chmod(0766, ($thumbsfile || no_way()));
-e syntax OK
[mike at ratdog mike]$ perl -MO=Deparse,-p -e 'chmod 0766,$thumbsfile or no_way()'
(chmod(0766, $thumbsfile) or no_way());
-e syntax OK
perldoc B::Deparse will let you see more things you can use this for.
Hope this helps,
Mike
--
mike at stok.co.uk | The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike at exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA
More information about the Austin
mailing list