xjlittle has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I used find2perl to convert the following find command to perl. I added the while statement to this part of the code to get user input. This part works ok.
while (<STDIN>){ File::Find::find({wanted => \&wanted}, $_); exit; }
This works ok for defining one directory. What I would like to do is give the users the ability to define more than one directory. Below is the complete code. Thank you for any suggestions. John
#! /usr/bin/perl -w eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0; #$running_under_some_shell use strict; use File::Find (); print "In which directory do you want to search?\n"; # Set the variable $File::Find::dont_use_nlink if you're using AFS, # since AFS cheats. # for the convenience of &wanted calls, including -eval statements: use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; sub wanted; sub doexec ($@); use Cwd (); my $cwd = Cwd::cwd(); # Traverse desired filesystems while (<STDIN>){ File::Find::find({wanted => \&wanted}, $_); exit; } sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid); (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && (int(((-s _) + 511) / 512) > 10239) && doexec(0, 'ls','-la','{}'); } sub doexec ($@) { my $ok = shift; my @command = @_; # copy so we don't try to s/// aliases to consta +nts for my $word (@command) { $word =~ s#{}#$name#g } if ($ok) { my $old = select(STDOUT); $| = 1; print "@command"; select($old); return 0 unless <STDIN> =~ /^y/; } chdir $cwd; #sigh system @command; chdir $File::Find::dir; return !$?; }

Replies are listed 'Best First'.
Re: Using a user input array with File::Find
by Bloodnok (Vicar) on Jun 28, 2009 at 01:17 UTC
    Assuming white space separated user input, what's wrong with using split to auto-split the user input, giving:
    while (<STDIN>){ File::Find::find({wanted => \&wanted}, split); exit; }
    ??

    Is it just me or am I missing something (coz it's stupid o'clock here) ?? Coz it strikes me that exit in the loop is preventing you reaching your goal since even if the user entered a number of paths, the exit would cause the loop (and indeed the program) to terminate once the first (set of) path(s) has been processed - remove the exit and get the user to just enter <CTRL><D> to end the list and thus the program.

    A user level that continues to overstate my experience :-))
      Ahh ok. I didn't know I could use split like that (I don't get to do enough perl!). I originally tried using a foreach loop with the array created by the input from the user. The split worked really well. Thank you!
Re: Using a user input array with File::Find
by oko1 (Deacon) on Jun 28, 2009 at 01:12 UTC

    Unless I've misunderstood your question, there's no need for that while loop: File::Find can take a list of directories as its last argument.

    ... File::Find::find({wanted => \&wanted}, @ARGV);

    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf