perl outrage
Oct. 23rd, 2003 05:04 pmnon-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";
off topic...
Date: 2003-10-24 12:20 am (UTC)Re: off topic...
Date: 2003-10-24 01:22 am (UTC)augh!
Date: 2003-10-24 12:35 am (UTC)Attempt at answer to bonus question: 1 proc for csh invocation, 1 proc for cat, and 1 for the anonymize. So three additional processes to add to the Perl process? Or am I missing something?
Re: augh!
Date: 2003-10-24 12:53 am (UTC)yup, I think you missed one.
- Perl starts a process to handle backticks.
- The backtick-ed shell starts csh (let's not even talk about why csh is evil).
- cat does indeed require its own process.
- As does anonymize.
- And then, there's the Perl program itself
making a total of five by my count. The whole thing could have been done like so: For a total cost of three processes and much less brain-gnawing punctuationRe: augh!
Date: 2003-10-24 04:51 am (UTC)test2.pl just outputs its own pid, and I consistently got a pid one off of the initial pid, like so:
So it seemed that the bash component of the qx() wasn't invoking its own process, since it would have to get a pid before test2.pl did, but the numbers are sequential. However, doing it with csh I got:
Somehow these two scripts are 21 process increments apart. It's not clear to me how that's happening. Do you know, am I misreading something here, is my testing procedure flawed, or is the csh call actually somehow grabbing 21 processes (or causing the OS to skip 21 pids?). I ran it repeatedly and each time I get a skip of 21 pids between test.pl and test2.pl.
Weird.
Re: augh!
Date: 2003-10-24 06:05 am (UTC)http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
Re: augh!
Date: 2003-10-24 06:54 am (UTC)Of course, if the 21 pid thing is really csh, that amounts to something like 25 additional procs to this script for each invocation of that system call! :) Woooo.