Those of you who have need to delve into the core perl tests will see this wierd looking idiom. The reason for this I will explain.

Approaching from the other side, I have a CPAN module with tests, some of which create files and directories on the target machine. In order to make the tests rerunnable, I have a test t/00_clear.t, as follows:

# This is a special 'test' to clear out the residue from any # previous tests that were run, prior to running new tests. # Note: this needs to be portable, so we can't use `rm -rf test`. ######################### use Test::More tests => 1; use File::Find; find( { bydepth => 1, wanted => sub { (-d $_) ? rmdir($_) : unlink($_); }, }, 'test'); rmdir 'test'; ok(!(-d 'test'),"Test directory removed");
I had a specific objective in this module, to make it portable. One of the target platforms was VMS, and I was disappointed to find this test failing when the test suite was rerun (and subsequent tests owing to data lying around).

Further digging showed that $! contained "directory not empty", and I realised that the problem was that VMS has multiply versioned files, but File::Find will only visit each filename once, so the above code leaves any back versions behind, and fails to remove the containing directories.

I posted to the vmsperl list, and received an immediate response:

1 while unlink 'foo';

(this is the canonical method)
Thinking about this incantation, I incorporated it into my module test:
# This is a special 'test' to clear out the residue from any # previous tests that were run, prior to running new tests. # Note: this needs to be portable, so we can't use `rm -rf test`. ######################### use Test::More tests => 1; use File::Find; find( { bydepth => 1, wanted => sub { if (-d $_) { rmdir $_ ; } else { 1 while unlink $_; } }, }, 'test'); rmdir 'test'; ok(!(-d 'test'),"Test directory removed");
and this worked a treat!

I recommend this idiom for everyone writing tests for CPAN modules whenever there is a possibility that a file can become multiversioned on some platforms. I think that portability is a noble aim in itself.

--
I'm Not Just Another Perl Hacker

In reply to 1 while unlink 'foo' by rinceWind

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.