my $astring = "some string";
my $svptr = (\$astring)+0;
#works on 32 bit ONLY
my $packesvptr = pack('L', $svptr);
my $svbinary = unpack('P[L4]', $packesvptr);
my $packedPV = substr($svbinary, 4*3, 4);
print "\"".unpack('p', $packedPV)."\"";#null terminated, use 'P
+' for binary data
print "\n\nExample 2\n\n";
$packedPV = pack('p', $astring );
print "\"".unpack('p', $packedPV)."\"";
This is pretty much all of Perl's Perl Language pointer API. You can get a pointer to a scalar string (not a pointer to a scalar integers!!!, egh, actually you can, but if you need to do it, you know how to do it) and you can get a SV *and you can read an arbitrary memory location into a scalar string, null terminated or fixed length. pack wants pointers in packed binary "\x01\x02\x03\x04" format, not english/readable/perl scalar integers "1234567" format. Only major limitation is Perl language can't copy scalar strings to arbitrary memory address (but see this hack Pure Perl module(246 lines, Linux/Win32) that calls external libraries - no XS file.
, but its not pure Perl language (Dynaloader Module), so that doesn't meet the standard of pure Perl language, also there is syscall
, I dont use unix, so I dont know if there is a way to call a C memcpy with syscall). My code is 32 bit only. Changes would have to be made for it to work on 64 bits.
after reading Re^4: How to access the contents of a specific memory address?
, I'll add this
pointer dereferencing with basic Perl commands (rather than any specially designed modules)
You mentioned with the basic Perl language. malloc and memcpy and free are all available somewhere someway on CPAN as XS modules, and they will work correctly without any crashes except for cases where they will crash in C and in Perl together, always (access vio, corrupting malloc headers, etc). Some monks here question why on earth you need pointers in Perl? They are correct, you can't save a pointer to a disk, it will never be valid in another process. You can't send a pointer through IPC to another process, its not valid in the other process. You can't send a pointer through the network to another PC, it is not valid on that other PC. No perl basic language commands take integer pointers (except for syscall
maybe). To use your integer pointers you would need a FFI XS module, or a XS module that wants SVIVs with pointers in them as parameters (a possible design choice for a XS library to keep the XS code as small as possible, and put the parameter conversion logic into Perl, even though its quite slower then) for passing to a C function.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||