I haven't seen anyone explicitly state the problem with that snippet. I'm assuming that "/cgi and perl" is a directory.

With Perl, you don't open a directory like you would a normal file. Use the opendir command instead. Also, at least on my Unixy machine, that will attempt to read a file in the root directory -- no guarantee what it will do on Windows.

Obviously, if the directory open fails, reading from the filehandle won't work. So the while line doesn't do anything -- which is good, considering what happens next.

The unlink line will probably do the trick (depending on how well globbing is implemented in your version of Perl). Unfortunately, it returns a list of all of the files ending in '.txt' in the current directory, which is hardly what you want. You'll either need to chdir to the directory you want to work on, or prepend that directory name to each of the files you want to remove.

Add the requisite error checking, too. I'd code it with readdir and grep as someone already posted.


In reply to Re: Unlinking... by chromatic
in thread Unlinking... by damian1301

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.