#!/bin/perl -w use strict; use File::Find; # create various wnated functions my $wanted= make_wanted( \&wanted_1, 'toto', 'tata'); find( $wanted, '.'); print "\n"; $wanted= make_wanted( \&wanted_1, 'foo', 'bar', 'baz'); find( $wanted, '.'); print "\n"; $wanted= make_wanted( \&wanted_2, 'toto', 'tata'); find( $wanted, '.'); print "\n"; $wanted= make_wanted( \&wanted_2, 'foo', 'bar', 'baz'); find( $wanted, '.'); print "\n"; # a regular function, can access its arguments and the File::Find variables sub wanted_1 { my @args= @_; print "wanted_1( ", join( ', ', @args), ") on $_\n" if( m/\.xml$/); } sub wanted_2 { my @args= @_; print "wanted_2( ", join( ', ', @args), ") on $_\n" if( m/\.txt$/); } # the closure generator # creates a function that calls the function passed as first argument # passing it the arguments make_wanted is called with sub make_wanted { my $wanted= shift; # get the "real" wanted function my @args= @_; # "freeze" the arguments my $sub= sub { $wanted->( @args); }; # generate the anon sub return $sub; # return it }