Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Help with Class::InsideOut

by neilwatson (Priest)
on Feb 19, 2016 at 18:17 UTC ( [id://1155657]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings Avout,

Why am I getting this error:

#!/usr/bin/perl use strict; use warnings; my $person = My::Person->new( 'neil', 'watson' ); print 'Name is', $person->given_name(); 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 = shift; register( $self ); $given_name{ id $self } = shift; $surname { id $self } = shift; } 1; $ ./indsideout.pl Use of uninitialized value in hash element at ./indsideout.pl line 23. Use of uninitialized value in hash element at ./indsideout.pl line 24. Can't locate object method "given_name" via package "watson" (perhaps +you forgot to load "watson"?) at ./indsideout.pl line 7.

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Help with Class::InsideOut
by Tanktalus (Canon) on Feb 19, 2016 at 18:41 UTC

    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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1155657]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found