Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Address of anonymous hash

by aixtal (Sexton)
on Feb 02, 2011 at 07:32 UTC ( [id://885674]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I just found out that anonymous hashes do not have a stable address throughout program execution :

use Scalar::Util qw(refaddr); for (1 .. 10) { print refaddr { foo => 1 }, "\n"; }
The above fragment of code prints out adresses that seem to run in a cycle of 3 :
4303377064
4303375648
4303377184
4303377064
4303375648
4303377184
4303377064
4303375648
4303377184
4303377064

I guess this is due to some compilation internals. But still, I found it puzzling. I was naively expecting an anonymous hash to be compiled once and for all at a given place.

I wonder if some monk has an insight on this ?

Replies are listed 'Best First'.
Re: Address of anonymous hash
by Corion (Patriarch) on Feb 02, 2011 at 07:47 UTC

    It's exactly the reverse. An anonymous hash needs to be created every time you assign it, because the only thing you can store is a reference to it. The difference shows in the two programs:

    #!perl -w use strict; use Data::Dumper; my @hashes; for (1..2) { push @hashes, { foo => 'bar' }; }; $hashes[0]->{baz} = 1; print Dumper $hashes[1]; # { foo => 'bar' }

    Your expectation would change this to the following:

    <c> #!perl -w use strict; use Data::Dumper; my @hashes; my $constant_hash = { foo => 'bar' }; for (1..2) { push @hashes, $constant_hash; }; $hashes[0]->{baz} = 1; print Dumper $hashes[1]; # { foo => 'bar', baz => 1 }
Re: Address of anonymous hash
by davido (Cardinal) on Feb 02, 2011 at 08:11 UTC

    Your example creates a new anonymous hash that falls out of scope with each iteration of the loop. So in the simplest terms, it's not the same anonymous hash each time. The fact that a newly created anonymous hash has or doesn't have the same address as a previous one that has fallen out of scope is practically coincidental. More importantly, it shouldn't matter what is happening behind the curtain.


    Dave

      Understood! Many thanks to Corion and you, these explanations are crystal clear !
Re: Address of anonymous hash
by ikegami (Patriarch) on Feb 02, 2011 at 17:35 UTC
    You can think of «{ ... }» as «Hash->new(...)». (Of course, what it returns isn't blessed.)
Re: Address of anonymous hash
by sundialsvc4 (Abbot) on Feb 02, 2011 at 15:30 UTC

    You should never use a memory-address as a “key.”   It is never wise to build code that is in any way dependent upon “an implementation side-effect,” even if that side-effect appears to be stable.   You will get burned ...

      You should never use a memory-address as a “key.”

      Have you heard of inside-out objects? At the base, they do exactly what just proclaimed shouldn't be done.

      { my %objs; sub new { my $obj = { ... }; my $key = 0+$obj; $data{$key} = $rec; return $key; } }

      (Blessing omitted for clarity.)

      I don't know to what side-effect you are referring. That numification of a reference returns its address? That's safe.

      You should never use a memory-address as a “key.”

      Have you heard of inside-out objects? At the base, they do exactly what just proclaimed shouldn't be done.

      { my %objs; sub new { my $obj = { ... }; my $key = 0+$obj; $data{$key} = $rec; return $key; } }

      (Blessing omitted for clarity.)

      I don't know to what side-effect you are referring. That numification of a reference returns its address? That's safe.

      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-03-19 04:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found