Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Re: capturing STDOUT

by nop (Hermit)
on May 02, 2001 at 01:48 UTC ( #77148=note: print w/ replies, xml ) Need Help??


in reply to Re: capturing STDOUT
in thread capturing STDOUT

The cgi.pl example in the distro takes a different approach:

#!/usr/bin/perl -w ###################################################################### +######### # # Example of how to use the Spreadsheet::WriteExcel module to send an +Excel # file to a browser in a CGI program. # # On Windows the hash-bang line should be something like: # #!C:\Perl\bin\perl.exe # # Dec 2000, John McNamara, jmcnamara@cpan.org # use strict; use Spreadsheet::WriteExcel; # Set the filename and send the content type my $filename ="cgitest.xls"; print "Content-type: application/vnd.ms-excel\n"; print "Content-Disposition: attachment; filename=$filename\n\n"; # Create a new workbook and add a worksheet. The special Perl filehand +le - will # redirect the output to STDOUT # my $workbook = Spreadsheet::WriteExcel->new("-"); my $worksheet = $workbook->addworksheet(); # Set the column width for column 1 $worksheet->set_column(0, 0, 20); # Create a format my $format = $workbook->addformat(); $format->set_bold(); $format->set_size(15); $format->set_color('blue'); # Write to the workbook $worksheet->write(0, 0, "Hi Excel!", $format);
On my browser, this (1) prompts "do you want to open or save this downloaded file?" and (2) opens TWO windows, one blank. Ugh. Can you comment on your excellent tie soln above, and the alternative as per cgi.pl? Thanks!


Comment on Re: Re: capturing STDOUT
Download Code
Re: Re: Re: capturing STDOUT
by jmcnamara (Monsignor) on May 02, 2001 at 03:25 UTC

    On my browser, this (1) prompts "do you want to open or save this downloaded file?" and (2) opens TWO windows, one blank.

    As to why you see two windows, I cannot say. I cannot replicate this behaviour. Maybe someone with more CGI experience could explain this. However, I have noted some small differences in the way Netscape and IE handle the example program. The joys of CGI and browsers, I guess. %^)

    If you wish to stream the file to Excel you can set the Content-type as follows:     print "Content-type: application/vnd.ms-excel\n\n";
    If you wish to prompt the user to view the file or save it with a specified filename you can do something like the following:
    my $filename ="cgitest.xls"; print "Content-type: application/vnd.ms-excel\n"; print "Content-Disposition: attachment; filename=$filename\n\n";
    The example that you posted has been updated in more recent versions of the module to highlight these options. However, the view or save options can be overridden by the client browser.


    Can you comment on your tie soln above, and the alternative as per cgi.pl?

    In a CGI program you have to redirect the Excel file to STDOUT. The easiest way to do this is to use the "Minus" file as a file name in the constructor:     my $workbook = Spreadsheet::WriteExcel->new("-");
    The "Minus" file is documented in perlopentut*. The comment in the above example refers to it as a filehandle but it isn't, it is a file name.

    As such you do not need to use tie at all in this situation. You would only need to use the above technique if you wanted to do something unusual such as stream the file to a browser and save a local copy.

    The option of passing filehandles to the constructor was introduced mainly to allow the files to be streamed over sockets and to allow Spreadsheet::WriteExcel to work with mod_perl.

    John.
    --
    * Does anyone know of anywhere else that this is documented?

      My below is script not generating STDOUT in browser. I followed the instructions u have given bt didn't work.. Can someone help on this..

      #!/appl/CW_NETCOOL/PERL/bin/perl use strict; use Spreadsheet::WriteExcel; use CGI qw(:standard); # Set the filename and send the content type my $filename ="cgitest.xls"; print "Content-type: application/vnd.ms-excel\n"; #print "Content-Disposition: attachment; filename=$filename\n\n"; # Create a new workbook and add a worksheet. The special Perl filehand +le will # redirect the output to STDOUT # binmode(STDOUT); my $workbook = Spreadsheet::WriteExcel->new(\*STDOUT); my $worksheet = $workbook->addworksheet(); # Set the column width for column 1 $worksheet->set_column(0, 0, 20); # Create a format my $format = $workbook->addformat(); $format->set_bold(); $format->set_size(15); $format->set_color('blue'); # Write to the workbook $worksheet->write(0, 0, "Hi Excel!", $format); $workbook->close;

        Here's your problem:

        print "Content-type: application/vnd.ms-excel\n"; #print "Content-Disposition: attachment; filename=$filename\n\n";

        HTTP headers need to end in a double line break. Your Content-Type header has only one. Your Content-Disposition header would do the trick, but it's commented out.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://77148]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (18)
As of 2014-08-29 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (280 votes), past polls