Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Hand referance

by Fingo (Monk)
on Feb 24, 2001 at 08:23 UTC ( [id://60606]=perlquestion: print w/replies, xml ) Need Help??

Fingo has asked for the wisdom of the Perl Monks concerning the following question:

Let's say I know the exact location of a peice of data, so I know how a referance to it will look. Is it possible to actually create a referance by hand? I would do something like this:  $ref_foo = ARRAY(0x80d1f04); I have tried this with strings, and it did not work.
Thanks

Replies are listed 'Best First'.
Re: Hand referance
by sierrathedog04 (Hermit) on Feb 24, 2001 at 10:05 UTC
    Brian L. Matthews explains it here as follows:
    1. A reference is not a memory address. If you go to the memory address corresponding to the scalar value of a reference you will not find your data there.
    2. A reference contains type information. A mere memory address, on the other hand, contains no type information and thus can store any object.
    3. Larry Wall intentionally decided to use the term "reference" rather than "pointer" so that people would understand that Perl is not pointing to a memory address.

    As Larry points out, Perl is not appropriate for all applications. For example, Perl itself is not written in Perl, it is written in C.

    If you have an application which requires low-level manipulation of the contents of memory addresses then that application is not an appropriate candidate for coding in Perl.

    Furthermore, it says in the Learning Perl book by our own Merlyn et al that a reference is somewhat like a pointer, but it is safer. Presumably one reason that a reference is safer is that one cannot easily manipulate references to poke holes that can be exploited in an operating system.

      Thanks. I guss I will use somethin else to do this.
Re: Hand referance
by clintp (Curate) on Feb 24, 2001 at 08:49 UTC
    Let's say I know the exact location of a peice of data
    Let's say...you don't. :) I tell you what, you tell me how you know this, and I'll tell you how to do it. You won't like the answer.

    For starters, you can't take a string and get a viable reference from it. Well, almost. It involves XS programming and that's torture for most people.

    What'cha got in memory? How do you know it's there? Is this part of your process or someone else's? Is this a machine API (BIOS?)? Inquiring minds want to know.

Re (tilly) 1: Hand referance
by tilly (Archbishop) on Feb 25, 2001 at 03:40 UTC
    Perl is definitely the wrong language to play around with this kind of thing. OTOH if you want to proceed, there are two ways to go.

    The first is to install Inline and proceed from there. The second is to use unpack with "P" to get at what is in a particular spot in the memory of your current process. You should not be able to get at the memory of other processes this way.

    If you are on a *nix system and have root access you can play around with /dev/mem to find what memory looks like. Of course if you wanted a good seed for random numbers, you are better off either trusting Perl to do a good job, or using something like Math::Random.

Re: Hand referance
by myocom (Deacon) on Feb 24, 2001 at 08:55 UTC

    I doubt this is possible (besides which, how do you know where this data is in memory? Will it always be in that same place?), and even if it were, I'd hate to maintain any code that used this sort of technique.

    Perhaps if you told us what you were trying to accomplish we could come up with a better (and indeed, more Perlish) way to do it...

Re: Hand referance
by Fingo (Monk) on Feb 24, 2001 at 09:06 UTC
    Why I wan this? First of all curiosity. Second of all, playing with memory addresses is fun ;) Also a result of looking at the contents of a random address is the perfect seed. It would be very interesting to directly controll the mempry.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-19 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found