szabgab has asked for the wisdom of the Perl Monks concerning the following question:

How can I get all the things (files, subdirs etc) in a directory and I want to make it platform independent? Will this code work on every operating system?
glob "* .*"
(There is a space after the first *) If I recall there was some issue with this a few years ago on Windows and since then I keep using opendir and readdir for this specific case:
opendir my $dh, $dir or die; my @things = readdir $dh;
but now I tried it again and it seems to work on both Linux and Windows.

So what is the recommended way to list all entries in a directory?

Update

Apparently it was not clear from my question but I am only interested in the top most directory.

Regarding platform independence: yeah, I could have pointed out that I mentioned Linux and Windows as those are the two where I tried and no, I don't want to solve this perl question for operating systems that do not have perl.

Replies are listed 'Best First'.
Re: How to list all the things in a directory?
by Corion (Patriarch) on Apr 28, 2010 at 08:26 UTC

    readdir will get "all the things" in a directory, and Path::Class tries to be somewhat platform independent, and filters . and .. out for you. Also consider File::Find, if you want stuff beyond one directory.

    Path::Class is fairly nice, but doesn't respect Windows well - at least its ->slurp() method does not understand to use binmode, so you can only read in files that you know are text files where the line endings are not critical.

Re: How to list all the things in a directory?
by Ratazong (Monsignor) on Apr 28, 2010 at 08:24 UTC

    I'm always using File::Find - and it is working fine for me :-)

    However it might be overkill for you ...

    HTH, Rata
Re: How to list all the things in a directory?
by DrHyde (Prior) on Apr 28, 2010 at 09:39 UTC
    I'd use opendir() and readdir(). I'd not trust glob() to Do The Right Thing on, eg, VMS and RISC OS. Also, if you previously foudn that globbing didn't work on Windows, then you already have an example of a platform that glob() isn't portable for! Perhaps it was a particular version of Windows, or a particular version of perl, but if you want to be platform independent, then you need to take that in to account.
Re: How to list all the things in a directory?
by JavaFan (Canon) on Apr 28, 2010 at 11:03 UTC
    So what is the recommended way to list all entries in a directory?
    ...
    Apparently it was not clear from my question but I am only interested in the top most directory.
    Then the answer is readdir. I cannot really imagining something thinking there's a different answer (which was why I initially didn't assume you only wanted the top most directory).

    A relevant quote from perlport:

    Don’t count on filename globbing. Use "opendir", "readdir", and "closedir" instead.
    You also may want to read perlport and take its notes regarding readdir in combination with VMS and Acorn RISC OS into account.
Re: How to list all the things in a directory?
by toolic (Bishop) on Apr 28, 2010 at 12:55 UTC
    File::Slurp::read_dir from CPAN provides the same functionality as the opendir/readdir built-in combination. It also innately checks if the opendir failed.
    use File::Slurp qw(read_dir); my @things = read_dir($dir, keep_dot_dot => 1);
    To assuage your platform-dependence fears, it seems to have good coverage according to the CPAN Testers "Perl/Platform Version Matrix".

      That matrix only covers platforms that CPAN-testers have used. CPAN-testers have pretty good coverage on Unix-a-likes and Windows, but there's no-one testing regularly with VMS, RISC OS or Amiga. And those are the sort of platforms that you'd expect to have the most portability problems.

      I have anecdotal evidence that most modules don't pass their tests on VMS, and that VMS users are used to patching them locally. Unfortunately, I don't think any of them have got round to patching the CPAN-testing tools and submitting patches back to the maintainers. Anyone with a home VAX who can dedicate some time to that? Maybe you could apply for a TPF grant!

Re: How to list all the things in a directory?
by rovf (Priest) on Apr 28, 2010 at 12:13 UTC
    Will this code work on every operating system? glob "* .*"
    Should be. * catches all names not starting with a dot, and .* does the rest.

    I don't want to solve this perl question for operating systems that do not have perl.
    Certainly a reasonable assumption in the context of this forum ;-)

    Just out of curiosity: Is there anyone who has worked during the past, say, 10 years with an operating system where Perl was not available?

    -- 
    Ronald Fischer <ynnor@mm.st>

      How about Chrome OS, Palm OS, Symbian, whatever those i* devices run etc.

        Symbian has Perl (5.8.3, thanks to Jarkko), as does Windows CE. Android is supposed to have "a" Perl too (android-scripting, perldroid, Perl Wiki for Android), and as the iPhone is just a rebranded *BSD/OSX-for-ARM, it should be possible to compile Perl for it too.

        Updated: Added links for Android

      Is there anyone who has worked during the past, say, 10 years with an operating system where Perl was not available?
      I have. Several years ago (but less than 10 ;-)), I worked at a company had developed its own OS in the early 70s, and is still using it today. Noone has ever ported Perl to it. I've also worked with Linux systems where the resources were constraint in such a way (16M disk, 4M RAM, 1.6M OS-image) that fitting in Perl wasn't possible.

      Sure.

      * Palm OS - there's no perl port, and I find it highly unlikely that perl could ever be ported to it given the, umm, "eccentric" memory model and lack of things like filehandles, processes etc;

      * iPhone OS - there's no perl on the machine, Apple won't ever let it in the App Store, and even if you jailbreak, you'd still have to build (and possibly port) it yourself cos it's not in any of the Cydia repositories;

      * CP/M - OK, so I'm eccentric (and so was my customer), but I've done paid work on CP/M in the last ten years.

        CP/M - OK, so I'm eccentric (and so was my customer), but I've done paid work on CP/M in the last ten years.
        Incredible!!!! I had not expected that hardware from this time would still work! I have done my last CP/M(-86) development in the around 1990 and even then it was obsolete already.

        Thanks for the information!

        -- 
        Ronald Fischer <ynnor@mm.st>
Re: How to list all the things in a directory?
by k_manimuthu (Monk) on Apr 28, 2010 at 08:27 UTC

    You will be use the "File::Find" module. That is access the sub directories also.

Re: How to list all the things in a directory?
by JavaFan (Canon) on Apr 28, 2010 at 09:15 UTC
    I want to make it platform independent
    ...
    it seems to work on both Linux and Windows
    You know, "platform independent" means a bit more than just "Linux" and "Windows".

    Of course, you could do worse. For some, "platform independent" means it runs on both Redhat and Debian.

    And if it really has to work on "every operating system", doing it in Perl may not be your right choice. Perl itself doesn't run on "every operating system". But then, I don't think you really have an urge to run your code on your car or dishwasher.