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

I'm finishing up v0.01 of Excel::Template for posting to CPAN and I've run into an interesting situation with CGI scripts. I've got MP2 installed, but am using it right now for CGI scripts. In that setup, it looks like MP1 (no $ENV{MOD_PERL} is defined). But, you cannot TIEHANDLE to Apache - you have to tie to Apache::IO or Apache::Filter. But, AFAIK, those don't exist under MP1. I've got the following code written, but it doesn't work quite right ... any suggestions?
sub output { my $self = shift; # mod_perl 2.x if ( $ENV{MOD_PERL} ) { eval { require Apache::IO; Apache::IO->import; }; if ($@) { die "Cannot figure out what to do under Apache!\n"; } tie *XLS, 'Apache::IO'; binmode *XLS; return $self->write_file(\*XLS); } # mod_perl 1.x (or CGI under mod_perl 2.x) elsif ( $ENV{GATEWAY_INTERFACE} ) { my $module_name; eval { require Apache; Apache->import; $module_name = 'Apache'; }; if ($@) { die "Cannot figure out what to do under Apache!\n"; } tie *XLS, $module_name; binmode *XLS; return $self->write_file(\*XLS); } # CGI (or non-WWW) behavior else { $self->write_file('-'); } }

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Spreadsheet::WriteExcel under mod_perl 2
by perrin (Chancellor) on Nov 20, 2003 at 19:23 UTC
    Why don't you simply print to STDOUT? That does the right thing under both versions of mod_perl.

    UPDATE: I didn't realize you need to pass a filehandle to Spreadsheet::WriteExcel. I think you could still pass STDOUT though. However, I noticed some problems in your code. For one thing, $ENV{MOD_PERL} is defined under mod_perl 1.x as well. See this for more info.

      Can I just do this, then?
      sub output { my $self = shift; binmode STDOUT; $self->write_file(\*STDOUT); }

      In other words, will that work for the three situations I've outlined? (Are there others??)

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        It seems like it ought to work to me. Try it.
Re: Spreadsheet::WriteExcel under mod_perl 2
by jmcnamara (Monsignor) on Nov 21, 2003 at 11:28 UTC

    The CGI and MP1 syntax for use with Spreadsheet::WriteExcel are correct.

    I don't have a mod_perl 2 setup so I haven't been able to test it but the CGI method is also reported to work for MP2. See here.

    Update: There is now a mod_perl 2 example in the standard distro.

    --
    John.