my %options; getopts("e:", \%options); ... $wanted = qr($options{e}); sub getwanted(&@){ my ($wanted, $file) return 1 if $wanted->($file) ... } foreach (@files){ $filepath = $_->[0]; if (getwanted $wanted $filepath){ push @matched, $filepath; } }