sub genFind { my $patt = shift; my $code = "sub find {\n\tmy \$str = shift;\n\t\$str =~/$patt/;\n}; eval $code; die "Error in eval: $@\ncode = $code\n" if $@; } #call genFind ('foo'); $res = find ('barefoot'); #can now call 'find' as function