in reply to generate a new toc

You haven't declared $outputfile with my

update

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