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


In reply to Recursive file opening: reading an undef at the end of file by thor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.