I don't know the CGI specifics, but yes if you want to output binary info (and all files can be output as a direct binary copy of the file), something like this (code snippet that I had laying around from long ago):
open(my $inbin, '<', $infile) || die "unable to open $infile"; binmode($inbin) || die "unable to set binmode $infile; open (my $outbin, '>', $outfile) || die "unable to open $outfile"; binmode($outbin) || die "unable to set binmode $outfile"; while (read($inbin, my $buff, 8 * 2**10)) { print $outbin $buff; } close($inbin) || die("unable to close $infile"); close($outbin) || die("unable to close $outfile");
print will know how may bytes of the possible 8K are used in the $buff and that many bytes are output. In your case, you are using STDOUT, I think you also have to close it like I did in the file -> file version. Besides flushing any intermediate stuff, also says "EOF". There might be some alternate way when dealing with STDOUT. There are unbuffered versions, sysread and syswrite, I would read the man pages carefully. Don't mix buffered and non-buffered output to the same file handle.

You asked another question, don't know if it is relevant given the above, but for example on Unix, the way that the binary file test operator works, is that the file is opened and some amount of the file is read - there is an algorithm to decide whether this is a binary file or not. Anyway, you have the file, and you can just to a -b test on it, if that was of interest. However with an attachment, you may not care.

PS: I don't know how setting binmode could fail, but I just kept that stuff when I copied and pasted. I don't think the die message is necessary.


In reply to Re^3: printing file downloads by Marshall
in thread printing file downloads by Anonymous Monk

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.