perl - How to put parameters into finds "wanted function" and get a return -
i rather new perl. have find function travers directory structure has "wanted function" called each found file.
i want put in parameters "wanted function" , return value.
i solved global variables , works, don´t code ;-)
$param1 = "value"; $param2 = "value2"; find(\&wantedfunction , $directory); print $returnvalue; sub wantedfunction { #do $param1 , $param2 $returnvalue = f1($param1);"; }
is there better way that?
people have asked returning values file::find::find
before; it's easy enough use anonymous subroutine or closure ikegami , sobrique described, common suggestion use module file::finder
, think has cleaner interface.
the following creates hash of paths inside /tmp
corresponding levenshtein distance fixed string:
use strict; use warnings; use 5.010; use data::dump; use file::find; use file::finder; use text::levenshtein qw(distance); $start = '/tmp/foo'; %distances = file::finder->collect( sub { $file::find::name => distance($start, $file::find::name) }, '/tmp' ); dd \%distances;
output:
{ "/tmp" => 4, "/tmp/fie" => 2, "/tmp/foe" => 1, "/tmp/foo" => 0, "/tmp/fum" => 2, }
Comments
Post a Comment