http://www.perlmonks.org?node_id=204035

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

In my script I have a place where I run chunks of code and being that I am a lazy programmer I would like to capture STDOUT and combine it with any data I decide to return.
sub foo { my $captured_printed_data; my $SH = tie *STDOUT, 'IO::Scalar', \$captured_printed_data; $return_data = eval $module_code; $return_data = "$captured_printed_data \n$return_data" if(defined($c +aptured_printed_data) || ($captured_printed_data ne '')); close $SH; untie *STDOUT; return $return_data; }
When I run this using Komodo or command line it works just fine but when I run the script in Apache it doesn't display anything. Like the STDOUT is still tied. I am running Apache 1.3.x on Linux.

--BigJoe

Learn patience, you must.
Young PerlMonk, craves Not these things.
Use the source Luke.

Replies are listed 'Best First'.
Re: IO::Scalar on Apache Weirdness
by Zaxo (Archbishop) on Oct 09, 2002 at 20:10 UTC

    Apache does its own magic with STDOUT, you may be running into conflicts. Try localizing STDOUT within your sub:

    sub foo { local *STDOUT; # etc. }

    After Compline,
    Zaxo

      I tried your suggestion but it runs into the same issue. I do think it is like you said about Apache's magic. Thanks for the answer though.

      --BigJoe

      Learn patience, you must.
      Young PerlMonk, craves Not these things.
      Use the source Luke.
Re: IO::Scalar on Apache Weirdness
by Chmrr (Vicar) on Oct 10, 2002 at 01:43 UTC

    As with all things mod_perl, the Guide will be your guide. Check out this section and the one following it, specifically.

    Update: I originally read this assuming that this was running under mod_perl, not as a cgi. Ignore the above if the latter is true. ;>

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

      Thanks I will look into that. Because you were correct in assuming that I am running mod_perl.

      --BigJoe

      Learn patience, you must.
      Young PerlMonk, craves Not these things.
      Use the source Luke.
      Thanks for the link. I walked through the section in the mod_perl guide and it works!!

      --BigJoe

      Learn patience, you must.
      Young PerlMonk, craves Not these things.
      Use the source Luke.
Re: IO::Scalar on Apache Weirdness
by blahblahblah (Priest) on Oct 10, 2002 at 02:31 UTC
    I've used IO::Scalar in cgi scripts on apache before, and your code looked pretty similar to mine, so I thought I'd see what was different. In my code I never did anything with the return value from tie, and I think that's what's breaking for you. It's the "close $SH" line that's the problem. I tried the following script on my apache, and did not see any output:
    #!/usr/local/bin/perl print "Content-type: text/html\n\n"; use IO::Scalar; my $output; my $SH = tie *STDOUT,'IO::Scalar', \$output; print "hey\n"; $result = "output: $output"; close $SH; untie *STDOUT; print $result;
    Then I commented out the "close" line, and it worked! I'm not sure why -- I would have expected to at least see "output:" no matter what.