Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Example using CGI::Application::Plugin::Output::XSV

by logangha (Acolyte)
on May 04, 2020 at 23:01 UTC ( [id://11116464]=perlquestion: print w/replies, xml ) Need Help??

logangha has asked for the wisdom of the Perl Monks concerning the following question:

I have tried to run below code, saving its content into a file and then running

perl filename.pl

Below is the code

use CGI::Application::Plugin::Output::XSV qw(:all); # headers generated will be [ "first", "last", "phone" ] 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; }, });

Some recommendation to accomplish this? Or which are the steps to follow to create a csv file? Some complete example?

Replies are listed 'Best First'.
Re: Example using CGI::Application::Plugin::Output::XSV
by davebaker (Pilgrim) on May 05, 2020 at 02:53 UTC

    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

      A lot of thanks davebaker !!!
      I will prove it
      And I will let you know if I can make it

        Super!

        If you've never installed a Perl module before, you might want to go with one of the shorter alternatives presented later in this thread. I was just wanting to make the snippet work, which would require the other modules.

        Installing Perl modules often isn't as simple as copying files from metacpan.org -- instead, you'd typically want to use a utility program called cpan or cpanm that is run from a command line. If you have Perl already installed, you might be able to do that. You'd type "cpan CGI::Application" and then (after that module and any dependencies are installed automatically) "cpan CGI::Aplication::Plugin::Output::XSV" (without the quotation marks).

Re: Example using CGI::Application::Plugin::Output::XSV
by choroba (Cardinal) on May 05, 2020 at 10:18 UTC
    If you just want to create a csv file from the input structure without CGI, you can use Text::CSV_XS :
    #!/usr/bin/perl use warnings; use strict; use Text::CSV_XS qw{ csv }; my @header = qw( first_name last_name phone ); csv(in => [ \@header, map [ @$_{@header} ], { first_name => 'Jack', last_name => 'Tors', phone => '555-1212' }, { first_name => 'Frank', last_name => 'Rizzo', phone => '555-1515' }, ]);
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      You don't need the map. AOH is supported:

      #!/usr/bin/perl use warnings; use strict; use Text::CSV_XS qw( csv ); my @header = qw( first_name last_name phone ); csv (in => [{ first_name => "Jane", last_name => "Rizzoli", phone => "555-1212" }, { first_name => "Maura", last_name => "Isles", phone => "555-1515" }, ], headers => \@header, );

      Or if you have the hash list already.

      my @header = qw( first_name last_name phone ); my @aoh = ( { first_name => "Jane", last_name => "Rizzoli", phone => "555-1212" }, { first_name => "Maura", last_name => "Isles", phone => "555-1515" }, ); csv (in => \@aoh, headers => \@header);

      Enjoy, Have FUN! H.Merijn

      Thanks choroba
      I will keep it in mind

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11116464]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-29 01:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found