Your code is a snippet that is designed to work as part of a CGI script that you'd put on a web server and run via a web browser. The CGI script would be written to work with a framework called CGI::Application. So to run your code you'll need to install CGI::Application and a module called CGI::Application::Plugin::Output::XSV -- both are available at metacpan.org
Here's how I am able to get your snippet to work:
First I create a file named index.cgi which I put into a directory named /www/cgi-bin/MyOutputXSV
My perl executable (filename is perl) is in a directory called /opt; change the first line of the script to point to the installed perl executable file on your web server.
#!/opt/perl
use strict;
use warnings;
use lib ( '/www/cgi-bin/MyOutputXSV' );
use MyOutputXSV;
my $app = MyOutputXSV->new();
$app->run();
Next I create a file named MyOutputXSV.pm and put it into that same directory:
package MyOutputXSV;
use strict;
use warnings;
use parent 'CGI::Application';
use CGI::Application::Plugin::Output::XSV qw(:all);
# headers generated will be [ "first", "last", "phone" ]
sub setup {
my $self = shift;
$self->run_modes(
'generate_report' => 'generate_report',
);
$self->start_mode('generate_report');
}
sub generate_report {
my $self = shift;
return $self->xsv_report_web({
values => [
{ first_name => 'Jack',
last_name => 'Tors',
phone => '555-1212' },
{ first_name => 'Frank',
last_name => 'Rizzo',
phone => '555-1515' },
],
fields => [ qw(first_name last_name phone) ],
headers_cb => sub {
my @h = @{ +shift };
s/_name$// foreach @h;
return \@h;
},
});
}
1;
Next I change the Linux file permissions on index.cgi to be 705 (which lets my Apache web server read and execute the script) and then I run the script by entering https://mysitename.com/cgi-bin/MyOutputXSV/index.cgi into my web browser.
The script then returns data as a binary file named download.csv, which my browser detects and uses to present a popup box to me that asks what program I'd like to use to open it. I have Excel on my Windows 10 PC so that's what I tell the browser. Then Excel opens and I see the data in a 3x3 set of cells:
first last phone
Jack Tors 555-1212
Frank Rizzo 555-1515
|