#!/usr/bin/perl use strict; use warnings; use CGI qw( :standard ); use File::Spec::Functions; use Cwd; my $cgi = CGI->new(); my $index = $cgi->param('index'); my @files = find_files_by_index( $index ); print "index: $index\n@files", $/; sub find_files_by_index { my $idx = shift; my $dir = cwd(); my @files = (); opendir my $dh, $dir or die "$dir: $!\n"; while ( my $file = readdir $dh ) { my $fullpath = catfile( $dir, $file ); if ( !-d $fullpath and $file =~ m/_${idx}.[^.]+\z/ ) { push @files, $file; } } closedir $dh; return @files; }