in reply to 5.42: Does m// toss a string around?

It's pack 'P' that's making a copy of the string buffer.


While matching creates a copy of the string matched, matching uses the COW mechanism to avoid making a copy of the string buffer when possible.[1] The COW mechanism *is* being used here, so no copy is being made by matching.

However, pack 'P' forces the scalar to have a string buffer and for it to be modifiable.[2] Since you are starting with a buffer that's shared with the literal constant and the scalar associated with $& and other regex vars, this means pack 'P' forces a copy to be made.

use Config qw( %Config ); use Devel::Peek qw( Dump ); my $ptr_size = $Config{ ptrsize }; my $ptr_format = $ptr_size == 4 ? "L" : $ptr_size == 8 ? "Q" : die( "Unsupported pointer size $ptr_size\n" ); # https://perldoc.perl.org/perlapi#SvPV_force sub SvPV_force { unpack $ptr_format, pack "P", $_[0] } my $s = "abc"; Dump $s; printf "%x\n", SvPV_force( $s ); Dump $s;
SV = PV(0x57565c72fee0) at 0x57565c75e0d0 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x57565c761a90 "abc"\0 CUR = 3 LEN = 16 COW_REFCNT = 1 00.00.57.56.5c.78.5b.c0 SV = PV(0x57565c72fee0) at 0x57565c75e0d0 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x57565c785bc0 "abc"\0 CUR = 3 LEN = 16

Notice how Dump showed a shared buffer (IsCOW) before pack 'P', but one that isn't shared afterwards.


You can use the following to get the address of the string buffer without forcing it to exist and to be modifiable:

use B qw( svref_2object ); # https://perldoc.perl.org/perlapi#SvPVX sub SvPVX { svref_2object( \$_[0] )->PV } my $s = "0123456789"; printf "%x\n", SvPVX( $s ) while $s =~ /\G./g;

  1. Matching will make a copy of the string buffer if it can't use COW. Example.

  2. From perl5420delta,

    pack("p", ...) and pack("P", ...) now SvPV_force() the supplied SV unless it is read only. This will remove CoW from the SV and prevents code that writes through the generated pointer from modifying the value of other SVs that happen the share the same CoWed string buffer.

    Note: this does not make pack("p",... ) safe, if the SV is magical then any writes to the buffer will likely be discarded on the next read. [GH #22380]


Update: Renamed SvPV to SvPVX for accuracy.

Replies are listed 'Best First'.
Re^2: 5.42: Does m// toss a string around?
by Anonymous Monk on Jan 22, 2026 at 10:50 UTC

    Ah, thanks. If I understand correctly, it was 5.42 which fixed the long-standing issue with "pack 'P',".

    (I think the PV method constructs new string rather than returns numeric value. I'll use "pack 'P'," for the next example, for brevity of output over Devel::Peek::Dump; there's no 5.42 below)

    Wild goose chase, then, being sidetracked with imagined issue with 5.42, but initially I investigated s///, not m//. I now suspect similar reasons, related to COW, can you explain please? The 5.18 re-uses string buffer when new length is either the same or less than original. The 5.20 and then 5.26 loose both abilities.

    print "$^V\n"; my $x = '0123456789'; { my $s = "$x$x"; $s =~ s/(.)$/a/; printf "%x\n", unpack "Q", pack "P", $s; $s =~ s/(.)$/b/; printf "%x\n", unpack "Q", pack "P", $s; } { my $s = "$x$x"; $s =~ s/.$//; printf "%x\n", unpack "Q", pack "P", $s; $s =~ s/.$//; printf "%x\n", unpack "Q", pack "P", $s; } __END__ v5.18.4 29cdda8 29cdda8 29ce1e8 29ce1e8 v5.20.3 2b774f8 2b774f8 2b76b78 2b76f38 v5.26.3 2c9e118 2c9d7b8 2c9da88 2b4ae88

      I think the PV method constructs new string rather than returns numeric value

      No.

      SvPVX creates an object that provide information about a scalar ($_[0] aka $s), then uses that object's PV method to obtain the address of the string buffer of $s.

      I now suspect similar reasons, related to COW, can you explain please?

      COW was introduced in 5.20. Before 5.20, hacks which malfunctioned and/or a more expensive alternative had to be used.

        (OK, the PV method doesn't matter) I thought I got it (s/// implies matching; and $& and friends COW-share, in a way, original buffer; hence replacement operator (after 5.18) always re-allocates) but now I think the following example shows "match and manually replace" doesn't force re-allocation, then why s/// does?

        use Devel::Peek qw( Dump ); $Devel::Peek::pv_limit = $Devel::Peek::pv_limit = 10; $_ = "x" x 99; $_ .= "x"; # Force unsharing. Dump( $_ ); /(.+)/; Dump( $_ ); $_ = 'aaa'; Dump( $_ ); print "\n\n"; $_ = "x" x 99; $_ .= "x"; # Force unsharing. Dump( $_ ); s/(.+)/aaa/; Dump( $_ ); __END__ 5.042000 SV = PV(0x1e2951e23e0) at 0x1e29521d298 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x1e295228ad0 "xxxxxxxxxx"...\0 CUR = 100 LEN = 101 SV = PV(0x1e2951e23e0) at 0x1e29521d298 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x1e295228ad0 "xxxxxxxxxx"...\0 CUR = 100 LEN = 101 SV = PV(0x1e2951e23e0) at 0x1e29521d298 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x1e295228ad0 "aaa"\0 CUR = 3 LEN = 101 SV = PV(0x1e2951e23e0) at 0x1e29521d298 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x1e295228ad0 "xxxxxxxxxx"...\0 CUR = 100 LEN = 101 SV = PV(0x1e2951e23e0) at 0x1e29521d298 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x1e295215d60 "aaa"\0 CUR = 3 LEN = 16