Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat reasons are there to prefer glob over readdir (or vice-versa) in Perl?
    primarykey
    data
    text
    <p>This question is a spin-off from <a href="https://stackoverflow.com/questions/1499672/how-do-i-open-an-array-of-files-in-perl">this one</a>. Some history: when I first learned Perl, I pretty much always used <code>glob</code> rather than <code>opendir</code> + <code>readdir</code> because I found it easier. Then later various posts and readings suggested that <code>glob</code> was bad, and so now I pretty much always use <code>readdir</code>.</p> <p>After thinking over <a href="https://stackoverflow.com/questions/1499672/how-do-i-open-an-array-of-files-in-perl">this recent question</a> I realized that my reasons for one or the other choice may be bunk. So, I'm going to lay out some pros and cons, and I'm hoping that more experienced Perl folks can chime in and clarify. The question in a nutshell is are there compelling reasons to prefer <code>glob</code> to <code>readdir</code> or <code>readdir</code> to <code>glob</code> (in some or all cases)?</p> <h3><code>glob</code> pros:</h3> <ol> <li>No dotfiles (unless you ask for them)</li> <li>Order of items is guaranteed</li> <li>No need to prepend the directory name onto items manually</li> <li>Better name (c'mon - <code>glob</code> versus <code>readdir</code> is no contest if we're judging by names alone)</li> <li><p>(From ysth's answer; cf. <code>glob</code> cons 4 below) Can return non-existent filenames:</p> <pre><code>@deck = glob "{A,K,Q,J,10,9,8,7,6,5,4,3,2}{\x{2660},\x{2665},\x{2666},\x{2663}}"; </code></pre></li> </ol> <h3><code>glob</code> cons:</h3> <ol> <li>Older versions are just plain broken (but 'older' means pre 5.6, I think, and frankly if you're using pre 5.6 Perl, you have bigger problems)</li> <li>Calls <code>stat</code> each time (i.e., useless use of <code>stat</code> in most cases).</li> <li>Problems with spaces in directory names (is this still true?)</li> <li><p>(From brian's answer) Can return filenames that don't exist:</p> <pre><code>$ perl -le 'print glob "{ab}{cd}"' </code></pre></li> </ol> <h3><code>readdir</code> pros:</h3> <ol> <li>(From brian's answer) <code>opendir</code> returns a filehandle which you can pass around in your program (and reuse), but <code>glob</code> simply returns a list</li> <li>(From brian's answer) <code>readdir</code> is a proper iterator and provides functions to <code>rewinddir</code>, <code>seekdir</code>, <code>telldir</code></li> <li>Faster? (Pure guess based on some of <code>glob</code>'s features from above. I'm not really worried about this level of optimization anyhow, but it's a theoretical pro.)</li> <li>Less prone to edge-case bugs than <code>glob</code>?</li> <li>Reads everything (dotfiles too) by default (this is also a con)</li> <li>May convince you not to name a file <code>0</code> (a con also - see Brad's answer)</li> <li>Anyone? Bueller? Bueller?</li> </ol> <h3><code>readdir</code> cons:</h3> <ol> <li>If you don't remember to prepend the directory name, you <strong>will</strong> get bit when you try to do filetests or copy items or edit items or...</li> <li>If you don't remember to <code>grep</code> out the <code>.</code> and <code>..</code> items, you <strong>will</strong> get bit when you count items, or try to walk recursively down the file tree or...</li> <li>Did I mention prepending the directory name? (A sidenote, but my very first post to the Perl Beginners mail list was the classic, "Why does this code involving filetests not work some of the time?" problem related to this gotcha. Apparently, I'm still bitter.)</li> <li>Items are returned in no particular order. This means you will often have to remember to sort them in some manner. (This could be a pro if it means more speed, and if it means that you actually <em>think</em> about how and if you need to sort items.) <strong>Edit</strong>: Horrifically small sample, but on a Mac <code>readdir</code> returns items in alphabetical order, case insensitive. On a Debian box and an OpenBSD server, the order is utterly random. I tested the Mac with Apple's built-in Perl (5.8.8) and my own compiled 5.10.1. The Debian box is 5.10.0, as is the OpenBSD machine. I wonder if this is a filesystem issue, rather than Perl? </li> <li>Reads everything (dotfiles too) by default (this is also a pro)</li> <li>Doesn't necessarily deal well with a file named <code>0</code> (see pros also - see Brad's answer)</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload