I must be missing something.

UPDATE: I was missing something. As ikegami points out in the comments, rmtree("foo", 0, 0) does the job. That's what I get for not carefully reading the docs.

Why is doing rm -rf, but in perl, so hard? Before you point to rmtree in File::Path, know this:

$ mkdir foo $ touch foo/bar $ chmod 444 foo/bar $ perl -e 'use File::Path "rmtree"; rmtree("foo", 0, 1)' Can't remove directory foo: Directory not empty at -e line 1 $ ls foo bar

Not only did it not remove foo, it threw a warning. I've stepped through rmtree, and I see no flags/args I can set/pass to make it do the right thing. Maybe I've missed it though...

Note how unlike rm -rf this is:

$ rm -rf foo $ ls foo ls: foo: No such file or directory

Yay! It did what I asked and didn't complain.

Path::Class and File::Remove both use File::Path::rmtree underneath, so they're just as unhelpful. So what to do?

use Path::Class; my $dir = dir('foo'); $dir->recurse(callback => sub { unlink $_[0] if !$_[0]->is_dir; }); $dir->rmtree(0, 1);

That works, but look how clunky and un-concise it is. I'm not asking it to be as concise as "rm -rf", but surely it should fit on one line. And note how unlink() happliy removes my 444 file, yet rmtree won't even try.

So, here are my requirements:

Thanks,
Todd

In reply to concise perl rm -rf idiom that works? by thepler

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.