This is a more complete version of the code. I have removed most of the internals of the function that generate the XL sheet and also all the functions in the module that are not used in the code.
The function generate_xl is called from a function in a sub class of CGI::Application.
package PackageName::GenerateXL;
use strict;
use warnings;
use Carp;
use Data::Dump qw( dump );
use English;
use File::Temp;
use File::Spec;
use Params::Validate qw( :all );
use POSIX qw( strftime );
use Spreadsheet::WriteExcel;
use CorrectModuleName::DatabaseRow;
use CorrectModuleName::HashUtils qw( values_sorted_by_key );
use CorrectModuleName::SQLCreator;
use CorrectModuleName::Database;
#Global reference to a Database object
my $global_database;
sub generate_xl{
#validate parameters with Params::Validate
my ( $sheet_name, $database, $parameters ) = validate_pos( @_, 1, {
+isa => 'ERM::Database' }, { type => HASHREF, optional => 1 }, );
$global_database = $database;
#Mapping between the supported sheets and the generate functions
my %supported_sheets = (
vareregistrering => \&_generate_vareregistrering,
nyvare => \&_generate_avtaleskjema_nyvare, #REMOVED IN EXAMPLE
prisendring => \&_generate_avtaleskjema_prisendring, #REMOVED
);
if ( !exists $supported_sheets{ $sheet_name } ) {
confess "$sheet_name is not a supported sheet.\n";
}
#open a temporary file on the server for workbook
my $tmpdir = File::Spec->tmpdir();
my ($filehandle, $filename) = File::Temp::tempfile( "generated_XXXXX
+X", DIR => $tmpdir, SUFFIX => '.xls', UNLINK => 1);
confess "ERROR\n" if !$filehandle;
binmode $filehandle;
my $generated_workbook = Spreadsheet::WriteExcel->new($filehandle);
#EXECUTION NEVER REACHES THIS POINT.
$supported_sheets{$sheet_name}->($generated_workbook, $parameters);
$generated_workbook->close();
close $filehandle;
#read the whole file into a string.
my $excel_string = '';
open( my $FILE, $filename);
binmode $FILE;
while ( my $line = <$FILE> ) {
$excel_string .= $line
}
close $FILE;
return $excel_string;
}
sub _generate_vareregistrering{
my ( $workbook, $parameters ) = validate_pos( @_, { isa => 'Spreadsh
+eet::WriteExcel' }, { type => HASHREF } );
my $productid = $parameters->{ productid };
confess "Cannot generate a 'vareregistrerings' form without a produc
+tid."
if !defined $productid;
#THE ERROR WITH THE WRONG NAME IS HERE, BUT EXECUTION NEVER REACHES
+THIS POINT
my $database_row = WrongModuleName::DatabaseRow->new( { tablename =>
+ 'PRODUCT', conditions => { PRODUCTID => $productid }, database => $g
+lobal_database, } );
#DO LOTS OF STUFF TO GENERATE THE XL SHEET BY CALLING FUNCTION ON $w
+orkbook.
}
1;
By the way, thanks to the person that greatly improved the title.
|