Phoenix.pm: Best Methods on Win32

Scott Walters scott at illogics.org
Mon Nov 10 18:19:04 CST 2003


> 	opendir (ROOT, $localRoot) || die "Cannot open directory $pathRoot: $!\n";

A few minor stylic things:

"or" is like "||" but is very low precedence, so you could write that without
the parens. This can make code more concise (which may not be a good thing),
but it also serves to avoid precedent accidents, where you alternate on the
last value in a list or an argument rather the return of the function call.

Also, modern perls (was it 5.6.0 that did this?) let you use lexicals for filehandles.
When the current scope is left, the filehandle is closed, and it is easy to
store references to them and bless them. In fact, they're automatically
blessed into IO::Handle if you use IO::Handle. 

Also also, if you use the "3 arg" version of open, your scripts are more
secure. Perl promotes a nasty command interjection problem with the two
arg one - if you read the filename from user code and it contains any
shell meta characters, then Perl uses those characters to decide whether
you're opening a pipe to a program you want to run (run! danger!) or 
write to a file or read to a file or append or whatever. 

Also also also, if you put a \n on the end of a die string, it will prevent
Perl from adding context to the error message (line and such), which may
or may not be what you want.

Hence:

  opendir my $root, '>', $localRoot or die "Cannot open directory $pathRoot: $!";
 
Had that been a file you opened, you could do things like:

  $root->flush(); # flush written data 

Perl, being an inelegant language, requires us programmers to work overtime
on style.
  
All of which is a round about way of saying I don't see any major problems =)

-scott

On  0, Scott Thompson <scottcodes at thepurplehaze.com> wrote:
> 
> > -----Original Message-----
> > From: owner-phoenix-pm-list at pm.org
> > [mailto:owner-phoenix-pm-list at pm.org]On Behalf Of Scott Thompson
> > Sent: Monday, November 10, 2003 1:49 PM
> > To: phoenix-pm-list at happyfunball.pm.org
> > Subject: RE: Phoenix.pm: Best Methods on Win32
> >
> > Devil's advocate, for those on the list who might be New To Perl,
> > here's what I'm working on so far:
> >
> 
> Here's the "final" version I got working.  FYI, the output is "designed" to
> be dumped into a CSV file for review in Microsoft Excel.
> 
> Comments and suggestions definitely welcome.
> 
> ------------------------------>8  Cut 8<------------------------------
> #!perl -w
> use strict;
> 
> # Global variables...
> my $pathDepth = 0;
> my $pathRoot = $ARGV[0] ? $ARGV[0] : "C:\\Program Files";
> my $pathMax = $ARGV[1] ? $ARGV[1] : 50;
> 
> # Main code section...
> procDir($pathRoot);
> 
> # Function code section...
> sub procDir {
> 	my $localRoot = shift;
> 	opendir (ROOT, $localRoot) || die "Cannot open directory $pathRoot: $!\n";
> 	my @pathSubdirs = grep {!/^\./ && -d "$localRoot\\$_"} readdir(ROOT);
> 	rewinddir (ROOT);
> 	my @pathFiles = grep {-f "$localRoot\\$_"} readdir(ROOT);
> 	closedir (ROOT);
> 
> 	foreach my $pathFile (@pathFiles) {
> 		if (length("$localRoot\\$pathFile") > $pathMax) {
> 			print "\"$localRoot\\$pathFile\"," . length("$localRoot\\$pathFile") .
> "\n";
> 		}
> 	}
> 	foreach my $pathSubdir (@pathSubdirs) {
> 		$pathDepth++;
> 		procDir("$localRoot\\$pathSubdir");
> 		$pathDepth--;
> 	}
> }
> ------------------------------>8  Cut 8<------------------------------
> 
> Scott
> 



More information about the Phoenix-pm mailing list