my $path = './space/dir/'; my %codes = ( 'traffic' => \&traffic , 'packetloss' => \&packetloss, 'JitterIN' => \&jitter_in, 'jitterOUT' => \&jitter_out , 'PacketLossIN' => \&packet_loss_in , 'PacketLossOUT' => \&packet_loss_out , ); foreach my $path(keys(%dir)){ (my $pathforcode = $path) =~ /\/(.*?)\/[^\/]*$/; $codes{$pathforcode}->($path,$dir{$path}); }