Stephen wrote:
> 
> In <[EMAIL PROTECTED]>, [EMAIL PROTECTED]
> wrote
> >
> >   use strict;
> >   use warnings;
> >   use File::Find;
> >
> >   my $file_count = 0;
> >   my $dir_count = 0;
> >
> >   find (\&wanted, "C:/SomeFolder");
> >
> >   sub wanted {
> >     if (-d) {
> >       return unless /[^.]/;
> >       $dir_count++;
> >     }
> >     elsif (-f _) {
> >       $file_count++;
> >     }
> >   }
> >
> >   printf "There are %d files in %d directories.\n",
> >     $file_count,
> >     $dir_count;
> 
> Thanks for the reply, Rob.  With Perl it was love at first sight, but
> the honeymoon is rough.  If you could bear with me and answer the
> following questions I think I'll have a better understanding of how
> File::find works.
> 
> 1.  What is the underscore character following the -f?  No laughing. <g>

The underscore is a special filehandle.

perldoc -f -X
[snip]
               If any of the file tests (or either the `stat' or
               `lstat' operators) are given the special filehan�
               dle consisting of a solitary underline, then the
               stat structure of the previous file test (or stat
               operator) is used, saving a system call.  (This
               doesn't work with `-t', and you need to remember
               that lstat() and `-l' will leave values in the
               stat structure for the symbolic link, not the real
               file.)  Example:

                   print "Can do.\n" if -r $a || -w _ || -x _;

                   stat($filename);
                   print "Readable\n" if -r _;
                   print "Writable\n" if -w _;
                   print "Executable\n" if -x _;
                   print "Setuid\n" if -u _;
                   print "Setgid\n" if -g _;
                   print "Sticky\n" if -k _;
                   print "Text\n" if -T _;
                   print "Binary\n" if -B _;


> 2.  Could you please explain how the last line works?  I don't see where
> the %d comes from, and how the two variables tacked on the end relate to
> it.

printf() is borrowed from the C programming language.

http://www.die.net/doc/linux/man/man3/printf.3.html


> 3.  Continuing with this example, how\where would I implement something
> like the following:
> 
>    foreach $dir(@dir_list) {
>       opendir (DIR, $dir);
>         my @files = sort ( grep(/TXT$/, readdir(DIR)) );
>         $number = @files;    # better way to get array length?

If you just want to get the number of files then you don't need an array
as grep returns the number in a scalar context.

          $number = grep /TXT$/, readdir DIR;


>         closedir(DIR);
>         print "The folder $dir contains $files files.\n"
>         }
> 
> 4. Maybe I'm restating the above question, but in addition to the
> &wanted sub, File::find accommodates process, etc., as well.  When/how
> can these are typically used?

Sorry, I've never used them.


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to