Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Testing for group or world writable files

by kcott (Archbishop)
on Jul 07, 2015 at 13:48 UTC ( [id://1133541]=note: print w/replies, xml ) Need Help??


in reply to Testing for group or world writable files

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1133541]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found