perl - Why do I get a number instead a list of filenames from readdir? -
i have piece of perl code searchnig directory , display contents of directory, if match found. code given below:
$test_case_directory = "/home/sait11/desktop/salt/data_base/test_case"; $xml_file_name = "sample.xml" $file_search_return = file_search($xml_file_name); print "file search return::$file_search_return\n"; sub file_search { opendir (dir, $test_case_directory) or die "\n\tfailed open directory contains test case xml files\n\n"; print "xml_file_name in sub routines:: $xml_file_name\n"; $dirs_found = grep { /^$xml_file_name/i } readdir dir; print "files in directory dirs_found :: $dirs_found\n"; closedir (dir); return $dirs_found; }
output is,
xml_file_name in sub routines:: sample.xml files in directory dirs_found :: 1 file search return::1
it not returning file name found. instead returns number 1 always.
i don't know why not returning file name called sample.xml
present in directory.
($dirs_found) = grep { /^$xml_file_name/i } readdir dir; #capture
problem that, evaluating grep
scalar context, change list context give desired result.
in scalar context, grep
returns number of times expression true.
in list context, returns elements expression true.
Comments
Post a Comment