grashoper has asked for the wisdom of the Perl Monks concerning the following question:

Since I am still having my problem with the toc file I thought maybe I should try creating my own, but I cannot get that to work either. i get following error(s). Global symbol $outputfile requires explicit package name at toc.pl line 39 Global symbol $outputfile requires explicit package name at toc.pl line 40 here is the code
#!/usr/bin/perl -w # # To index an entire directory use: # perl toc.pl *.html # use strict; # holds the name of each file # as it is being processed. my($file); # holds the text of the heading # (from the anchor tag). my($heading); # holds the last heading level # for comparision. my($oldLevel); # holds each line of the file # as it is being processed. my($line); # used as temporary variables # to shorten script line widths my($match); my($href); # holds the name of the heading # from the anchor tag. my($name); # holds the level of the current heading. my($newLevel); my($outputFile); # First, I open an output file and print the # beginning of the HTML that is needed. # $outputFile = "fulltoc.htm"; open(OUT, ">$outputFile"); print OUT ("<HTML><HEAD><TITLE>"); print OUT ("Table of Contents\n"); print OUT ("</TITLE></HEAD><BODY>\n"); # Now, loop through every file in the command # line looking for Header tags. When found, Look # for an Anchor tag so that the NAME attribute can # be used. The NAME attribute might be different # from the actual heading. # foreach $file (sort(@ARGV)) { next if $file =~ m/^\.htm$/i; print("$file\n"); open(INP, "$file"); print OUT ("<UL>\n"); $oldLevel = 1; while (<INP>) { if (m!(<H\d>.+?</H\d>)!i) { # remove anchors from header. $line = $1; $match = '<A NAME="(.+?)">(.+?)</A>'; if ($line =~ m!$match!i) { $name = $1; $heading = $2; } else { $match = '<H\d>(.+?)</H\d>'; $line =~ m!$match!i; $name = $1; $heading = $1; } m!<H(\d)>!; $newLevel = $1; if ($oldLevel > $newLevel) { print OUT ("</UL>\n"); } if ($oldLevel < $newLevel) { print OUT ("<UL>\n"); } $oldLevel = $newLevel; my($href) = "\"$file#$name\""; print OUT ("<LI>"); print OUT ("<A HREF=$href>"); print OUT ("$heading</A>\n"); } } while ($oldLevel--) { print OUT ("</UL>\n"); } close(INP); } # End the HTML document and close the output file. # print OUT ("</BODY></HTML>"); close(OUT);
now I get the following error. readline () on closed filehandle INP at toc.pl line 60

Replies are listed 'Best First'.
Re: generate a new toc
by FunkyMonk (Bishop) on Feb 20, 2008 at 20:39 UTC
    You haven't declared $outputfile with my

    update

    and there's a lot more variables that need mying too.

Re: generate a new toc
by NetWallah (Canon) on Feb 20, 2008 at 21:37 UTC
              ....now I get the following error. readline () on closed filehandle INP at toc.pl line 60

    You need to do some error-checking on file open:

    open(INP, "$file") or die "Could not open file $file : $!\n";

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

      OK I tried to do open (INP, "+>" $file)or die "Could not open file $file : $!\n"; now I get a scalar found where operator expected error..
        OK I tried to do open (INP, "+>" $file)or die

        The arguments of open() are separated with commas. You mean

        open (INP, "+>", $file) or die;

        Don't put single variables into double quotes, it is a useless interpolation (except when you use a reference of a package which overloads stringification, and know what you're doing).

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}