in reply to UPDATE :: finding a file exist amongst many files

File::Find::Rule works wonders for doing these tasks. It allows you to supply regexes as the filenames you're looking for.

use warnings; use strict; use File::Find::Rule; my $dir = 'test'; my @files = File::Find::Rule->file ->name('tpsm_*') ->in($dir); for (@files){ print "$_\n"; }

In a test/ directory, I've got:

blah.txt tpsm.txt tpsm_10.txt tpsm_4.txt

Here's the output of files found matching the name I've sent in to look for:

test/tpsm_10.txt test/tpsm_4.txt

Replies are listed 'Best First'.
Re^2: finding a file exist amongst many files
by t-rex (Scribe) on Aug 25, 2016 at 14:15 UTC

    thanks for the reply steve. basically i am looking for if a file with tpsm_x exists, so on the shell it is pretty easy to find this. i want to know its perl counterpart, nevertheless i can try this but i don't want to install this package(legacy code issues).

      Take two, with builtins only. Note that with readdir, it doesn't keep the whole path, so you have to add it back manually:

      use warnings; use strict; my $dir = 'test'; opendir my $dh, $dir or die $!; while (readdir $dh){ if (/tpsm_.*/ && -f "$dir/$_"){ print "$dir/$_ is a file\n"; } }

      update: or use glob as Corion said :)

Re^2: finding a file exist amongst many files
by t-rex (Scribe) on Aug 26, 2016 at 08:57 UTC

    Hello Steve, I just came to know of the requirement and we shall need to process these file , so i was thinking to write a perl sub for iterating through all these files in the manner you have gone through, but without the package Find::Rule, could share a snippet for the same?