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

You can just use variables from an outer scope in your callback:
my $source = “/mnt/usbdisk”; sub Recursive { my $msg = $source; ... {
This is the general way to handle such problems in Perl. You can abstact that away with closures:
sub myfind { my ($code, @args) = @_; find(sub { $code->(@args)}) }

Now you can call

myfind \&Recursive, $source;

And have the argument $source passed through to sub Recursive. But most likely you can solve your problem with outer lexical variables in the first place.

Replies are listed 'Best First'.
Re^2: Pass an "extra' variable to Find::File subroutine
by almut (Canon) on Jun 22, 2010 at 13:56 UTC

    As you have it (and as you say), $source would be passed to the Recursive function.  However, it's meant to be the second argument to File::Find's find function (the top-level directory to start recursing from), as that's how its API is defined.

      You are right, my mistake. If I understand the question correctly, it should actually be
      sub myfind { my ($code, $source, @args) = @_; find(sub { $code->(@args)}, $source) } myfind(\&Recursive, $source, $addtional_argument)
      Perl 6 - links to (nearly) everything that is Perl 6.
        Thanks, that was exactly it! It works! To be honest: I don't have any idea what the code does (or better: how it works), but for now I'm happy my problem is solved. And for later, I'm gonna try to find out how you solved my problem! :) Thanks again, Tim
        The great mistake is to anticipate the outcome of the engagement; Let nature take it's course, and your tools will strike at the right moment.