This subroutine appears to be written for perl 4 (update: which is over 7 years out of date.) . This is evidenced by the use of local, not my which wasn't in perl 4, and the lack of grep or push which also weren't in perl 4 (strikethrough is update as of 27 Jan 2002, thanks to grinder). In perl 5, my is strongly preferred to local in most circumstances (see also perlsub under the heading "Temporary Values via local()"). Also grep is much easier to use then a for loop for "filtering" a list.

This subroutine also relies on . and .. being returned from readdir first. This is not a portable assumption.

Here is the subroutine re-written not to have those problems:

sub get_sub_dirs { use File::Spec; # so we don't have problems with # platforms which use odd path # seperators. my($path) = @_; my @dirs; opendir(DIR, $path) or die "Cannot open $path: $!\n"; @dirs = grep { # not . or .. !/\A\.{2}\z/ # XXX: Won't work on OSes where # normal directories can be called # '.' or '..' (e.g. MacOS)? and # is a directory -d File::Spec->catfile($path, $_); # (using catfile no chdir is needed; # we just get the full pathname of # the file in question.) } readdir DIR; closedir DIR; return @dirs; }

In reply to Re: Return an Array of Sub-Dir Names by wog
in thread Return an Array of Sub-Dir Names by cynix

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.