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

Re: pack and ASCII

Thread Previous | Thread Next
January 9, 2012 10:52
Re: pack and ASCII
Message ID:
On 01/09/12 11:36, demerphq wrote:
> On 9 January 2012 17:27, Leon Timmermans<>  wrote:
>> On Mon, Jan 9, 2012 at 4:46 PM, John P. Linderman (jpl)
>> <>  wrote:
>>> I was more concerned that the documentation suggested that pack/unpack would
>>> only work on ASCII strings, not on arbitrary strings.  Granted, the length
>>> associated with "a" and "A" might need some amplification, similar to what
>>> is there for the "length" function.  If pack/unpack cannot deal with
>>> non-ASCII strings (I know they work ok for bytes with the high-order bit
>>> on), then what happens when the corresponding argument includes non-ASCII
>>> characters?  -- jpl
>> Well, your question led me to discover what I consider to be a bug:
>> perl -E 'use utf8; my $packed = pack "A*", "ţ"; say utf8::is_utf8($packed);'
>> 1
> Interesting. I think I agree that this is a bug.
>> Personally, it makes no sense to me to pass pack "A" a character
>> string in the first place, if only because it relies on the internal
>> encoding of perl for its result, but to return a character string is
>> just plain wrong.
> A and in particular Z are there so you can easily create data
> structures in Perl which can be passed into C/XS.
Among other things.  There's More Than One Way to Use It:-)  I use pack 
to create fixed-length records that I can write to external storage, 
read back, and re-create the original data.  There would seem to be no 
particularly good reason not to allow, say, utf8 encoded strings to 
participate, as long as I am careful to allow enough space to avoid 
truncating (just as is the case with ASCII strings).  Padding, either 
with blanks or '\0's, seems to be something that is reasonable, but it 
interacts with encodings in ways that I don't pretend to understand.  -- jpl
> cheers,
> Yves

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