A more efficient way of getting a directory listing?

    You might have noticed no comments are visible yet. I've actually got four to publish, but I'm just finishing off the display side of the system now (there was an afternoon nap that got in the way of that). Anyways, my current problem is that:

      
      open FH, "ls /home/mikal/blog-comments/$path/$filename/ 2> /dev/null |" or die "Couldn't get a list of files";
      
      


    Is terribly inefficient and slow. Is there a faster way of getting a listing of a directory's contents in perl? [tags: directory listing slow]

posted at: 23:04 | path: /perl | permanent link to this entry

    #1 Stewart Smith

    you're looking for opendir and readdir

    opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
    @dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
    closedir DIR;

    (that's from the readdir perldoc page, you'll need to modify it slightly)

    Add a comment to this post:

    Your name:

    Your email: Email me new comments on this post
      (Your email will not be published on this site, and will only be used to contact you directly with a reply to your comment if needed. Oh, and we'll use it to send you new comments on this post it you selected that checkbox.)


    Your website:

    Comments: