Don't mind if I chip in myself. (plaid's approach doesn't take into account the possibility of a non-UNIX platform...)
sub process_file {
open F, shift;
# ...do stuff with F...
close F;
}
sub recurse_dir {
opendir D, shift;
while (readdir D) {
process_file ($_) if -f;
recurse_dir ($_) if -d;
}
closedir D;
}
recurse_dir ($ARGV[0]);
Two comments: first, I'm not sure if that's what the OP wanted at all. It's a rather useful idiom nonetheless. Second, this would have been a much more elegant solution had I made recurse_dir receive a subref and return a closure that recursively applied this subref to each file (as one would do in Scheme, e.g.). Unfortunately, this causes serious scope issues, especially if one chooses to
use strict;
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.