Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • The problem with doing things like this is that you are completely ignoring the encoding. Encoding is one of the reason why there are parallel IO hierarchies in Java: InputStream [sun.com]/OutputStream [sun.com] vs Reader [sun.com]/Writer [sun.com]. The latter understand characters, the former understand bytes. Unfortunately, every time you go near a Reader/Writer, you need to specify a character encoding. As a rule, I try to prefer Reader/Writer where I can.

    Classes to look at:

    • The commons io [apache.org] library has implementations for useful stuff like this -- see IOUtils#toString() for a stream (with default encoding or with a specified one).
      • Thanks for the pointer!

        --
        J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
    • Thank you for the useful information!

      For the record, ByteArrayOutputStream.toString() does offer a version which specifies the character encoding as a parameter. But that's definitely something I wasn't thinking about (especially since I just wanted it for logging). My personal wiring is probably far too close to Paul Graham's recent unforgivable sin in making Arc ASCII-only for the taste of people around here, or anyone steeped in Java. It's something I'd prefer to leave as Somebody Else's Problem as

      --
      J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
      • As to thinking about encodings, I'm completely in agreement with you — everything should use UTF-8!
  • …Scheme:

    (let ((str (with-output-to-string
                 (write exprs))))
      (do-something-to str))
    --
    Ordinary morality is for ordinary people. -- Aleister Crowley
    • Or IO::Scalar in Perl. :)

      --
      J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
      • You no longer need IO::Scalar since perl 5.8. Plain old open will now accept a scalar ref:

          my $str
          open my $fh, '>', \$str;
        • Cool! Thank you!

          --
          J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers