Hi all,

I'm sure this problem is due to my ignorance of these issues, but I'm puzzled by this. I have a script, where I want to give the user the option of saving the output (from a database) to a file (which will eventually generate a pdf), or showing it in the browser. I'm using CGI.pm mostly for the output. I can get one or the other, but can't get both. Here's a bit of the code I'm working on:

if ($query{submit} eq "PDF") { open STDOUT, ">tempfile.html" or graceful_exit("can't open fil +e"); }
Then there are a bunch of print statements, and the like, and then I close STDOUT.

Choosing to display the output ($query{submit} equals "HTML" in that case), works fine, but if PDF is chosen, the file is empty. (There are no permissions or filesystem issues.

If, instead I use the following code:

if ($query{submit} eq "PDF") { open OUTPUT, ">tempfile.html" or graceful_exit("can't open fil +e"); } else { open OUTPUT, '>STDOUT'; # I know this is wrong }
Followed by print OUTPUT statements, the file gets created fine, but there is no output to the web browser.

I can't seem to find anywhere (perlopentuit or the camel book), the syntax on an open statement that allows me to open STDOUT as a regular filehandle.

I'm stumped. Because the output is generated from database output and manipluation of that data, I don't want to create redundant sets of print statements, one to a file and the other to the browser. The other option I could choose is to put this all in a subroutine, and then send the output to one or another place, depending on choice. But this seems like it ought to be straightforward.

Thanks!

In reply to Redirection of output by michellem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.