Before I get too set in my (bad?) ways with writing objects, would some of you eyeball this and let me know where I veered (very?) off course. Basically a sanity check please?
package Twitter::Objects;
use strict;
use warnings FATAL => qw( all );
use lib '..';
use Base::Data qw(data_file get_hash);
use Base::Convert qw(hashtagify);
use Twitter qw(target_new_subscribers);
use Twitter::ListSort qw(list_sortflags list_compare);
# The 'data_file' subroutine takes 2 parameters (directory,filename).
# The directory is the location within the main data directory.
my %headings = (
account_totals => [qw(screen_name followers friends updates)],
people => [qw(id screen_name name time_zone location last_status f
+riends followers greet)],
lists => [qw(id slug name user members_count members_change subsc
+ribers_count subscribers_change status)],
mentions => [qw(id screen_name date text reply_screen_name reply_id)
+],
retweets => [qw(id tweet retweeters)]
);
# 'accounts' is to return an array(ref) of all of my Twitter accounts.
sub accounts {
my ($class) = @_;
my @accounts = qw(
Lady_Aleena
LadyAleena_ABC
LadyAleena_CBS
LadyAleena_FOX
LadyAleena_NBC
LadyAleena_SyFy
LadyAleena_TNT
LadyAleena_USA
LadyAleena_TV
LadyAleena_eros
LadyAleena_home
LadyAleena_test
);
return bless \@accounts;
}
# 'account_totals' is to return a hash(ref) totals such as
# followers, friends, & updates each account has.
sub account_totals {
my ($class, $account) = @_;
my %accounts = get_hash(
file => data_file('Twitter','account_totals.txt'),
headings => $headings{account_totals},
);
return bless $accounts{$account};
}
# 'acct_data' returns other collected data for my Twitter
# accounts. See the headings for people, mentions, and
# retweets for the data to be returned as a hash(ref).
sub acct_data {
my ($class, $account, $type) = @_;
die "Invalid type, $type does not exist." if !$headings{$type};
my %data = get_hash(
file => data_file("Twitter/users/$account","$type.txt"),
headings => $headings{$type},
);
return bless \%data;
}
my $base_link = "https://twitter.com/#!";
my %add_lists_data = (
'Advtr of Brisco County Jr' => {
long_name => 'The Adventures of Brisco County, Jr.',
abbr => 'Bricso County'
},
'Buck Rogers 25th Century' => {
long_name => 'Buck Rogers in the 25th Century',
abbr => 'Buck Rogers'
},
'Crossing Jordan/Las Vegas' => {
long_name => 'Crossing Jordan & Las Vegas',
abbr => 'CJLV'
},
'ER Third Watch Med Invgtn' => {
long_name => 'ER, Third Watch, & Medical Investigation',
abbr => 'ETM'
},
'Eureka Warehouse13 Alphas' => {
long_name => 'Eureka, Warehouse 13, & Alphas',
abbr => 'EWA'
}
);
# 'lists_data' returns a hash(ref) of all the basic data
# collected about my lists (owned, subscribed to, and a member of
# on Twitter. 'lists_data' also collates other data about the lists
# I own such as how many more subscribers I want for each list. Also
# it puts the list members and subscribers into arrays. Some lists
# have a few special options, so the %add_lists_data is included into
# the hash(ref) returned for those lists.
sub lists_data {
my ($class, $account) = @_;
my %lists = get_hash(
file => data_file("Twitter/users/$account","lists.txt"),
headings => $headings{lists},
);
for my $list (values %lists) {
list_sortflags($list);
my $user = $list->{user};
my $slug = $list->{slug};
my $name = $list->{name};
my $mems = $list->{members_count};
my $subs = $list->{subscribers_count};
my $abbr = $add_lists_data{$name}{abbr} ? $add_lists_data{$name}{a
+bbr} : '';
$list->{abbr} = $abbr;
$list->{hashtag} = length($abbr) ? hashtagify($abbr) : hashtagif
+y($name);
$list->{link} = "$base_link/$user/$slug";
$list->{long_name} = $add_lists_data{$name}{long_name} ? $add_list
+s_data{$name}{long_name} : $name;
$list->{percent} = int(($subs / $mems) * 100) if $mems > 0;
$list->{target} = target_new_subscribers($list->{members_count}
+,$list->{subscribers_count});
my @statuses = split(/,/,$list->{status});
$list->{status} = [@statuses];
next if !grep(/owner/,@{$list->{status}});
for my $list_people (qw(members subscribers)) {
my $file = data_file("Twitter/users/$account/lists/$slug","$list
+_people.txt");
next unless -s $file;
open(my $lp_fh,'<',$file);
my @lp = <$lp_fh>;
chomp(@lp);
$list->{$list_people} = [@lp];
}
}
return bless \%lists;
}
# 'all_lists' takes all of the lists from all of my accounts
# and puts them into one big hash(ref).
sub all_lists {
my @accounts = Twitter::twitter_accounts;
my %lists;
for my $account (@accounts) {
my $sublists = Twitter::Objects->lists_data($account);
for (keys %{$sublists}) {
$lists{$_} = $$sublists{$_} unless $lists{$_};
}
}
return bless \%lists;
}
# 'lists_people' is a hash(ref) which is all of the members
# and subscribers of all my lists so I know who is a member or
# subscriber to more than one list.
sub lists_people {
my $lists = Twitter::Objects->all_lists;
my %people;
for my $list (sort { list_compare($a,$b) } values %{$lists}) {
my $name = $list->{name};
next if !grep(/owner/,@{$list->{status}});
for my $type (qw(subscribers members)) {
if ($list->{$type}) {
my @list_people = @{$list->{$type}};
for my $person (@list_people) {
push @{$people{$type}{$person}}, $name;
}
}
}
}
return bless \%people;
}
1;
Update: per mbethke's suggestion, I added comments to let you know what each object is supposed to do. I hope they are clear enough.