in reply to getting files from Dir

You've had some good suggestions so far. The only other thing I'd add is that if you go the opendir route, it's always a good idea to check the return value (returns 1 on success, 0 on failure):
opendir(DIR, $somedir) || die "Could not open $somedir - $!\n";
-- vek --

Replies are listed 'Best First'.
Re^2: getting files from Dir
by Coruscate (Sexton) on Jan 25, 2003 at 01:37 UTC

    As an aside to this, if you are using ||, make sure you've got the parentheses around the arguments passed to opendir. Otherwise you'll be testing for the value of $somedir and dying if $somedir is an empty or undefined scalar. Better yet, just use 'or', so that your test on the success of opening the directory will do what you are expected, whether or not the parentheses are present:

    opendir DIR, $somedir or die "Could not open '$somedir': $!";

    Also, if your script is going to be extremely long, you might want to exclude the newline (\n) at the end of your die statement, to make debugging easier (ie: knowing which line in your program died on you. In this case however, it seems to be a short script, so you'll probably know where the problem is. :)


    "User error. Replace user and press any key to continue."

      You make a couple of good points Coruscate and are probably worth pointing out to avoid confusion. I made that observation myself in this thread. For the record though, I generally use parentheses around functions (just my coding style) and therefore use || and && when testing return values:
      open($someFile) || die "Couldn't open $someFile - $!\n"; opendir(DIR, $someDir) || die "Couldn't open $someDir - $!\n"; system($shellCommand) && die "Couldn't run $shellCommand - $!\n";
      -- vek --