#! /usr/bin/perl use warnings; use strict; use feature qw{ say }; open my $fh, '>', $_ for my @files = qw( a1 a2 b1 b2 ); for my $mask ('a*', 'b*') { while (my $f = glob $mask) { say $f; last } } unlink @files;