G'day Neil,

I see answers identifying a precedence problem. Rather than using the ! operator and adding (multiple levels of) parentheses, you can just use the low-precedence equivalent: not (see "perlop: Operator Precedence and Associativity"). There's also low-precedence and, or and xor. I find these operators make the code easier to read than using parentheses: you, of course, may have a different preference.

Instead of using Test::More's ok() function (and, where necessary, negating the test), consider using the is() and isnt() functions. This (at least in the case of your posted code) circumvents the precedence issue entirely; it also aligns the test function with the test description. Here's an example roughly based on your code:

#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 4; my $mode = (stat $0)[2]; isnt(-d $0, 1, "This script isn't a directory."); is( -O $0, 1, "This script is owned by real uid."); isnt($mode & 020, 020, "This script isn't group writable."); isnt($mode & 002, 002, "This script isn't world writable.");

[Note that I've used the core (builtin) stat function. File::stat, by default, overrides this.]

Output:

1..4 ok 1 - This script isn't a directory. ok 2 - This script is owned by real uid. ok 3 - This script isn't group writable. ok 4 - This script isn't world writable.

Update: While my intent was to compare ok() with is() and isnt(), which I believed I achieved, I did notice a bug in the example code. I've fixed the code; retested; and posted the new code and output. The offending line was:

isnt((stat $0)[2] & 022, 022, "This script isn't group or world writab +le.");

Update 2: My bug fix (from the initial update) left the code a bit messy. I've tidied that up. Now just one call to stat with the result stored in $mode. Retested successfully; output unchanged; new code posted.

-- Ken


In reply to Re: Testing for group or world writable files by kcott
in thread Testing for group or world writable files by neilwatson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.