dMv (daemonv) wrote,
dMv
daemonv

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";
...
}
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded 

  • 0 comments