How does $self get assigned to the same exact memory location every time a new object is created?

In normal usage, you don't get the same exact memory location every time a new object is created. Quite on the contrary: Every object has its own value for $self which is returned by the new() method as a reference to a new (!) hash. You might get the same memory location if you drop an object and then create another one, if Perl's memory management decides that way.

In your code example, $self is a lexical variable which gets a location in memory once, when the module's code is evaluated. The value of $self gets overwritten every time a new object is created, so the return value of kool_func_for_users changes with every new object - but then, it does so for all objects on that class. In effect, you have created a "class attribute" which has the name $self but in your case should actually be called $most_recent_object. There are valid use cases for these, and there's also a Moose module MooseX::ClassAttribute.

Here's a short demo (in which I've also fixed your attribute syntax by using parens instead of braces, and by using is and isa as intended by Moose):

use strict; use warnings; use 5.010; # saving some vertical space package My::Demo; use Moose; has 'why_pass_self' => ( is => 'ro', isa => 'Str', default => 'dunno, is this ok?' ); my $self; sub BUILD { my $s = shift; $self = $s; } sub _kool_func { say $self->why_pass_self; } sub kool_func_for_users { my $self = shift; $self->_kool_func; } ##################################################################### package main; use Scalar::Util qw(refaddr); my $o1 = My::Demo->new; print "o1 ", refaddr($o1), " at says: "; $o1->kool_func_for_users; my $o2 = My::Demo->new(why_pass_self => "That's why!"); print "o2 ", refaddr($o2), " at says: "; $o2->kool_func_for_users; print "o1 ", refaddr($o1), " at says: "; $o1->kool_func_for_users;
PS: I just noticed I've provided what Corion suggested:
Maybe consider just creating two objects of the same such class and watch what happens.

In reply to Re^3: Why not use a local variable for $self in Moose? by haj
in thread Why not use a local variable for $self in Moose? by nysus

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



  • 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:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.