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 |