singhabsk has asked for the wisdom of the Perl Monks concerning the following question:

i want to use regular expression instead of file name in INPUT method for file processing as i do not know the file name but its type is .txt

I am running one perl xyz.pl program. In the same folder one 3-4-111.txt file is there. which is called by open(INPUT, "<3-4-111.txt");

but i want to use RE instead of typing 3-4-111.txt each time in program. Is it possible????? please give a idea or code.. thanking you

Replies are listed 'Best First'.
Re: regular expression for filename
by Anonymous Monk on May 07, 2011 at 10:16 UTC
Re: regular expression for filename
by Khen1950fx (Canon) on May 07, 2011 at 14:53 UTC
    I think that you are thinking of opening a file for input. You won't need a regex for that. For example, here's the simplest example that I could think of to get you started:
    #!perl #xyz.pl use strict; use warnings; my $file = '3-4-111.txt'; open FH, '<', $file or die $!; my(@lines) = <FH>; @lines = sort @lines; foreach my $line(@lines) { print $line, "\n"; } close FH;
    Now, if you need to find all the files in a directory that end in .txt, you could use glob:
    #!/usr/bin/perl use strict; use warnings; my @files = glob('/your/dir/*.txt'); foreach my $file(@files) { print $file, "\n"; }
    To answer your first question---A regex for finding a file with the extension .txt would simply be: /\.txt$/.