Let's summarize the problems:
- You're not checking the return value of opendir, so you don't know if it succeeded.
- You're not checking the return value of open, so you don't know if it succeeded.
- You should be opening the '/mail' directory, not "mail" (which is what the string looks like after the backslash is processed away).
- You are attempting to read from '$file' (that's the literal filename $file), when you mean to be reading from the filename contained in the scalar variable $file.
- You're calling your input filehandle WRITE, which isn't a bug but is confusing.
Fix those things and see how it goes.
Update: At Not_A_Number's request/suggestion I'm updating to add the very good points mentioned by MidLifeXis and toolic.
- The file you open needs to have the path prepended to it.
- readdir returns the '.' and '..' directories too. A -f test would determine that you're looking at a file, not another directory.
- autodie would eliminate the need to explicitly check the return values of your IO operations.
That gives you a total of eight "bullet points" to work on. Once they're resolved, assuming some other problem hasn't been introduced in the process, you'll be on track.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.