?

Log in

No account? Create an account
Learning Moment - dmv/blogs/lj

> Recent Entries
> Archive
> Friends
> Profile
> dmv/index

Links
dmv/index

Alt. Info Streams
Twitter
Memestream
Flickr Stream

March 14th, 2003


Previous Entry Share Next Entry
10:30 am - Learning Moment
My brain is not quite spun up yet.

# Learning Point:
# When writing a loop that goes through the contents of
# a directory, that causes a new file to be created *in
# that directory*, bad things can happen if the new file
# matches the criteria of the file name filter *and* the
# directory listing is dynamic.


(run as script.pl something.c because you forgot that you don't have to give it the source file(s) on the command line anymore. Not great code sample below. And yes, I fixed the problem in one of the dozen or so ways I can imagine doing so.)


#!/usr/local/bin/perl
$postfix = ( $#ARGV > -1 ) ? shift @ARGV : ".unc";

sub annotate( $file );

opendir( HERE, "." ) || die "can't open dir .\n";

while ( $f = readdir HERE )
{
&annotate($f) if ($f =~ /\.c$/);
}

sub annotate( $file )
{
my $file = shift @_;
print "Annotating $file.\n";
open( SRC, "<$file" ) || die "can't read file $file\n";;
open( ANN, ">$file$postfix" ) || die "can't open file $file$postfix\n";
...
}

(Leave a comment)


> Go to Top
LiveJournal.com