http://qs321.pair.com?node_id=963939

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

I've have been going around in circles trying to get a grasp on objects, and when I finally go to give it a try, I can not get an object to work as I was hoping.

package Twitter::Objects; use strict; use warnings; use lib '..'; use Base::Data qw(data_file get_hash); my %accounts = get_hash( file => data_file('Twitter','account_totals.txt'), headings => [qw(screen_name followers friends updates)], ); sub new { my $self = \%accounts; bless($self); return $self; } 1;

Now, when I go to get data out of that object, I have to type my $foo = Twitter::Objects->new; print $foo->{Lady_Aleena}{updates};. I thought a way to shorten that would be the following.

package Twitter::Objects; use strict; use warnings; use lib '..'; use Base::Data qw(data_file get_hash); my %accounts = get_hash( file => data_file('Twitter','account_totals.txt'), headings => [qw(screen_name followers friends updates)], ); sub new { my ($account) = @_; my $self = \%{$accounts{$account}}; bless($self); return $self; } 1;

However, when using the above like so ... my $foo = Twitter::Objects->new('Lady_Aleena'; print $foo->{updates};, I get back an empty string. So, why does the subhash not work but the main hash does?

get_hash

Just in case you want to know.

sub get_hash { my %opt = @_; open(my $fh, '<', $opt{file}) or die("can't open $opt{file} $!"); my $line_number = 0; my %hash; while (my $line = <$fh>) { ++$line_number; chomp $line; my @values = split(/\|/,$line); my $n = 0; $hash{$values[0]}{sort_number} = $line_number if $opt{sort}; for my $heading (@{$opt{headings}}) { $hash{$values[0]}{$heading} = defined($values[$n]) ? $values[$n] + : ''; ++$n; } } return %hash; }
Have a cookie and a very nice day!
Lady Aleena