Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Yet Another threaded find|grep perl utility

by Athanasius (Archbishop)
on Nov 05, 2015 at 15:54 UTC ( [id://1147002]=note: print w/replies, xml ) Need Help??


in reply to Yet Another threaded find|grep perl utility

Hello QuillMeantTen,

If you see ... things that can be made better, do post about them ...

This sub caught my eye:

sub printattr{ my $file = shift; my @fileattr = split(//,(stat($file))[2]); print "["; for my $attr (@fileattr){ if($attr eq 0){ print "---"; } elsif($attr eq 1){ print "--x"; } elsif($attr eq 2){ print "-w-"; } elsif($attr eq 3){ print "-wx"; } elsif($attr eq 4){ print "r--"; } elsif($attr eq 5){ print "r-x"; } elsif($attr eq 6){ print "rw-"; } else{ print "rwx" } } print "]\n"; }

It can be written more succinctly using an array (or, better, a persistent array reference) for lookup:

use feature 'state'; sub printattr { state $attrs = [ qw( --- --x -w- -wx r-- r-x rw- rwx ) ]; state $maxi = $#$attrs; my ($file) = @_; print '['; print $attrs->[$_ < $maxi ? $_ : $maxi] for split //, (stat $file) +[2]; print "]\n"; }

Update: Since each array index is limited to the range 0 .. 9, it’s simpler and more efficient to just extend the array:

sub print_attr { state $attrs = [ qw( --- --x -w- -wx r-- r-x rw- rwx rwx rwx) ]; my ($file) = @_; print '['; print $attrs->[$_] for split //, (stat $file)[2]; print "]\n"; }

Hope that’s of interest,

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

Replies are listed 'Best First'.
Re^2: Yet Another threaded find|grep perl utility
by QuillMeantTen (Friar) on Nov 06, 2015 at 07:49 UTC

    Great advice, I found my first solution quite unelegant, I'll rewrite it that way.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-03-19 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found