That looks like a perfectly respectable script. You could make it a bit shorter, but for the most part there's really no reason to.

One thing I would consider is using the File::Copy module to copy the file; it will be shorter, and perhaps faster on some platforms. Even if you don't use that, reading the entire file into memory then writing the entire thing out wastes some memory. A normal idiom for that is something like:

open(INPUT,$file) or die ("Error opening '$file' for input: $!\n"); open(OUTPUT,">".$file) or die ("Error opening '$file' for output: $!\n +"); while(read(INPUT,$buf,8192)) { print OUTPUT $buf; } close(INPUT) or die "Couldn't close '$file' after input: $!\n"; close(OUTPUT) or die "Couldn't close '$file' after output: $!\n";

One other thing to think about is error handling. It looks like when you encounter an error, you print out a message, and put the entire rest of the program inside an else block. If you just print the error and immediately exit, such as with die, the flow of your program will be more clear. Also, you might as well use more useful error messages than "Error 3", and print the reason for the error with the $! variable.

When you call readdir the first time, you may be throwing away the first item from the directory; be careful of that. Also, if you want to see if there's something in the directory, using $allThings[0] eq "" is a sloppy way to do it; $allThings[0] is undefined, and it so happens that when compared to a string it will be converted to an empty string. It's probably best to just see how many elements are in the array, with @allThings == 0 or !@allthings.


In reply to Re: this script normaly deplace files in a directory ! by sgifford
in thread this script normaly deplace files in a directory ! by boby_drack

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.