Bill Adams wrote:
> Hi All,
> 
> I've inherited a perl application.  Can anyone explain to me the code
> below, particularly "find sub {"
> 
> find sub { $chambers{$_} = $File::Find::name . "/passwd" if  -r
> $_/passwd"}, @ARGV;

Jonathan gave you a detailed answer, so I'll add just a bit more.

This line is calling a subroutine named find and passing a code reference,
followed by the contents of @ARGV.

The find() subroutine is defined in and exported by the File::Find module.
The first argument needs to be a reference to a subroutine to be called for
each file found in the list of directories passed (via @ARGV in this case).

The "sub" keyword creates an "anonymous" subroutine and returns a reference
to it. This code could have been written as:

   sub foo {
      $chambers{$_} = $File::Find::name . "/passwd" 
         if -r $_/passwd";
   }

   find(\&foo, @ARGV);

In this case, the subroutine is not anonymous, but has a name, "foo".
However, since we aren't going to call this subroutine anywhere but from
find(), there's no reason to give it a name. Hence, the use of "sub" to
create an anonymous routine.

For more information on the "sub" keyword, see 'perldoc perlref' and
'perldoc perlsub'.

The parentheses can be left off of the find() call because it has been
predeclared (by File::Find).

HTH

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Reply via email to