in reply to Getting the PV pointer from a SV

This version also support 64-bit versions of Perl.

$ perl -E' use Config qw( %Config ); my $ptr_size = $Config{ptrsize}; my $ptr_format = $ptr_size == 4 ? "L" : $ptr_size == 8 ? "Q" : die("Unrecognized pointer size"); my $s = "abc"; my $ptr = unpack($ptr_format, pack("p", $s)); say sprintf "0x%x", $ptr; use Devel::Peek; Dump($s); ' 0x1766c40 SV = PV(0x174dcf0) at 0x176d958 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x1766c40 "abc"\0 CUR = 3 LEN = 16

You should call utf8::upgrade or utf8::downgrade first to control the storage format of the string.

Replies are listed 'Best First'.
Re^2: Getting the PV pointer from a SV
by rodd (Scribe) on Mar 09, 2016 at 15:02 UTC

    Awesome, thanks for the 32/64-bit portability code.

    Pack/unpack "p" and "P" is such a mysterious template code to me... I wonder if Devel::PeekPoke::peek( ptr, len ) could be written also with pack/unpack... I wouldn't know where to put the length though, maybe in the template somewhere appended to the letter 'p'.

      Not only can it be done, that's exactly what Devel::PeekPoke::PP does.

      $ perl -E' use Config qw( %Config ); my $ptr_size = $Config{ptrsize}; my $ptr_format = $ptr_size == 4 ? "L" : $ptr_size == 8 ? "Q" : die("Unrecognized pointer size"); sub pv { unpack($ptr_format, pack("p", $_[0])) } sub peek { unpack('P'.$_[1], pack($ptr_format, $_[0])) } my $s = "abcdef"; my $ptr = pv($s); say peek($ptr+1, 4); ' bcde