[LA.pm] C-style Perl vs. regex

Andy Lester andy at petdance.com
Thu Feb 9 08:05:41 PST 2006


On Wed, Feb 08, 2006 at 08:35:51AM -0800, Robin Rowe (rower at movieeditor.com) wrote:
> Please let me know how this code could be improved.
> my @domains =
> ('cinepaint.org','linuxmovies.org','risingcast.com','screenplaylab.com','smashphone.com');

The qw() operator makes these lists nicer:

my @domains =
  qw( cinepaint.org linuxmovies.org risingcast.com screenplaylab.com smashphone.com );

> 	my $zone = join "", $domain, ".zone";

Use interpolation

  my $zone = "$domain.zone";

> 	open(FILE,">>$hosts_addon_file") or die("can't open $hosts_addon_file");
> 	my $hosts = join "", " mail.", $domain, " www.", $domain;
> 	print FILE $hosts

Use three-parm open and lexical filehandles:

  open( my $fh, ">>", $hosts_addon_file )
      or die "Can't open $hosts_addon_file: $!\n";   # $! is error message
  print $fh " mail.$domain www.$domain"

> 	unlink($hosts_addon_file);
> 	unlink($local2_file);

Check your return codes

  unlink( $hosts_addon_file ) or warn "Can't unlink $hosts_addon_file: $!\n";

xoa

-- 
Andy Lester => andy at petdance.com => www.petdance.com => AIM:petdance


More information about the Losangeles-pm mailing list