for my $file ( ... ){ my $filename = $file; my %filnameKeyref; my $topicref = sub { ... $filenameKeyref{ $filename }; return; }; my $twig= XML::Twig->new( ... => $topicref ); $twig->...; } #### for my $file ( ... ){ my $complexAssociations = FooAssoc( $file ); WalkAndTalk( $complexAssociations ); }