Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use File::Find;
- use File::Stat qw/:stat/;
- sub _process_directories {
- # return only binary files
- return grep { -B }
- sort( @_ );
- }
- sub _postprocess_directories {
- }
- sub _process_files {
- return unless -f;
- # generate a stat object for each file
- # or return undef no empty hashref
- my @stat_obj = map({ $sb = stat $_;
- $_ = (defined $sb) ? {
- name => $_,
- owner => $sb->uid(),
- group => $sb->gid(),
- size => $sb->size() / 1024,
- mtime => $sb->mtime(),
- }
- : undef
- } $_);
- # generate the output from the stat object
- print map( {
- "$_:".$stat_obj[0]->{$_}.' '
- }
- #grep { !/time/ }
- keys(%{$stat_obj[0]})),"\n";
- }
- my %options = (
- wanted => \&_process_files,
- preprocess => \&_process_directories,
- #postprocess => \&_postprocess_directories,
- bydepth => 1,
- follow_skip => 2,
- no_chdir => 0,
- );
- finddepth(\%options,@ARGV);
- =head1 About This Script
- I read 'Perl Best Practices' - Damian Conway
- In the book he talked a lot about instinctual coding versus thoughtful coding.
- I realized I code instinctually and this was my attempt at thoughtful code.
- The book stressed the cost and unneccessary need of typical iteratives and suggested
- deploying grep && map as a substitute to 'instinctual for' loops.
- This was my first attempt at using compound iteratives via map && grep.
- =head1 IDEA
- Recursively crawl a directory and report on some `file stat` metadata.
- This could help for troubleshooting directories which contain large amounts of files.
- Or easily be adapted for other filesystem crawling purposes.
- =head2 Meat and Potatos
- =over 8
- Simply put File::Find does all the hard work, I use its preprocess and work subroutines to report on
- filesystem information with a compound print statements using map, grep and sort.
- =back
- =head1 BUGS
- This script probably has bugs.
- It was a learning exercise.
- Do not use this for any sort of production.
Add Comment
Please, Sign In to add comment