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

I'm on mod_perl, attempting to write-out a file previously slurped with read_file. File length is, say 1024 bytes, but when I download the file, it turns out to be 1025 bytes long.

I changed print $content; to print '1'; and the output I get is "1\x0A".

I don't know how to get rid of that, it was mangling my uploads too but I worked around that with read_file. For reasons unknown, I can't use write_file on STDOUT.

Any ideas to get rid of that \x0A?

Replies are listed 'Best First'.
Re: Trash in my prints
by blindluke (Hermit) on Aug 18, 2011 at 20:35 UTC

    print() in mod_perl adds a trailing newline. Try using binmode with the filehandle.

    binmode STDOUT; print '1';

    Good luck,
    Luke Jefferson

      That's a bug in mod_perl. Even if it's documented, it's still a bug. It's a bug in the design.

        That's a bug in mod_perl. Even if it's documented, it's still a bug. It's a bug in the design.

        It is neither

        It is a perl feature, see binmode

Re: Trash in my prints
by blue_cowdawg (Monsignor) on Aug 18, 2011 at 19:50 UTC
        Any ideas to get rid of that \x0A?

    what is chomp()?


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

      No go, how would I chomp a print ''; exit;, it still spits a lone \x0A

            No go, how would I chomp a print '';

        Whaaaa?

        You're leaving something out here. \0a (or 0xaH) is a line feed or EOL character. You must have something out there emitting an extra EOL.

        If $text represents the slurped in file and you do a chomp ($line) you should eliminate the EOL character.

        What platform are you running on and what platform did the files come from? I know that WinBloze adds extra \0a characters on normal text files and if you try reading them in Unix it will appear as extra EOLs.


        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Trash in my prints
by Anonymous Monk on Aug 18, 2011 at 19:59 UTC

    Tried that, but print '';exit; gives the \x0A alone.

    Anywhere else to look?

      Try it like this:

      { local $\; print '1'; }

        That did the trick.

        The code where I found this problem spits a file as an attachment to the browser, kinda looks like this:

        sub render_file { my ( $x, %p ) = @_; my $content = read_file( $p{name}, binmode => ':raw' ); print $content; return Apache2::Const::OK; }
        nothing really complex. I didn't know $\ would affect the output of print, or rather, didn't know mod_perl would set it like blindluke says.

        What really strikes me as odd now is the fact that the code hasn't changed in ages and no one had noticed it was broken...

        You learn something new everyday. Thanks everyone!