Slash Boxes
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.
More | Login | Reply
Loading... please wait.
  • That's a very clear explanation of what I've thought for some time, but was unable to phrase.

    A quick way to upgrade yourself from level 2 to 3 is reading [] ;-).
    • Thank *you* for the good tutorial.

      So, I've been thinking there need to be some standards for CPAN modules to declare if it accept/return strings or bytes. (If they need to handle both)

      For instance, HTML::Parser has an instance method called utf8_mode [].

      Another example (that triggered me to write this entry) is Catalyst's uri_for() method []. At some release the developers changed the implementation to accept only strings (UTF-8 flagged or not) in its %query_values hash.

      Based on the complaints and patches made by
      • by Juerd (1796) on 2008.02.20 21:22 (#61180) Homepage
        Strings or bytes is not the right distinction, because both kinds are strings. I usually call them "text string" and "binary string", or "character string" and "byte string". Sometimes I call the former "Unicode string" to emphasize that all text strings are Unicode strings.

        A trap is the UTF-8 string, which is a byte string representing characters, and has "the flag" off (which to perluninewbies is confusing because this flag is called UTF8). Compare this with the result of pack "N*", LIST, which is a byte string representing numbers. You'll note that UTF-32 looks a lot like pack N* in practice :).

        I strongly believe that the behavior of accepting both UTF-8 encoded strings, and SvUTF8 flagged strings, in the same function, is wrong.

        I also strongly suggest that any use of "use bytes" and functions in the utf8:: namespace, is misguided. If you want to use bytes, either have a function that deals only with byte input, or have a function that deals only with text input and encode it yourself. The stuff does not encode, it provides a view into perl's internal byte buffer. For text strings, the encoding of this buffer may be either utf8 or latin1.

        For DBIx::Simple I have a similar dilemma. I can easily add automatic decoding/encoding for database values, and would love to do so. But databases can also be used for storing binary values. My current plan is to release a very simple CPAN module:

        (Typed in my browser, untested.)

        package BLOB;
        sub mark {
            my $class = shift;
            my $self = \shift;
            bless $self, $class;
        use base Exporter;
        our @EXPORT = qw(is_blob);
        sub is_blob {
            my $blob = shift;
            return undef if not blessed \$blob;
            return (\shift)->isa('BLOB');
        =head1 SYNOPSIS
            use BLOB;
            print is_blob($jpeg_data);  # 1
            my $bytes = is_blob($foo) ? $foo : encode_utf8($foo);
        Then, DBIx::Simple doesn't have to parse SQL and know which columns are blobs: the user can mark a string as a BLOB and I can just skip encoding for those values. PerlIO layers could be told to skip things marked as BLOB when encoding too.

        Functions that for some reason need to accept both kinds of string (which can be necessary to support existing stuff, or in heavily abstracted code, but should generally be avoided), can then just tell the user to mark byte strings as blobs before passing them.

        To take it one step further, it should have a mechanism like encoding::warnings in place to disallow (fatal error would be best, I believe) upgrading a BLOB.
        • Hm, just to clarify, I prefer to use characters vs. bytes like you say. If I sometimes use "strings" somewhere, it's just a slip of keystrokes, or I meant Unicode strings instead.

          And also, I'm a bit afraid that you misunderstood what I meant with mention to I didn't mean we should call "use bytes" in this situation to force string operations to be bytes-wise. Not at all.

          I meant declaring "use bytes" *might be* a good way for programmers to tell the module authors "Hey I want this module to do what
          • "use bytes;" is lexical: it cannot influence what a module does. I don't know who to thank for this, but I'm happy that at least my code won't be broken at a distance by the numerous uninformed and misinformed people who throw a "use bytes" at their code to replace one kind of (for them) vague behavior with another kind of vague behavior. :)

            Experience has show so far that the only workable way of supporting both byte strings and text strings in your function, is to provide two separate functions, or a mecha
            • Agreed in both: we should use two different functions to accept characters or bytes, and also would be useful to DWIM. :)