Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??

You might be interested in my node To glob or not to glob for the caveats of glob.

What would be the easiest to accomplish "listing all files/subdirectories including dotfiles/dotsubdirs but without the . and .." and "listing all dotfiles/dotsubdirs only, without the . and .."?

If you mean core-only, then:

use File::Spec::Functions qw/ no_upwards catfile catdir /; opendir my $dh, $path or die "$path: $!"; my @files = map { -d catdir($path,$_) ? catdir($path,$_) : catfile($path,$_) } sort +no_upwards readdir $dh; closedir $dh;

Note: I think that on some OSes (VMS?), there's a difference between catfile and catdir, that would require you to use the -d test, but I believe the above should work fine on any other OS. (Or, you can omit the catfile entirely if bare filenames are ok.) <update> Confirmed the difference between catfile and catdir with File::Spec::VMS and File::Spec::Mac, so I updated the above example with the -d test accordingly. </update> <update2> I don't have a VMS or Classic Mac to test on, but I realized that my update had a bug in that I wasn't doing the -d test on the full filename. So I hope that this updated version would really be correct on those platforms. </update2>

If you need absolute pathnames, you probably want to add a $path = rel2abs($path); (also from File::Spec). Otherwise, if CPAN is fine, then I really like Path::Class, its children includes everything except the . and .. by default:

use Path::Class; my @files = dir($path)->children(); # - or - my @files = dir($path)->absolute->children();

In reply to Re: glob() and dot files (updated) by haukex
in thread glob() and dot files by perlancar

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.