In buildtoc, move the local $/ = ''; to the scope of the file it relates to.