I don't address efficiency here. Unless your directory is very big, your approach should be efficient enough.
Just a couple of comments on your code, though:
1) In windows, you don't need to use the backslash as a path separator thingy, you can use a forward slash, which doesn't need escaping. This means that instead of typing for example:
my $path = 'c:\\perl\\progs\\testing';You can do:
my $path = 'c:/perl/progs/testing';which is a lot easier to type (and easier to read).
2) Change your line:
close (DIR);to:
closedir (DIR);3) Re the first line inside your sub:
($path)=@_;This is usually written something like:
my $whatever = $_[0];or else:
my $whatever = shift;Note that this uses a distinct variable limited to the scope of the sub, which is generally a Good Idea. Indeed, you could even do away with this line and have as the first line of your sub:
opendir (DIR, shift) || die "$!";4) Lastly, considering this line:
my @files=grep{/\.c$/} readdir(DIR);I would recommend that you also test that everything that you put into @files really is a file (rather than a directory, for example):
my @files = grep{ -f and /\.c$/ } readdir(DIR);dave
In reply to Re: Get file names
by Not_a_Number
in thread Get file names
by nofernandes
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |