love, play & inquiry (trochee) wrote,
love, play & inquiry
trochee

perl outrage

non-geeks, avert thine eyes.

I just had to rant somewhere about a very bad paragraph of perl:

opendir (DIR, $dir) || die "cannot open $dir\n";
while ($_ = readdir(DIR))
{
    if(/^foo/){;}       #OK
    else{next;}

   if(/^bar/){next;}   # skip bar data
   print STDERR "$_ ";
   `csh -c "cat $dir/$_/* | $pipethrough >> $dir/temporary"`;
}
`csh -c "cat  $dir/temporary | anonymize > $output"`;
print STDERR "\nDone\n";
closedir(DIR);
`rm $dir/temporary`;
exit;
# more code here...

Augh. I'll rewrite this when I have time.

Bonus question: How many processes are started by the following?

`csh -c "cat  $dir/temporary | anonymize > $output"`;

Here's my rewrite:
opendir (DIR, $dir) or die "cannot open $dir: $!\n";
while ($_ = readdir <DIR>)
{
   next unless /^foo/; # must begin with foo
   next if /^bar/;     # skip beginning with bar

   warn "$_\n";
   `cat $dir/$_/* | $pipethrough >> $dir/temporary`;
}
`anonymize < $dir/temporary > $output`;
warn "\nDone\n";

closedir(DIR) or die "couldn't close directory $dir: $!\n";
unlink($dir/temporary) or warn "couldn't unlink $dir/temporary: $!\n";
Subscribe
  • Post a new comment

    Error

    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 7 comments