Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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[12345] +' 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.

update: 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.

In reply to Re: How to access the contents of a specific memory address? by bulk88
in thread How to access the contents of a specific memory address? by pat_mc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-24 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found