Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.97k stars 560 forks source link

[doc] perlfunc pack snippet has different output #22425

Closed poti1 closed 3 months ago

poti1 commented 3 months ago

Where

perlfunc - pack

A () group is a sub-TEMPLATE enclosed in parentheses. A group may take a repeat count either as postfix, or for [unpack](https://perldoc.perl.org/perlfunc#unpack-TEMPLATE%2CEXPR), also via the / template character. Within each repetition of a group, positioning with @ starts over at 0. Therefore, the result of

pack("@1A((@2A)@3A)", qw[X Y Z])
is the string "\0X\0\0YZ".

Description

I ran the snippet, but did not notice any \0 NULL chars:

Not with a say:

perl -E 'say pack("@1A((@2A)@3A)", qw[X Y Z])'
XYZ

Nor with Devel::Peek::Dump:

perl -Me -e 'dd pack("@1A((@2A)@3A)", qw[X Y Z])'
SV = PV(0xb400007fb702df90) at 0xb400007fb701f678
  REFCNT = 1
  FLAGS = (POK,pPOK)
  PV = 0xb400007fb71140a0 "XYZ"\0
  CUR = 3
  LEN = 16

# Directly using Dump:
perl -MDevel::Peek=Dump -e 'Dump pack("@1A((@2A)@3A)", qw[X Y Z])'                                      SV = PV(0xb4000070ac02df80) at 0xb4000070ac09d540
  REFCNT = 1
  FLAGS = (PADTMP,POK,pPOK)                                    PV = 0xb4000070ac02c2e0 "XYZ"\0
  CUR = 3                                                      LEN = 16

Am I missing something? Or perhaps the functionality is changed?

tonycoz commented 3 months ago

This was broken by 3b10bc60979cf changing the quotes from ' to ", if I escape the @s:

$ ./perl -Ilib -MDevel::Peek -we 'Dump(pack("\@1A((\@2A)\@3A)", qw[X Y Z]))'
SV = PV(0x5639ac8e14d0) at 0x5639ac904090
  REFCNT = 1
  FLAGS = (PADTMP,POK,IsCOW,READONLY,PROTECT,pPOK)
  PV = 0x5639ac922130 "\x00X\x00\x00YZ"\0
  CUR = 6
  LEN = 16
  COW_REFCNT = 0