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

  • I don't update frequently because I respect you.

    Housing: We've got two contenders and some really fun stories about trying to buy a house in the begining of a buyer's market. Our poor young…

  • (no subject)

    Vanya finished. Rus. Ukr. Mold. Amer. CH. Vlastelin Udachi Iz Podnebesija. I moved all of the big items from my apartment into storage this…

  • Disperse the Stuff

    If you have an interest in stuff, or free beer, or feel like hanging out, I will be at my place most of Sunday (24th of July). Stop by late afternoon…

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded 

  • 0 comments