Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Help with Class::InsideOut

by Tanktalus (Canon)
on Feb 19, 2016 at 18:41 UTC ( #1155662=note: print w/replies, xml ) Need Help??


in reply to Help with Class::InsideOut

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!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2019-05-26 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (153 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!