Example scripts: directory listing in perl, php, python, and ruby
I remember when I fell in love with Perl. It was the summer of 1995, and peliom and I had just met, and were working at the Lab. Postscript hacking using MacPerl on OS 8. It was beautiful.
That was more than ten years ago, and even though I’ve remained a Perl hacker the whole time, I see massive amounts of development happening on Python and Ruby, and the Perl community seems to be slowing down (what’s up with Perl 6 anyway?), so, despite the lack of block-level scope, I think it might finally be time to move on.
I don’t know enough about either Python or Ruby to figure out which to learn, so I’ll learn them both, and deal with choosing one later. Along the way I’ll post some example scripts. Anyone else making the jump from Perl or PHP to something modern might find these useful. Here is the first example: printing out a directory listing using readdir and glob in your favorite scripting language:
[perl]
- !/usr/bin/perl
use warnings;
my $dir = ‘/’;
- Use readdir
- _______________________________________________________________________________
opendir(HANDLE, $dir) or die “Cannot opendir $dir: $!”;
foreach my $file (readdir(HANDLE)) {
print “\t$file\n”;
}
closedir(HANDLE);
- Use globbing
- _______________________________________________________________________________
foreach my $file (glob(“/*”)) {
print “\t$file\n”;
}
[/perl]
[python]
- !/usr/bin/python
import os
import dircache
import glob
dir = “/”
- Use os.listdir
- _______________________________________________________________________________
files = os.listdir(dir)
for file in files:
print “\t” + file
- Use dircache
- _______________________________________________________________________________
files = dircache.listdir(dir)
for file in files:
print “\t” + file
- Use glob
- _______________________________________________________________________________
files = glob.glob(“/*”)
for file in files:
print “\t” + file
[/python]
[ruby]
- !/usr/bin/ruby
dir = “/”
- Use foreach
- _______________________________________________________________________________
Dir.foreach(dir) {|file| print “\t#{file}\n” }
- Use glob
- _______________________________________________________________________________
files = Dir.glob(“/*”)
files.each do |file| print “\t#{file}\n” end
[/ruby]
These scripts, as well as a PHP version, are checked in at SourceForge.
Filed under: code code, example scripts, perl, php, python, ruby · 3 Comments

Neat, I’ve been thinking of looking in to ruby recently, but haven’t had the time.
Oh, and with perl, there’s always more than one way to do it :)
Neat thing about fileglobbing in perl is that you don’t need to use glob(), you can just use the diamond operator:
foreach(@_=){print “$_\n”;}
works great!
Oops, there should be a (greaterthan)/*(lessthan) after @_=
Thanks Pete!
I’m intentionaly trying to avoid using Perl idioms that only Perl people can understand. After talking to a few non-perl people, it seems like they mostly hate perl because they can’t grok the syntax of the idioms, and those are easy to avoid. Well, also, objects.
I totally understand the desire to type as few chars as possible, but I’ve found that non-perl people are generally ok with perl code that doesn’t use idioms and special variables.
You can enclose code blocks in
followed by /code in square brackets