Hi venerable monks,

I am trying to learn the Perl 6 object model and succeeded to work my way through a number of Perl 6 object features, including inheritance, roles, submethods and some other slightly more advanced features, but I am repeatedly stumbling on a very basic feature: how to access private attributes within the class?

Consider this very basic class for 3-D dots:

class Dot { has $.x; has $.y; has $!z; method get { return ($!x, $!y, $!z); } }; my $a = Dot.new(x => 23, y => 42, z => 2); say $_ for $a.get;

The output of the above code is:

23 42 (Any)
My understanding is that using the "." twigil for declaring the attribute will create an accessor method making in effect these attributes public outside the class. And indeed, with this, I can easily access the x and y attributes from outside the class (code not shown here above, but something like: say $a.x;).

My problem is with the z attribute. Since it is declared with the ! twigil, it cannot be accessed from outside the class. Fair enough, that's precisely what I wanted to test. So that's why I wrote a get accessor method within the class. But it seems that the get function is also not able to read the z attribute, which stubbornly remains undefined (well Any), even if I try to print it from within the class.

I tried various syntax variations (using self, naming the invocant, etc.), but nothing seemed to work, the z attribute remains unaccessible.

Trying to use the z attribute for some calculation in a method declared within the class gives me the following slightly more informative warning:

Use of uninitialized value of type Any in numeric context ...

I guess I must be missing some very simple and basic point, and I went through all the documentation that I could find, but I can't figure out what is wrong with this test. Any idea? Thanks in advance for any clue.

I am using Rakudo * / Perl 6 dated September 2015, so a fairly recent release.


In reply to Private attributes in Perl 6 objects by Anonymous Monk

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.