develooper Front page | perl.perl5.porters | Postings from July 2019

[perl #134238] sysread OFFSET is bytes or characters?

Thread Previous
From:
Tony Cook via RT
Date:
July 17, 2019 01:54
Subject:
[perl #134238] sysread OFFSET is bytes or characters?
Message ID:
rt-4.0.24-14232-1563328462-836.134238-15-0@perl.org
On Tue, 09 Jul 2019 21:17:08 -0700, jim.avera@gmail.com wrote:
> Then would it be correct to change sysread()'s pod to just say LENGTH
> &
> OFFSET specify octets, and remove all mention of characters
> 
> (except for a note the prior to v5.30.0 sysread could be used with
> non-raw filehandles but with inconsistent behavior...or something).
> -Jim

Since the SCALAR might contain characters over 0xff, the offset is in characters, and it works:

$ echo foo | ./perl -Ilib -MDevel::Peek -e '$x = "\x{100}"; sysread(STDIN, $x, 1, 1); Dump $x'
SV = PVMG(0x55ef89817280) at 0x55ef897e7b20
  REFCNT = 1
  FLAGS = (SMG,POK,pPOK,UTF8)
  IV = 0
  NV = 0
  PV = 0x55ef897da140 "\304\200f"\0 [UTF8 "\x{100}f"]
  CUR = 3
  LEN = 10
  MAGIC = 0x55ef897ca530
    MG_VIRTUAL = &PL_vtbl_utf8
    MG_TYPE = PERL_MAGIC_utf8(w)
    MG_LEN = -1

$ perl -le 'print "\xFF"' | ./perl -Ilib -MDevel::Peek -e '$x = "\x{100}"; sysread(STDIN, $x, 1, 1); Dump $x'
SV = PVMG(0x556b74aed4b0) at 0x556b74abdb30
  REFCNT = 1
  FLAGS = (SMG,POK,pPOK,UTF8)
  IV = 0
  NV = 0
  PV = 0x556b74aaf5f0 "\304\200\303\277"\0 [UTF8 "\x{100}\x{ff}"]
  CUR = 4
  LEN = 10
  MAGIC = 0x556b74aa0530
    MG_VIRTUAL = &PL_vtbl_utf8
    MG_TYPE = PERL_MAGIC_utf8(w)
    MG_LEN = -1

LENGTH is now always in bytes though.

The documentation for sysread() does appear to use characters/bytes in the right places, so I don't know if anything needs to be done here (marked with **):

Attempts to read LENGTH **bytes** of data into variable SCALAR from the
specified FILEHANDLE

An OFFSET may be specified to place the read data at some place in the
string other than the beginning.  A negative OFFSET specifies
placement at that many **characters** counting backwards from the end of
the string.  A positive OFFSET greater than the length of SCALAR
results in the string being padded to the required size with C<"\0">
bytes before the result of the read is appended.

Tony

---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=134238

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About