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
  2. use strict;
  3. use warnings;
  4.  
  5. my $dir = ‘/’;
  6.  
  7. # Use readdir
  8. #_______________________________________________________________________________
  9. print “Directory listing in perl of $dir using readdir\n;
  10. opendir(HANDLE, $dir) or die “Cannot opendir $dir: $!”;
  11. foreach my $file (readdir(HANDLE)) {
  12.     print \t$file\n;
  13. }
  14. closedir(HANDLE);
  15.  
  16. # Use globbing
  17. #_______________________________________________________________________________
  18. print “Directory listing in perl of $dir using globbing\n;
  19. foreach my $file (glob(“/*”)) {
  20.     print \t$file\n;
  21. }

PYTHON:
  1. #!/usr/bin/python
  2.  
  3. import os
  4. import dircache
  5. import glob
  6.  
  7. dir = “/”
  8.  
  9. # Use os.listdir
  10. #_______________________________________________________________________________
  11. print “Directory listing in python of “ + dir + ” using os.listdir”
  12. files = os.listdir(dir)
  13. for file in files:
  14.     print \t + file
  15.  
  16. # Use dircache
  17. #_______________________________________________________________________________
  18. print “Directory listing in python of “  + dir + ” using dircache”
  19. files = dircache.listdir(dir)
  20. for file in files:
  21.     print \t + file
  22.  
  23. # Use glob
  24. #_______________________________________________________________________________
  25. print “Directory listing in python of “  + dir + ” using glob”
  26. files = glob.glob(“/*”)
  27. for file in files:
  28.     print \t + file

RUBY:
  1. #!/usr/bin/ruby
  2.  
  3. dir = “/”
  4.  
  5. # Use foreach
  6. #_______________________________________________________________________________
  7. print “Directory listing in ruby of #{dir} using Dir.foreach\n
  8. Dir.foreach(dir) {|file| print \t#{file}\n }
  9.  
  10. # Use glob
  11. #_______________________________________________________________________________
  12. print “Directory listing in ruby of #{dir} using Dir.glob\n
  13. files = Dir.glob(“/*”)
  14. files.each do |file| print \t#{file}\n end

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