Jarkko Hietaniemi <jhi(_at_)iki(_dot_)fi> writes:
Nick Ing-Simmons <nik(_at_)tiuk(_dot_)ti(_dot_)com> writes:
My names were meant to be used like this:
sysread(Handle,$buffer,...); # buffer seq of bytes
my $str = utf8_to_chars(substr($buffer,$start,$len));
# now we have string of chars and we can use char ops ...
my @words;
foreach (split(/\s/,$str)
{
push(@words,ucfirst(lc($_)));
}
my $newstr = join(' ',@words);
# get back byte stream that protocol needs
my $bytes = chars_to_utf8($newstr);
syswrite(Handle,$bytes);
So now that looks like this - _IFF_ I am reading Encode pod correctly:
sysread(Handle,$buffer,...); # buffer seq of bytes
# choose the bytes
my $str = substr($buffer,$start,$len);
# Now see what those utf8 bytes mean...
defined(utf8_to_chars($str)) || die "Malformed data";
# now we have string of chars and we can use char ops ...
my @words;
foreach (split(/\s/,$str)
{
push(@words,ucfirst(lc($_)));
}
my $newstr = join(' ',@words);
# get back byte stream that protocol needs
my $bytes = $newstr;
defined(chars_to_utf8($bytes)) || die "Chars out of range";
syswrite(Handle,$bytes);
The 'in-place' stuff is a slight pain - these thing become chop-like.
I cannot just say
syswrite(Handle,chars_to_utf8(join(' ',map( { ...} ))));
But I guess the function style makes it hard to test for error cases.
As this is a _perl_ API why cannot we return the string and use
length() on it if we want to know that?
--
Nick Ing-Simmons <nik(_at_)tiuk(_dot_)ti(_dot_)com>
Via, but not speaking for: Texas Instruments Ltd.