You have two basic problems here.

The first problem, the uninitialized errors, is that your new class method is not actually creating the object, or at least isn't keeping it. You're calling register, but you're discarding the registered object. I'd suggest:

# object constructor sub new { my $self = register( shift ); $given_name{ id $self } = shift; $surname { id $self } = shift; $self }
Note that we're also returning $self. It's important.

The second problem is that your calls to the readonly function, and it is a function!, happens after you're done trying to print $person->given_name - in other words, Class::InsideOut doesn't know anything about these readonly attributes yet! If you were to put your package into My/Person.pm so you could use it, that would solve your problem. Or, for test purposes, move it to the top of the file so it gets executed first.

With those two changes, and an extra space after Name is just so it looks nicer, I have:

#!/usr/bin/perl package My::Person; use strict; use warnings; use Class::InsideOut qw[public readonly private register id]; # declare the attributes readonly given_name => my %given_name; readonly surname => my %surname; # object constructor sub new { my $self = register( shift ); $given_name{ id $self } = shift; $surname { id $self } = shift; $self } package main; use strict; use warnings; my $person = My::Person->new( 'neil', 'watson' ); print 'Name is ', $person->given_name();
And this works fine :)

Good luck!


In reply to Re: Help with Class::InsideOut by Tanktalus
in thread Help with Class::InsideOut by neilwatson

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