Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Getting the PV pointer from a SV

by ikegami (Patriarch)
on Mar 09, 2016 at 13:33 UTC ( [id://1157160]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1157160]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found