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

Greetings. I have this code that tests file modes. It works but perlcritic does not like it. Is there a way to fix it?

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; use Test::More; my $file = $ARGV[0]; my @sb = stat( $file ); my $mode = $sb[2] & 0777; warn "mode of [$file] is [$mode]"; ok( -O $file, "File owned by real uid" ); ok( -f $file, "File is a plain file" ); ok( ! ( $mode & 022 ), "File is not group or world writable" ); done_testing; $ perlcritic ./foo.pl Integer with leading zeros: "0777" at line 11, column 21. See page 58 + of PBP. (Severity: 5) Integer with leading zeros: "022" at line 17, column 17. See page 58 +of PBP. (Severity: 5)

IIRC those numbers are octal, but if I wrap them in oct() test 3 fails.

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Perlcritic and checking file modes
by Athanasius (Archbishop) on Jul 08, 2015 at 16:00 UTC

    Hello neilwatson,

    IIRC those numbers are octal, but if I wrap them in oct() test 3 fails.

    You have to remove the leading zero when wrapping in oct():

    1:53 >perl -wE "say 022;" 18 1:53 >perl -wE "say oct(22);" 18 1:53 >perl -wE "say oct(022);" Illegal octal digit '8' ignored at -e line 1. 1 1:53 >

    (Otherwise the argument to oct is converted from octal to decimal before it’s passed to oct.) So using:

    my $mode = $sb[2] & oct(777); ... ok( ! ( $mode & oct(22) ), "File is not group or world writable" );

    should make Perl::Critic happy. :-)

    Update: Alternatively, you can stringify the argument to oct and leave the leading zero in place:

    1:53 >perl -wE "say oct('022');" 18 2:08 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Perlcritic and checking file modes
by kcott (Archbishop) on Jul 08, 2015 at 16:43 UTC

    G'day Neil,

    To be honest, that does sound like a typo somewhere in your code using oct. You haven't shown that code so please confirm that with these lines:

    my $mode = $sb[2] & 0777; ... ok( ! ( $mode & 022 ), "File is not group or world writable" );

    Test 3 passes and you get those 2 perlcritic warnings.

    But, when changing those lines to:

    my $mode = $sb[2] & oct(777); ... ok( ! ( $mode & oct(22) ), "File is not group or world writable" );

    Test 3 fails but you get those 0 perlcritic warnings.

    And do note that I've removed the leading zeros for the oct arguments: oct(0777) and oct(022) will still generate the perlcritic warnings.

    If you believe you have checked closely and there's no typos, can you provide a short, self-contained script to reproduce the problem that we can run.

    I did actually try to reproduce it with the script I posted in response to your last question regarding this "Testing for group or world writable files". Without oct, I got 4 passes and 4 perlcritic warnings; with oct, I got 4 passes and 0 perlcritic warnings.

    Personally, although there's much about PBP I like, this isn't one of them: you're working with modes, there's a variable called $mode (so that's somewhat obvious), modes are in octal, why complain about octal numbers. If you're of the same view, just turn the warnings off (in the smallest scope possible):

    ## no critic (LeadingZeros) my $mode = $sb[2] & 0777; ## use critic ... ## no critic (LeadingZeros) ok( ! ( $mode & 022 ), "File is not group or world writable" ); ## use critic

    -- Ken

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Perlcritic and checking file modes
by toolic (Bishop) on Jul 08, 2015 at 16:02 UTC
    Post your output for this command so we can see your file permissions:
    ls -l ./foo.pl

    Post your modified code with oct (the code that fails for you).

    You can disable this policy in your ~/.perlcriticrc file:

    [-ValuesAndExpressions::ProhibitLeadingZeros]

    To get a handy reference to the POD for your violation: Sort and summarize perlcritic output

Re: Perlcritic and checking file modes
by Anonymous Monk on Jul 08, 2015 at 22:36 UTC

    Greetings. I have this code that tests file modes. It works but perlcritic does not like it. Is there a way to fix it?

    Sure, stop using perlcritic rules that you disagree with -- perlcritic, it can't choose for you, it doesn't choose at all

    A reply falls below the community's threshold of quality. You may see it by logging in.