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

hello. i am brand new to Perl and trying to learn from the ground up. when it comes to OO - i am stuck in the java mindset. what i would like to do is create a House Object like such
package House; sub new { my $class = shift; my $self = { id => $_[0], name => $_[1], detail => $_[2], }; bless $self, $class; return $self; }
then i would like to create a hash of new
use House; . . . %houserooms = ( kitchen => { House->new("1","kitchen","sleek and modern") } den => { House->new("2","den","board games and more") } ); . . . etc
then when a user types a name of the houseroom it can then print the details so something like
my $roomcmd = <>; #this def does not print and I am not sure why or how to make it print + the details print $houserooms{$roomcmd}->{detail};
i am trying to approach this from an OO standpoint. And my thought in hashing objects was that new rooms could be added quickly and this could be built upon by indicating which rooms connect to other rooms. thank you!

Replies are listed 'Best First'.
Re: Use Objects as Hash Values?
by NetWallah (Canon) on Apr 15, 2013 at 05:01 UTC
    Try replacing the STDIN read with:
    chomp (my $roomcmd = <>) ;
    Without that, $roomcmd will have "\n" as the last character.

    I would re-design your House Class as:

    package Room; sub new { my ($class,$id,$type,$desc) = @_; return bless {id=>$id, name=>$type, detail=>$descr}, $class; } #-------------------------- package House; my $roomid = 1; sub new{ my $class = shift; my $self = { rooms => {}, }; bless $self, $class; return $self; } sub addroom { my ($self, @room_params) = @_; die "No room type specified" unless $room_params[1]; die "I already have a $room_params[1]. No code to add another." if + $self->{rooms}{$room_params[1]} ; $roomid++; $room_params[0] ||= $roomid; # Auto-incremented $self->{rooms}{$room_params[1]} = Room::->new (@room_params); return $self; # ALlows "addroom" calls to be chained... } sub findroom{ my ($self, $room_type) = @_; return $self->{rooms}{$room_type}; }

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

Re: Use Objects as Hash Values?
by Athanasius (Archbishop) on Apr 15, 2013 at 08:37 UTC

    Hello cmikel, and welcome to the Monastery!

    As NetWallah says, you need to chomp the keyboard input string before using it as a hash key. NetWallah’s proposed OO design, using a House class as a container for Room objects, is a good approach. However, your original design can also be made to work with a few tweaks:

    #! perl use strict; use warnings; package Room { sub new { my $class = shift; my $self = { id => shift, name => shift, detail => shift, }; return bless $self, $class; } } my %houserooms = ( kitchen => Room->new(1, 'kitchen', 'sleek and modern'), den => Room->new(2, 'den', 'board games and more'), ); print "\nEnter room (or \"quit\"):\n"; chomp(my $roomcmd = <>); while ($roomcmd ne 'quit') { print $houserooms{$roomcmd}->{detail} // "$roomcmd not found"; print "\n\nEnter room (or \"quit\"):\n"; chomp($roomcmd = <>); }

    Typical output:

    18:22 >perl 607_SoPW.pl Enter room (or "quit"): kitchen sleek and modern Enter room (or "quit"): den board games and more Enter room (or "quit"): bedroom bedroom not found Enter room (or "quit"): quit 18:23 >

    Note that an object is a blessed reference, so there is no need to use syntax like this:

    %houserooms = ( kitchen => { House->new("1","kitchen","sleek and modern") }, ... );

    which takes a House object (already a reference!), puts it into another (anonymous) hash, and stores a reference to that hash as the value corresponding to the “kitchen” key in the hash %houserooms.

    See perlreftut, perlref, perlootut, and perlobj.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thank you both very much! Both your answers helped! For now I think I might stick with my original design - but it was helpful to see how to build an additional class as the container for my other objects.