Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: switcher

by turnstep (Parson)
on Dec 31, 2000 at 23:41 UTC ( [id://49120]=note: print w/replies, xml ) Need Help??


in reply to switcher

Quick notes:
  • shift @ARGV is not needed: a simple shift will suffice.
  • Always check the result of your opendir and bail if it does not work.
  • Don't shift twice to disgard the "dot directories" from your readdir. Use a simple regex instead: it works very well with grep like so:
    @fools = grep { !/^\.\.?$/ } readdir(DIR);
  • Better yet, as mentioned above, use File::Find

Replies are listed 'Best First'.
Re: Re: switcher
by $CBAS (Scribe) on Jan 01, 2001 at 17:36 UTC

    I know now that using File::Find would be better but I didn't know that when I wrote it so I used the right-tool-for-the-job ;-)

    I changed the code to check the result of the opendir, would it have caused problems otherwise?

    Why not shift it twice? Isn't that way faster than a grep?
    A shift would just ++ the array's base pointer (in C) so it sounds logical to me to use this over grep, right?

      Yes, two perlfunc:shifts or a perlfunc:splice would be faster than grep, if you can absolutely always rely on . and .. being the first two items listed in a directory. Which you probably can't even if that is the case on your system.

      A false return on opendir means that the system had a reason why it couldn't read what was in the directory (probably no read permission for user, you can find out in $!). This means that the call to readdir would fail (I think it would be fatal under warnings and strict) or if the program tried to keep on going @fools would be empty, causing other problems.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 23:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found