rodd has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
I'm looking for nicer way, in pure Perl and standard library preferably, to get the address location of the actual data held by an SV.
I've implemented this ugly, regex-based hack below and it works, $ptr holds the actual address... but is there a better way?.
use Data::Peek; my $buf = "hello world"; my $dd = DDump( $buf ); # similar to Devel::Peek::Dump, but stringified my ($ptr) = $dd =~ /PV = (\S+)/; $ptr = hex $ptr; # $ptr had a string like "0x78ad928efff" use Devel::PeekPoke; print peek( $ptr, 4 ); # prints "hell", so it workedBasically what I did was to capture the "PV = (...)" value out of the string with a regex. Here are the contents of the Devel::Peek::Dump():
SV = PV(0x7fea785135c0) at 0x7fea785264f0 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x7fea70219200 "hello world"\0 CUR = 11 LEN = 16
In case you are curious, the reason for getting the address is due to the need to call an external (to perl) system that wants mem address and length of the byte sequence, not to use peek()/poke() or any other Perl code hack.
Maybe the solution is to bless $buf into B::SV, then call one of its methods? Like I said, I want to avoid XS, C or hopefully any outside dependencies.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getting the PV pointer from a SV (pack 'p')
by BrowserUk (Patriarch) on Mar 09, 2016 at 12:10 UTC | |
|
Re: Getting the PV pointer from a SV
by ikegami (Patriarch) on Mar 09, 2016 at 13:33 UTC | |
by rodd (Scribe) on Mar 09, 2016 at 15:02 UTC | |
by ikegami (Patriarch) on Mar 09, 2016 at 17:28 UTC |