Greetings all,
I'm confused with a little piece of code I've written. I was asked to come up with a program that would take two or more files, and tell what action would be taken by the first that would not be taken by any of the others. The file's format is for each line, it is either a colon separated list, starts with an 'I' which means to include another file, comments begin with a #, and blank lines are ignored. So, I recursively parse these files (when I hit an 'I', I open that file and process it). What I find wierd is that the
while(<$fh>) structure seems to be reading an extra line at the end of each file that is more than one level deep in the recursion. I "fixed" the problem by turning warnings off, but for curiosity's sake I would like to know what's going on. Any ideas? My code is below
#!/usr/local/perl/bin/perl -w
use strict;
my %hash;
my $cal = shift;
read_cal($cal, 0);
foreach my $file (@ARGV) {
read_cal($file, 1);
}
foreach my $key (sort keys %hash) {
print "$key\n" if $hash{$key} == 1;
}
sub read_cal {
my $cal = shift;
my $fh;
print "---------------$cal-----------------\n";
my $mode;
open($fh, $cal) or die;
while(<$fh>) {
chomp;
s/\s//g;
next if /^#/;
next if /^\s*$/;
next if $_ eq "";
if (/^I/) {
read_cal(substr($_,1), $mode);
}
print "|$_|\n";
my $schedule = ((split(':'))[-1]);
if (!$mode || exists($hash{$schedule}) ) {
$hash{$schedule}++;
}
}
}
thanks in advance,
thor