gctaylor1 has asked for the wisdom of the Perl Monks concerning the following question:
I cannot figure out how to assign values via STDIN to a hash reference object when one of the values is a list. It seems like I get further when using an anonymous array to hold the values, and if I hard code the values(the commented line below) it works. Most other attemps result in the error "Can't use string ("a b c") as an ARRAY ref while "strict refs" in use..., <STDIN> line 3." I've tried a number of things including splitting the incoming data which results in another error.
use strict; use warnings; package Music; sub new { my $class = shift; my ($artist, $title, @tracks) = @_; my $ref= {"Artist" => $artist, "Title" => $title, # "Tracks" => [qw(1 2 3)] "Tracks" => [@tracks] }; bless($ref, $class); return $ref; } sub set_music { my $self = shift; print "Enter name of album "; chomp($self->{Artist} = <STDIN>); print "Enter title of ", $self->{Artist},": "; chomp($self->{Title} = <STDIN>); print "Enter tracks: "; chomp($self->{"Tracks"} = <STDIN>); } sub show_music { my $self = shift; print $self->{'Tracks'}->[1],"\n"; print @{$self->{'Tracks'}},"\n"; print "\n"; } 1;
#!/usr/bin/perl use strict; use warnings; use Music; my $artist3 = Music->new; $artist3->set_music; $artist3->show_music;
I'm stumped and out of ideas to try. Being the newbie that I am I guess this has to be a common thing to do. But I
can't find an example that works for me.
Thank-you.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How do I assign values via STDIN to hash reference object list?
by JavaFan (Canon) on Nov 02, 2008 at 19:22 UTC | |
by gctaylor1 (Hermit) on Nov 02, 2008 at 19:30 UTC |