in reply to Re^2: syswrite numbers, not strings
in thread syswrite numbers, not strings

I have studied the pack, unpack, and packtut extensively, and conclude that they do not get the chance to come into play: Perl is tinkering with the write.

Sorry, but you are wrong. Your problem is one of your perception. Perl doesn't "tinker with the write".

A byte with the numeric value of 44 is simply the bit pattern 00101100. It will only be interpreted as an ascii character by whatever you are using to look at it.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: syswrite numbers, not strings
by akujbida (Acolyte) on Dec 18, 2014 at 07:14 UTC
    I'm using interceptty to watch the serial port, so I am confident about what is really happening, and understand why you pointed out that trap.
    syswrite(FH, 45); #puts 0x34 0x35 onto the wire, as if I said '45'
    and I do call that tinkering. I'm probably wrong in here somewhere, though, usually am.

    I'm not sure what I've done, but now syswrite(FH, pack('C', $packet)) is working. Will test more.

      Remember this is Perl, where conversion between strings and numbers is almost always automatic and transparent, so "tinkering" might be too strong a word. Although one could argue the documentation isn't perfectly clear on this, it's at least strongly implied that syswrite writes a byte array / string, not individual numbers as bytes, so the 45 gets converted to "45", which is 0x34 0x35. (That's one of the reasons pack exists.)

        Indeed, and I think that sums up the situation very well. I have said tinkering, because in this case it bit me, though I have almost certainly had beneficial use of the 'tinkering' at other times.