You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "Homsher, Dave V." <DV...@bemis.com> on 2000/12/13 21:23:52 UTC

STDOUT issues ...

Hi all ...

I have searched through the archives for an answer and have found a lot of
discussion, but nothing concrete enough to help ...

I am using Spreadsheet::WriteExcel to create an Excel doc (hence the name)
but I keep getting blank documents back.

The module can write to a file or to STDOUT using '-'. From the archives I
see that this is similar to an issue with Apache::Magick where the C STDOUT
(expressed as '-' or '>-') is different from Perl's default STDOUT.

I have also tried the code from the mod_perl guide:
	use constant IS_MODPERL => $ENV{MOD_PERL};
	if (IS_MODPERL) {
	  tie *OUT, 'Apache';
	} else {
	  open (OUT, ">-");
	}

I'm probably not using this as intended, though (an example would help).
Does this go before the handler sub, or in it ???

Here is the code below (BTW it does work as a plain cgi script w/o the
mod_perl additions of course). You can see the different ways I have been
trying to do this ...

package Planning_xls;
# File: /usr/local/apache/lib/perl/Planning_xls.pm

use strict;
use Apache::Constants qw(:common REDIRECT HTTP_SERVICE_UNAVAILABLE);
use Spreadsheet::WriteExcel;

sub handler {
	my $r = shift;

	use constant IS_MODPERL => $ENV{MOD_PERL};
	if (IS_MODPERL) {
	  tie *OUT, 'Apache';
	} else {
	  open (OUT, ">-");
	}

#	*STDOUT = *r{IO};
#	tie *STDOUT => Apache;
#	tie *STDOUT => $r;
#	tie *STDOUT => 'Apache';
#	open(STDOUT, ">-");
#	untie *STDOUT;
#	open(STDOUT, ">&=" . fileno($fh));


	$r->content_type('application/vnd.ms-excel');
#	$r->content_type('text/html');
	$r->send_http_header;

	my $workbook = Spreadsheet::WriteExcel->new("-");
#	my $workbook = Spreadsheet::WriteExcel->new(STDOUT);

	my $worksheet   = $workbook->addworksheet("test");
	my $format      = $workbook->addformat();

	$format->set_bold();
	$format->set_color('red');
	$format->set_align('center');

	$worksheet->write(0, 0, 1);
	$worksheet->write(1, 1, 1.2345);
	$worksheet->write(2, 2, "formatted", $format);

	$workbook->close();

	return OK;

}
1;
__END__

Thanks for any help ... I know I am missing something obvious ...

Dave Homsher
Webmaster
MACtac IT

     "The tree of liberty must be watered
     periodically with the blood of tyrants and patriots alike.
     ... Resistance to tyrants is obedience to God."
	     - Thomas Jefferson

Re: STDOUT issues ...

Posted by Joshua Chamas <jo...@chamas.com>.
"Homsher, Dave V." wrote:
> 
> I am using Spreadsheet::WriteExcel to create an Excel doc (hence the name)
> but I keep getting blank documents back.
> 
> The module can write to a file or to STDOUT using '-'. From the archives I
> see that this is similar to an issue with Apache::Magick where the C STDOUT
> (expressed as '-' or '>-') is different from Perl's default STDOUT.
> 
> ... 
>
>         my $workbook = Spreadsheet::WriteExcel->new("-");
> #       my $workbook = Spreadsheet::WriteExcel->new(STDOUT);
> 

If you don't get STDOUT method to work, you could try writing
it to a file as in

  Apache->register_cleanup(sub { unlink("/tmp/excel.$$"});
  my $workbook = Spreadsheet::WriteExcel->new("/tmp/excel.$$");
  ... then read file & print to output ...

I would use this method first anyway just to make sure 
a spreadsheet gets created before trying to get it to go
straight to STDOUT.  
  
--Josh

Re: STDOUT issues ...

Posted by Stas Bekman <st...@stason.org>.
> I have searched through the archives for an answer and have found a lot of
> discussion, but nothing concrete enough to help ...
> 
> I am using Spreadsheet::WriteExcel to create an Excel doc (hence the name)
> but I keep getting blank documents back.
> 
> The module can write to a file or to STDOUT using '-'. From the archives I
> see that this is similar to an issue with Apache::Magick where the C STDOUT
> (expressed as '-' or '>-') is different from Perl's default STDOUT.
> 
> I have also tried the code from the mod_perl guide:
> 	use constant IS_MODPERL => $ENV{MOD_PERL};
> 	if (IS_MODPERL) {
> 	  tie *OUT, 'Apache';
> 	} else {
> 	  open (OUT, ">-");
> 	}

I should kill this example, or provide more explanation. People are just
copy-n-paste examples without thinking too much

> Thanks for any help ... I know I am missing something obvious ...

This item will show up in the next version of the Guide:

=head1 Redirecting STDOUT into a String

Sometimes you have a situation where a black box functions prints the
output to C<STDOUT> and you want to get this output into a
string. This is just as valid under mod_perl, where you want the
C<STDOUT> to be tied to the C<Apache> object. So that's where the
C<IO::String> package comes to help. You can re-tie() the STDOUT (or
any other filehandler to a string) by doing a simple select() on the
C<IO::String> object and at the end to re-tie() the STDOUT back to its
original stream:

  my $str;
  my $str_fh = IO::String->new($str);
  my $old_fh = select($str_fh);
  
  # some function that prints to currently selected file handler.
  print_stuff()
  
  # reset default fh to previous value
  select($old_fh) if defined $old_fh;



_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide 
mailto:stas@stason.org   http://apachetoday.com http://logilune.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/