in reply to Help adding a worksheet in a subroutine with Spreadsheet::WriteExcel

The documentation for Spreadsheet::WriteExcel says:

In addition, close() may be required to prevent perl's garbage collector from disposing of the Workbook, Worksheet and Format objects in the wrong order. Situations where this can occur are:

The reason for this is that Spreadsheet::WriteExcel relies on Perl's DESTROY mechanism to trigger destructor methods in a specific sequence. This may not happen in cases where the Workbook, Worksheet and Format variables are not lexically scoped or where they have different lexical scopes.