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:
-
#!/usr/bin/perl
-
use strict;
-
use warnings;
-
-
my $dir = ‘/’;
-
-
# Use readdir
-
#_______________________________________________________________________________
-
print “Directory listing in perl of $dir using readdir\n“;
-
print “\t$file\n“;
-
}
-
-
# Use globbing
-
#_______________________________________________________________________________
-
print “Directory listing in perl of $dir using globbing\n“;
-
print “\t$file\n“;
-
}
-
#!/usr/bin/python
-
-
import os
-
import dircache
-
import glob
-
-
dir = “/”
-
-
# Use os.listdir
-
#_______________________________________________________________________________
-
print “Directory listing in python of “ + dir + ” using os.listdir”
-
files = os.listdir(dir)
-
for file in files:
-
print “\t“ + file
-
-
# Use dircache
-
#_______________________________________________________________________________
-
print “Directory listing in python of “ + dir + ” using dircache”
-
files = dircache.listdir(dir)
-
for file in files:
-
print “\t“ + file
-
-
# Use glob
-
#_______________________________________________________________________________
-
print “Directory listing in python of “ + dir + ” using glob”
-
files = glob.glob(“/*”)
-
for file in files:
-
print “\t“ + file
-
#!/usr/bin/ruby
-
-
dir = “/”
-
-
# Use foreach
-
#_______________________________________________________________________________
-
print “Directory listing in ruby of #{dir} using Dir.foreach\n“
-
Dir.foreach(dir) {|file| print “\t#{file}\n“ }
-
-
# Use glob
-
#_______________________________________________________________________________
-
print “Directory listing in ruby of #{dir} using Dir.glob\n“
-
files = Dir.glob(“/*”)
-
files.each do |file| print “\t#{file}\n“ end
These scripts, as well as a PHP version, are checked in at SourceForge.
Filed under: code code |
Tagged: example scripts , perl , php , python , ruby

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