in reply to Saving data to file

$main::data[0] accesses an element of the package global array  @main::data but there is no such array.

There is a package global scalar  $main::data that has been initialized with a reference to an anonymous array. An element of this anonymous array can be accessed by an expression like  $main::data->[0] or (shudder)  $main::data->[$main::i]

Had these variables been lexicals, Perl would have told you of the absence of  @data given that you are enabling warnings and strictures. As all your variables are fully-qualified package global variables, no such hints can be given.

Update: It's a small point in the wider context of what you are doing in the OPed code, but I'm curious why the single lexical you have chosen to use in your code is in the  output() subroutine: the  $fh lexical file handle. It is very common still to see code written using package global file handles. Why, I wonder, did you choose a lexical in this instance? (Although package file handles are still very common, they are not IMHO very kosher.)