See
Re^3: Portable length() in bytes.. The bottom line is length
will not calculate how many bytes will go out, and syswrite
is expecting a character count. (Don't get upset if what you
are writing is binary data; in that case you should have 8-bit characters.) See some examples (note that v255 is utf-8 encoded in perl, while "\xff" is not, and -CO tells perl that STDOUT expects utf8):
$ perl -we'$x = v255;
{use bytes; print STDERR "len:",($len=length $x),"\n" }
print STDERR "wrote: ",($x = syswrite STDOUT, $x, $len),"\n"'|cat
len:2
wrote: 1
˙
$ perl -we'$x = "\xff";
{use bytes; print STDERR "len:",($len=length $x),"\n" }
print STDERR "wrote: ",($x = syswrite STDOUT, $x, $len),"\n"'|cat
len:1
wrote: 1
˙
$ perl -CO -we'$x="\xff";
{use bytes;print STDERR "len:",($ln=length $x),"\n" }
print STDERR "wrote: ",($x = syswrite STDOUT, $x, $len),"\n"'|cat
len:1
wrote: 1
Aż
$ perl -CO -we'$x=v255;
{use bytes; print STDERR "len:",($len=length $x),"\n" }
print STDERR "wrote: ",($x = syswrite STDOUT, $x, $len),"\n"'|cat
len:2
wrote: 1
Aż
The "length" passed to syswrite is useless; it expects and returns character length and offset. And whether the string being output is 1 byte or 2 bytes, it's just one character, and will be output as either 1 or 2 bytes depending on the output filehandle,
not on how perl has it encoded.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.