Quantcast

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]

  1. !/usr/bin/perl
use strict;
use warnings;

my $dir = ‘/’;

  1. Use readdir
  2. _______________________________________________________________________________
print “Directory listing in perl of $dir using readdir\n”;
opendir(HANDLE, $dir) or die “Cannot opendir $dir: $!”;
foreach my $file (readdir(HANDLE)) {
print “\t$file\n”;
}
closedir(HANDLE);

  1. Use globbing
  2. _______________________________________________________________________________
print “Directory listing in perl of $dir using globbing\n”;
foreach my $file (glob(“/*”)) {
print “\t$file\n”;
}
[/perl]

[python]

  1. !/usr/bin/python

import os
import dircache
import glob

dir = “/”

  1. Use os.listdir
  2. _______________________________________________________________________________
print “Directory listing in python of ” + dir + ” using os.listdir”
files = os.listdir(dir)
for file in files:
print “\t” + file

  1. Use dircache
  2. _______________________________________________________________________________
print “Directory listing in python of ” + dir + ” using dircache”
files = dircache.listdir(dir)
for file in files:
print “\t” + file

  1. Use glob
  2. _______________________________________________________________________________
print “Directory listing in python of ” + dir + ” using glob”
files = glob.glob(“/*”)
for file in files:
print “\t” + file
[/python]

[ruby]

  1. !/usr/bin/ruby

dir = “/”

  1. Use foreach
  2. _______________________________________________________________________________
print “Directory listing in ruby of #{dir} using Dir.foreach\n”
Dir.foreach(dir) {|file| print “\t#{file}\n” }

  1. Use glob
  2. _______________________________________________________________________________
print “Directory listing in ruby of #{dir} using Dir.glob\n”
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.

3 Responses to “Example scripts: directory listing in perl, php, python, and ruby”

  1. Pete
    March 15th, 2007 | 6:10 am

    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!

  2. Pete
    March 15th, 2007 | 6:11 am

    Oops, there should be a (greaterthan)/*(lessthan) after @_=

  3. March 15th, 2007 | 9:10 am

    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

    [code]

    followed by /code in square brackets

Leave a reply