Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: what does rand[] return?

by secret (Beadle)
on Nov 30, 2005 at 11:29 UTC ( #512904=note: print w/replies, xml ) Need Help??


in reply to what does rand[] return?

Well if you do ;

print 0 + [10] ;
you will see that perl treats the reference to the anonymous array [10] as a number . So what you do is like :
rand number

In my case this number is 27783332 and the array reference is 0x1a7f0a4 ( that you can see by doing print [10]) which is 27783332 in hex ...

If i repeat execution i get the same reference over and over again for this anon array. I don't know why but that's another problem ;)

Replies are listed 'Best First'.
Re^2: what does rand[] return?
by Happy-the-monk (Canon) on Nov 30, 2005 at 11:53 UTC

    If i repeat execution i get the same reference over and over again for this anon array. I don't know why

    I am as ignorant as you are, but if I was perl,
    I would make life easy and return the next possible free address that was big enough to store your anonymous array reference. If you freed that up - no longer use it - and asked for a new one, I would lazily give you just the one that I had before.

    Cheers, Sören

Re^2: what does rand[] return?
by Errto (Vicar) on Nov 30, 2005 at 16:27 UTC
    If i repeat execution i get the same reference over and over again for this anon array. I don't know why but that's another problem ;)

    I'm not an internals guy but I would guess this is an intentional optimization, though Happy-the-monk may also be correct. It would rely on perl noticing the following things:

    • the contents of this array are all constants
    • no other references to this array are taken (i.e. its refcount drops to zero at the end of the loop)
    • the array is not modified in any way
    Given all that, perl might allocate the array only once and intentionally reuse the same reference on each iteration.

    One way to find out might be to modify the loop so it creates a second array reference of the same size and then does something with it like store a reference somewhere, and see if that first array still gets the same address every time.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2020-02-19 02:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (80 votes). Check out past polls.

    Notices?