Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: special directory entries

by jwest (Friar)
on Dec 04, 2001 at 01:12 UTC ( [id://129183]=note: print w/replies, xml ) Need Help??


in reply to special directory entries

You're better off to just strip . and .. out as they come in. Something like:
@dirs = grep(!/^\.\.?$/, readdir(DIR));
would do it nicely.

Hope this helps!

--jwest

-><- -><- -><- -><- -><-
All things are Perfect
    To every last Flaw
    And bound in accord
         With Eris's Law
 - HBT; The Book of Advice, 1:7

Replies are listed 'Best First'.
Re:x2 special directory entries
by grinder (Bishop) on Dec 04, 2001 at 03:19 UTC
    Why do people keep trotting out this readdir-and-regexp idiom to deal with current and parent directories?

    @dirs = grep($_ ne '.' and $_ ne '..', readdir(DIR));

    Sure it's a little more verbose, but it doesn't suffer from sneaky boundary failures (that blakem points out elsewhere in this thread). While it's more verbose, it's also more efficient, as it doesn't need regexp machinery.

    Of course, that should be File::Spec::curdir and File::Spec::updir, to be on the safe side.

    To find out FMTYEWTK about readdir, '.' and '..', I recommend reading this thread, especially merlyn's reply.

    --
    g r i n d e r
    just another bofh
Re: Re: special directory entries
by cybermack72 (Novice) on Dec 04, 2001 at 02:20 UTC
    Anything for Win32...?
      As far as I know, this should be portable to Win32. Unless grep isn't implemented there, but I believe it to be. If you let me know where this fails on Win32, I might be able to help.

      Also, wog's solution below looks like it will handle any portability issues transparently.

      --jwest


      -><- -><- -><- -><- -><-
      All things are Perfect
          To every last Flaw
          And bound in accord
               With Eris's Law
       - HBT; The Book of Advice, 1:7
      

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://129183]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found