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.
| [reply] [d/l] [select] |
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
| [reply] |
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. | [reply] |
Re: How to list all the things in a directory?
by JavaFan (Canon) on Apr 28, 2010 at 11:03 UTC
|
| [reply] [d/l] [select] |
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".
| [reply] [d/l] [select] |
|
|
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!
| [reply] |
Re: How to list all the things in a directory?
by rovf (Priest) on Apr 28, 2010 at 12:13 UTC
|
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
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
| [reply] |
|
|
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.
| [reply] |
|
|
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.
| [reply] |
|
|
| [reply] [d/l] |
|
|
|
|
Re: How to list all the things in a directory?
by k_manimuthu (Monk) on Apr 28, 2010 at 08:27 UTC
|
| [reply] |
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.
| [reply] |