in reply to Re: file::find question
in thread file::find question

your guess is right, however saying m|/| matches / and /var.

/etc/skel$ perl -le '$foo=qq(/); print "YES" if $foo =~ m|/|;' YES /etc/skel$ perl -le '$foo=qq(/var); print "YES" if $foo =~ m|/|;' YES

And yet I need my code to match "/" independently and "/var" independently.

print "enter text\n"; chomp(my $fs = <>); if ( $fs !~ m|/\w+| ) { ##-- only look for / --## print "YES\t$fs\nfirst test\n"; } elsif ( $fs =~ m|/\w+| ) { ##-- only look for /words --## print "YES\t$fs\nsecond test\n"; } /etc/skel$ /etc/skel$ perl test1 enter text /var YES /var second test /etc/skel$ perl test1 enter text / YES / first test

thus I use the code, but can you explain how I will use something similar to find_me( $fs, $fs =~ m|/| );

if ( $fs !~ m|/\w+| ) { ##-- only look for / --## find_me($File::Find::prune = 1); ##-- false, do not prune past / - +-## find_me; } elsif ( $fs =~ m|/\w+| ) { ##-- only look for /words --## find_me($File::Find::prune = 0); ## true, prune dir tree --## } sub find_me { my @directory = ($fs) ; use constant MAX_SIZE => (25*1024*1024) ; use constant DIVISOR => (1024) ; my ( $wanted, $list ) = find_by_min_size ( MAX_SIZE ) ; File::Find::find ( { wanted => $wanted, }, @directory ) ; my ( @sorted_large_files, @large_files ) ; @large_files = $list->() ; <snip> }

Replies are listed 'Best First'.
Re^3: file::find question
by hipowls (Curate) on Jan 05, 2008 at 05:45 UTC

    There are three possible actions

  • do not process
  • process and prune
  • process and do not prune
  • It seems to me that your cases are

  • / root directory, do not prune
  • /var /usr/local an absolute path where the top level directory has a name beginning with a-z A-Z or _, prune
  • every thing else e.g. .ssh /.kde tmp ../etc /+share, ignore
  • if ( $fs eq '/' ) { # only if the root directory my_find( $fs, 0 ); } elsif ( $fs =~ m|^/\w+| ) { # matches /var my_find( $fs, 1 ); } else { # everything else which we ignore # /.ssh # tmp # working/data # /-etc } sub find_me { my @directory = shift; # play nicely with others local $File::Find::prune = shift; <snip> }

    If these are not the cases that you want to match I strongly suggest that you think clearly about what you are trying to match. Are relative paths allowed? How about the tmp directory in your current working directory (no / in the path)? Is there a need to restrict directory names to beginning with word characters?

    Note that I pass parameters to find_me() rather rely on global variables, find_me() can now be used from elsewhere in the program. Also I localize $File::Find::prune so that changes to File::Find within find_me() don't leak out into the rest of your program. If you don't do that you will have to remember to explicit reset $File::Find::prune before you use any Find::File function elsewhere (or spend hours debugging;).

      I understand now. Excuse me for having a hard time putting that into words you could understand. Thank you!
      Shouldn't prune be set to 1 for true as in DO NOT desend dirs? From the man page "-prune" Do not descend into the directory currently matched. I ask b/c I ran these CLIs manually to look at Deparsed code and b/c my code is desending when "/" is used as input.
      /etc/skel$ find2perl /cygdrive/c/temp -size +4092k -ls -prune use strict; use File::Find (); use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; sub wanted; sub ls (); my @rwx = qw(--- --x -w- -wx r-- r-x rw- rwx); my @moname = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my (%uid, %user); while (my ($name, $pw, $uid) = getpwent) { $user{$uid} = $name unless exists $user{$uid}; } my (%gid, %group); while (my ($name, $pw, $gid) = getgrent) { $group{$gid} = $name unless exists $group{$gid}; } # Traverse desired filesystems File::Find::find({wanted => \&wanted}, '/cygdrive/c/temp'); exit; sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid); (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && (int(((-s _) + 1023) / 1024) > 4092) && ls && ($File::Find::prune = 1); }
Re^3: file::find question
by plobsing (Friar) on Jan 05, 2008 at 05:00 UTC
    Maybe I'm failing to see a difficult thing here, but couldn't you just do:
    # Sticking with pattern matching find_me( $fs, $fs =~ m|^/$| ); # Simpler find_me( $fs, $fs eq '/' ); # Simpler Still $File::Find::prune = $fs ne '/'; find_me;