G'day garny,

Welcome to the monastery.

The following repeats some of the information you've already been given. I've expanded on most of this because you say "I am a noob to perl" and you might as well get into good habits from the outset.

'die "cannot open"' and 'die"oops"' are poor error messages that convey little information. I find the autodie pragma is the easiest option; however, if you wish to hand-craft your error messages, do so in a useful fashion (e.g. 'die "Can't open $file for reading: $!"').

Unless you have a good reason not to, use lexical filehandles and directoryhandles (see examples in open and readdir respectively). Package variables (e.g. D and MYFILE in your posted code) are slower to access than lexical variables and you can't control their scope the way you can with lexical variables: in a tiny script, such as you have here, those reasons probably won't matter much, if at all; when you start writing more complex code, you may find this will save you debugging headaches (a good reason to avoid package variables even if the speed factor is negligible).

Get into the habit of using the 3-argument form of open: the documentation has details.

Close handles when you've finished with them; don't leave this until the end of your script. In the code your posted, the closedir() statement could have been placed immedaitely after the readdir() statement.

As explained in the readdir documentation, this function returns directory entries. While this will include the types of files you're intending to read (source code, config files, etc.), it will also include directories (at least the current, '.', and parent '..', directories) as well as symbolic links, character and block special files and so on: you can use the file test operators to filter the type(s) you want.

Instead of using

my @files = readdir($dirhandle);

which returns just the names of all the files, directories, etc., you can use

my @files = grep { -f } map { "$dir/$_" } readdir($dirhandle);

which returns the pathnames of just the files you want.

[Also consider File::Spec for a more portable solution for "$dir/$_".]

-- Ken


In reply to Re: cant open files from a directory by kcott
in thread cant open files from a directory by garny

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.