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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.