print get_num('.'); sub get_num{ my $dir = shift; (sort{$b<=>$a} map{/(\d+)/, $1}<$dir/foo_*.bar>)[0] + 1; }