use strict; use warnings; sub zip { my $file = shift; print "got a zip file $file\n"; } sub txt { my $file = shift; print "got a txt file $file\n"; } my @files = ("a.zip", "b.log", "c.zaaap", "d.txt"); #before you have a particular handler developed, #you can just set it to undef my %handlers = ("zip" => \&zip, "zaaap" => undef, "txt" => \&txt); my $pattern = join("|", map {"\\\.$_\$"} keys %handlers); print "pattern = $pattern\n"; foreach (grep {/$pattern$/} @files) { m/\.([^\.]*)$/; if (defined($handlers{$1})) { &{$handlers{$1}}($_); } else { print "No handler defined for $_\n"; } }