Passing variable and wild card character to grep in Perl

HI All,
I have a script that needs to find out a list of files in a directory, i pass the search parameter as an argument.

opendir ( DIR, $dir ) || die "Error in opening dir $dirname\n";
@filename1 = (grep {/$File_pattern/ } readdir(DIR));

The problem is my file patterns are like MQS_MT300_XXXXX_YYYYMMDD_*
the third word in the file varies everyday so i need use regex .


1. perl 2.pl MQS_MT535_[A-Za-z0-9]_20100831_

2.perl 2.pl MQS_MT535_[A-Za-z0-9]_20100831_

Above are the ways i call my script from the prompt, i have tried them and many other options , they do not work and my code is unable to find the files

is there a way i can crack this off, Apreciate your help

Are you looking for something like this ?

$
$
$ # list the files in current directory
$ ls -1
MQS_MT535_A1b2X_20100831_blah
MQS_MT535_PQ9rs_20100831_blah
MQS_MT535_XY#9t_20100831_blah
openfiles.pl
$
$
$ # show the contents of the Perl script
$ cat -n openfiles.pl
     1  #perl -w
     2  $some_dir = ".";
     3  $pattern = $ARGV[0];
     4  opendir (my $dh, $some_dir) || die "Can't opendir $some_dir: $!";
     5  @files = grep {/$pattern/x} readdir($dh);
     6  print $_,"\n" for (@files);
     7  closedir ($dh) || die "Can't closedir $some_dir: $!";
$
$
$ # run the Perl script passing a pattern to it
$
$ perl openfiles.pl "MQS_MT535_[A-Za-z0-9]{5}_20100831_"
MQS_MT535_A1b2X_20100831_blah
MQS_MT535_PQ9rs_20100831_blah
$
$
$ # run the Perl script again, passing yet another pattern so that all "MQS" files are selected
$
$ perl openfiles.pl "MQS_MT535_[A-Za-z0-9#]{5}_20100831_"
MQS_MT535_A1b2X_20100831_blah
MQS_MT535_PQ9rs_20100831_blah
MQS_MT535_XY#9t_20100831_blah
$
$
$ # or a very general pattern
$
$ perl openfiles.pl "MQS_MT535"
MQS_MT535_A1b2X_20100831_blah
MQS_MT535_PQ9rs_20100831_blah
MQS_MT535_XY#9t_20100831_blah
$
$

tyler_durden