Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

comment on

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

Reading directories is a bit more complicated than just opendir/readdir/closedir, especially if you want to recurse into subdirectories and follow common conventions like ignoring files that start with a dot (Linux/Unix "hidden" files).

There are modules like File::Find that can do a lot of the magic for you. But if you want do more complicated stuff, you may or may not have to roll your own. For example, handling files as you see them (a tactic i use in my own frameworks, especialy when i need to do a lot of processing per file or need complicated search rules) or deciding if you want to read certain directories depending on config options in a database or something like that.

Here's a simple example code of how to recursively iterate through subdirectories. Note that i only have one directory handle open at any one time, to avoid running out of open file handles (Let a bunch of Microsoft Office users get access to a shared drive for a decade and you get directory structures deeper than the Mariana Trench.)

#!/usr/bin/env perl use v5.36; use strict; use warnings; use Data::Dumper; use Carp; use English; my $dirfname = "."; # Start with working directory my @files = findFiles($dirfname); print Dumper(\@files); exit(0); sub findFiles($basedir) { my @fnames; my @dnames; opendir(my $dfh, $basedir) or croak($ERRNO); while((my $fname = readdir $dfh)) { if($fname =~ /^\./) { # Ignore "hidden" files next; } my $fullname = $basedir . '/' . $fname; if(-d $fullname) { push @dnames, $fullname; } elsif(-f $fullname) { push @fnames, $fullname; } else { # It is something else, like a softlink. Ignore it for now } } closedir $dfh; # Only recurse AFTER closing the current dir, this way we won't ex +ceed filehandle maximums foreach my $dname (@dnames) { push @fnames, findFiles($dname); } return @fnames; }

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP

In reply to Re: READDIR and DBI by cavac
in thread READDIR and DBI by justin423

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.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-06-14 01:26 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.