develooper Front page | perl.perl5.porters | Postings from January 2012

Re: pack and ASCII

Thread Previous
Eric Brine
January 10, 2012 13:47
Re: pack and ASCII
Message ID:
On Tue, Jan 10, 2012 at 4:33 PM, Eric Brine <> wrote:

> Yeah, you're not looking in the right place. You keep looking at how Perl
> stores the string (which is irrelevant) instead of what the string contains.

Showing that it's not only irrelevant in theory:

use strict;
use warnings;
use charnames ':full';

use Inline C => <<'__EOI__';

void testing(SV * sv) {
   STRLEN len;
   char*  buf = SvPVbyte(sv, len);
   STRLEN i;

   printf("%u bytes:", len);  // XXX %u is not portable.
   for (i=0; i<len; ++i)
      printf(" %02X", (unsigned int)(unsigned char)(buf[i]));


sub _u { my $_ = $_[0]; utf8::upgrade($_);   $_ }
sub _d { my $_ = $_[0]; utf8::downgrade($_); $_ }

# One byte in one string format.
testing(_d(pack("A", chr(0xE9))));             # 1 bytes: E9

# One byte in other string format.
testing(_u(pack("A", chr(0xE9))));             # 1 bytes: E9

# Bug: Forgot to encode.
testing(pack("A", "\N{BLACK SPADE SUIT}"));    # DIES with "Wide character
in subroutine entry"

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About