in reply to Re: Globbing uncertainty
in thread Globbing uncertainty

In my experience, readdir is almost never what you want. Use glob. Really, it's much easier, and it keeps the full pathname that is fed to it. Your @files array will hold only the filenames, not $path_to_dir. In order for it to be useful, you almost always will need to concatenate $path_to_dir to each item in the array anyway. Since that is probably the case, just use glob and the list you get back will already have the pathname in there, and the values will be useful immediately.

Replies are listed 'Best First'.
Re^3: Globbing uncertainty
by Marshall (Canon) on Nov 11, 2010 at 20:58 UTC
    In my experience, glob() is almost never a good idea for me - I used to think that it was until I gained more experience and got bitten by this critter a few times. So we just disagree a bit.

    For example, I've had trouble with ActiveState's glob vs BSD glob vs Posix glob and some pretty strange things have happened when my code wouldn't run on some other guy's windows box as a PerlApp.exe. Anyway haven been bitten a couple of times with cross platform weirdness, I am leery of glob(). Yes, to say open the file, you do indeed have to concatenate the $dirpath name. "$dirpath/$file". Lots of times I need to process the $filename (maybe update some serial number in it), which if full path, needs Basename or such to get the filename, or I need a more complex regex than what the simple glob() thing does or some other file test (like on modification date). Things like: Re: opening accented file names come up also.

    Anyway, the OP now knows a couple of ways. And can choose the tool that best fits the job at hand. I certainly am not saving never use glob() or always use readdir(). Always and never are two words that don't fit with programming.

      I usually use chdir and glob '*pm', or less often I escape paths used to build glob patterns