Include $! in die part of open(),It will give you the reason, why it can not open the file.

Note that the file names in $file do not include the full path and do include "." and ".." which you probably don't care to have. You can eliminate them with,

@FILES=grep(!/^\.\.?}$/, readdir(DIR));

The regular expression used in the grep can be extended to filter all sorts of ways, returning only files with a specific extension or starting with some text,etc.

To check for all the subdirectories in a directory, try code like this:

$path = shift; $path = "." unless $path; opendir( DIR, $path ) or die "Can't open $path: $!"; while ( $entry = readdir( DIR ) ) { $type = ( -d "$path\\$entry" ) ? "dir" : "file"; # $path is cr +ucial! print "$type\t$entry\n"; } closedir( DIR );

It's a common mistake to leave out the $path from the -d check. If you do this, perl thinks you're talking about files in the current directory. Since the dirs don't -e in your current directory, they definitely don't -d. Exceptions are . and .., which exist in every directory.

opendir, redir, closedir offer the most power and flexibility; but if you just want to read the files out of directory, you can also glob them:

$path = shift; $path .= "/*" while( $name=glob($path) ) { print "$name"; }

All is well. I learn by answering your questions...

In reply to Re: File handling basics by vinoth.ree
in thread File handling basics by ElectroRed

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.