in reply to Pass an "extra' variable to Find::File subroutine

\&Recursive is a callback function (or more precisely, a reference to it) that is being called by File::Find as it walks through the tree.  Why do you want to pass it a parameter?

Maybe you want a closure(?)  Something like this:

use File::Find; my $source = "/mnt/usbdisk"; sub make_wanted { my $arg = shift; return sub { my $diskpathfile = $File::Find::name; print "$arg: $diskpathfile\n"; } } find (make_wanted("hello"), $source);

Here, make_wanted() returns a parameterized function (closure) to be used as the callback routine, i.e. the function instance retains its parameter "hello" as $arg.

Replies are listed 'Best First'.
Re^2: Pass an "extra' variable to Find::File subroutine
by moritz (Cardinal) on Jun 22, 2010 at 13:17 UTC
    I can just guess, but that's how you pass data to callbacks in languages like C, which don't have closures.
      You'd design the C library differently.
        Would I?

        It's a common pattern in C libraries to pass a void pointer to callbacks, and allow the caller of the library to hand in a pointer which is then passed on to the callback.

        And that's a rather sane design in a language without closures, because otherwise the the callback can't obtain additional information about the caller (except for globals, which makes rekursive calls dangerous to impossible).

        Other popular design decision include a hidden, global state variable (same problem with reentrancy), or omitting callbacks altogether (might work for a directory traversal library like File::Find, but would be exceptionally painful for a GUI toolkit).