1

I have a piece of Perl code for searchnig a directory and display the contents of that directory, if match is found. The code is 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 to open directory that contains the 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 the directory are dirs_found :: $dirs_found\n";
    closedir (DIR);
    return $dirs_found;
}

Output is,

xml_file_name in sub routines:: sample.xml
Files in the directory are dirs_found :: 1
file search return::1

It is not returning the file name found. Instead it returns the number 1 always.

I don't know why it is not returning the file name called sample.xml present in the directory.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Senthil kumar
  • 965
  • 3
  • 16
  • 33
  • 4
    For easier ways to read the content of a directory, see http://stackoverflow.com/questions/3772001. – FMc Oct 07 '10 at 10:12
  • 1
    `readdir` is not returning anything 'wrongly'. The way you think `grep` works does not match how `grep` works in reality. See http://perldoc.perl.org/functions/grep.html – Sinan Ünür Oct 07 '10 at 10:46
  • You really need to turn on `use strict; use warnings;`. It's going to save you from many headaches. – Daenyth Oct 07 '10 at 17:55
  • possible duplicate of [How do I search for a particular file in a directory using Perl?](http://stackoverflow.com/questions/3667280/how-do-i-search-for-a-particular-file-in-a-directory-using-perl) – brian d foy Oct 07 '10 at 18:58
  • Curiously, we already answered this very question for you, and it's almost exactly the same code. – brian d foy Oct 07 '10 at 19:00
  • same code... but different questions ... :-) – Senthil kumar Oct 09 '10 at 08:39

4 Answers4

8

perldoc grep says:

In scalar context, returns the number of times the expression was true.

And that's exactly what you are doing. So you found 1 file and that result is assigned to $dirs_found variable.

Nikhil Jain
  • 8,232
  • 2
  • 25
  • 47
n0rd
  • 11,850
  • 5
  • 35
  • 56
3
($dirs_found) = grep { /^$xml_file_name/i } readdir DIR; #capture it

Problem was that, you were evaluating the grep as scalar context, change it to list context will give you the desired result.

In scalar context, grep returns the number of times the expression was true.

In list context, it returns the elements for which the expression was true.

Nikhil Jain
  • 8,232
  • 2
  • 25
  • 47
3

Why are you opening a directory and looking for a particular filename? If you want to see if the file is there, just test for it directly:

 use File::Spec::Functions;
 my $file = catfile( $test_case_directory, $xml_file_name );
 if( -e $file ) { ... }

When you run into these sorts of problems, though, check the result at each step to check what you are getting. Your first step would decompose the problem statement:

 my @files = readdir DIR;
 print "Files are [@files]\n";

 my $filtered = grep { ... } @files;
 print "Files are [$filtered]\n";

Once you do that you see that the problem is grep. Once you know that the problem is grep, you read its documentation, notice that you are using it wrong, and you're done sooner than it would take to post a question on StackOverflow. :)

brian d foy
  • 129,424
  • 31
  • 207
  • 592
2

you should say @dirs_found, not $dirs_found

Nikhil Jain
  • 8,232
  • 2
  • 25
  • 47
Habbie
  • 2,150
  • 15
  • 17