in reply to Parse for a name after a keyword search in all the files, given directory as input

Hi prash_sri,

In your code, you are using open instead of opendir, this won't work. If you had warnings turned on, you would have been warned about this, see Use strict and warnings. Also, as documented in readdir, you should prepend the name of the directory to the filename, and note that it will return every entry in the directory, including subdirectories, which you can filter with perl's -f filetest operator, and including the special directories . and .., which you can filter with File::Spec's no_upwards (although the aforementioned -f test would filter them too). See the examples under readdir.

Trying to write a perl code which browses all the files recursively under a given input directory

If by recursively you mean that you want to look into subdirectories too, then I would recommend File::Find over readdir/opendir, or perhaps one of the easier-to-use modules File::Find::Rule, Path::Class's recurse function, or Path::Tiny's visit function. Personally I like Path::Class, but I can recommend any of the modules I mentioned here.

Regards,
-- Hauke D

Update: s/including \Kdir/subdir/

  • Comment on Re: Parse for a name after a keyword search in all the files, given directory as input
  • Select or Download Code