Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

bitwise AND against a variable containing Fnctl constants not returning desired results

by theillien1 (Acolyte)
on Jun 05, 2014 at 22:15 UTC ( [id://1088934]=perlquestion: print w/replies, xml ) Need Help??

theillien1 has asked for the wisdom of the Perl Monks concerning the following question:

I wrote a script which will scan local, mounted file systems for world-writable files excluding any which are in a hash generated from a list in a file. It utilizes Fcntl in order to make use of the file mode constants.

The current version runs stat three times against a file:

return unless (((stat)[2] & S_IWUSR) && ((stat)[2] & S_IWGRP) && ((s +tat)[2] & S_IWOTH));

This is, of course, less than ideal since it is running one operation three times against every file. That said, it works and the output contains only the files I'm looking for minus those I've explicitly excluded.

It was suggested that I instead set a variable which contains these constants and run a bitwise AND with the $mode field from stat(). I tried this. The result was that the script returned every file, even those not world-writable.

Additionally, I get a "Possible precedence problem" error. I'm inexperienced with bitwise operations so I don't know what I'm encountering with that particular error.

# Some code my $mask = S_IWUSR | S_IWGRP | S_IWOTH; # Some more code return unless $dirStats[2] & $mask == $mask; # Even more code
Possible precedence problem on bitwise & operator at ./ww_files-v4-2 +.pl line 106.

Undestandably, the context is extremely vague here. I'm not sure if etiquette allows for the posting of entire scripts so I've pastebin'ed it: http://pastebin.com/A8d3K2mR

EDIT: All answers pointed to using parens around the bitwise AND before comparing to $mask. This solved both the "Possible precedence" error as well as the results being unexpected.

  • Comment on bitwise AND against a variable containing Fnctl constants not returning desired results
  • Select or Download Code

Replies are listed 'Best First'.
Re: bitwise AND against a variable containing Fnctl constants not returning desired results
by no_slogan (Deacon) on Jun 05, 2014 at 22:29 UTC

    & has lower precedence than ==, so use parens.

    return unless ($dirStats[2] & $mask) == $mask;
Re: bitwise AND against a variable containing Fnctl constants not returning desired results
by RichardK (Parson) on Jun 05, 2014 at 22:32 UTC

    bitwise & has a lower precedence that == , so perl thinks your code is this

    return unless $ds[2] & ( $mask == $mask)

    try adding more brackets to get it do do what you want.

    return unless  ($ds[2] & $mask) == $mask;

    There is a table of operators in precedence order in perlop

Re: bitwise AND against a variable containing Fnctl constants not returning desired results
by Old_Gray_Bear (Bishop) on Jun 05, 2014 at 22:35 UTC
    In Your code:
    return unless $dirStats[2] & $mask == $mask;
    Do you mean:
    return unless ($dirStats[2]) & ($mask == $mask);
    or
    return unless ($dirStats[2] & $mask) == $mask;
    or something else entirely?

    Parenthetical aside: the Perl interpreter makes guesses about intent based on the Precedence Rules. You can use parentheses to explicitly say what you want and remove the guess-work.

    ----
    I Go Back to Sleep, Now.

    OGB

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-19 11:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found