in reply to Linux Show Permissions: traverse file access

grep { s/user::/USER:$o:/} @A; # make native Linux dir/file permis +sions uppercase grep { s/group::/GROUP:$g:/} @A; # idem grep { s/other::/OTHER::/} @A; # idem

That is usually written as:

s/user::/USER:$o:/ for @A; # make native Linux dir/file permission +s uppercase s/group::/GROUP:$g:/ for @A; # idem s/other::/OTHER::/ for @A; # idem
$P{$k} =~ s/(?:user|group):[\s\S]*\K(other:.*)//mi;

[\s\S]* could also be written as (?s:.*) and the /m modifier is only useful if the anchors ^ or $ are used.

$P{$k} =~ s/[\n\r\s]+/ /g; # remove newlines

The \s character class includes the characters \n and \r so that could be simplified to $P{$k} =~ s/\s+/ /g;.

Naked blocks are fun! -- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re^2: Linux Show Permissions: traverse file access
by FreeBeerReekingMonk (Deacon) on Oct 20, 2025 at 18:36 UTC

    Points for pointing out that `s/// for @array` is 50% faster than `grep {} @array`.

    Points for telling me about `(?s:.*)` I did not know it could be written that way. It seems to be as fast as the other method (2% slightly faster), and might be more readable.

    For the third one, it comes at the prices of understanding the intention of the line code. But if I just don't use newlines, it might be better.

    # perl -E '$x="abc\ndef"; $x=~s/.*/u/mi; say $x' u def # perl -E '$x="abc\ndef"; $x=~s/[\s\S]*/u/mi; say $x' u # perl -E '$x="abc\ndef"; $x=~s/(?s:.*)/u/mi; say $x' u

    Will test this one in the field for a while.

      Points for pointing out that `s/// for @array` is 50% faster than `grep {} @array`.

      You could also do all three s/// at the same time:

      s/user::/USER:$o:/, s/group::/GROUP:$g:/, s/other::/OTHER::/ for @ +A;
      For the third one, it comes at the prices of understanding the intention of the line code. But if I just don't use newlines, it might be better.

      Another way to do that which may be better:

      $P{$k} =~ tr/ \n\r\t\f/ /s; # remove newlines
      Naked blocks are fun! -- Randal L. Schwartz, Perl hacker