my @in = qw(nc01123.log nc02123.log nc03123.log); my $year = '0'; my $month = '2'; @in = grep { /^nc$year$month.*\.log$/ } @in; print "@in\n"; __END__ Try zero: The nested sub block was hard to read so this didn't yield too much. perl -MO=Concise test.pl Try one: This makes it look like the regex is compiled anew each time. perl -MO=Concise,-exec test.pl Try two: This looks like the regex is compiled only once. Or maybe debug just dumps it once. use re 'debug';