If all you do is use an object as a hash reference then there is not much point making it an object. However, looking at your code it seems there is scope for a useful object. It looks like get_hash is probably general purpose code so it may make sense to base a class around that: a class that wraps up a bunch of options that can be saved to and loaded from disk. Consider:
use strict;
use warnings;
package OptionsBase;
sub new {
my ($class, %options) = @_;
my $self = bless \%options, $class;
$self->{headingsLU} = {map {$_ => 1} @{$options{headings}}};
return $self;
}
sub load {
my ($self) = @_;
open my $fIn, '<', $self->{file} or die ("Can't open $self->{file}
+: $!");
while (defined (my $line = <$fIn>)) {
chomp $line;
my ($rowName, @values) = split (/\|/, $line);
$self->{options}{$rowName}{sort_number} = $. if $self->{sort};
for my $heading (@{$self->{headings}}) {
$self->{rows}{$rowName}{$heading} = shift @values || '';
}
}
}
sub save {
my ($self) = @_;
open my $fOut, '>', $self->{file} or die ("Can't create $self->{fi
+le}: $!");
for my $key (keys %{$self->{rows}}) {
my @values =
map {defined $_ ? $_ : ''}
@{$self->{rows}{$key}}{@{$self->{headings}}};
print $fOut join ('|', $key, @values), "\n";
}
}
sub setOptions {
my ($self, $key, %options) = @_;
my @badOptions = grep {!exists $self->{headingsLU}{$_}} keys %opti
+ons;
die "Bad options: @badOptions\n" if @badOptions;
@{$self->{rows}{$key}}{keys %options} = values %options;
}
sub getOptions {
my ($self, $key, @options) = @_;
my @badOptions = grep {!exists $self->{headingsLU}{$_}} @options;
die "Bad options: @badOptions\n" if @badOptions;
die "No such row: $key\n" if !exists $self->{rows}{$key};
return map {$_ => $self->{rows}{$key}{$_}} @options;
}
package TwitterOptions;
push @TwitterOptions::ISA, 'OptionsBase';
sub new {
my ($class, %options) = @_;
return $class->SUPER::new(
name => 'Twitter',
file => 'account_totals.txt',
headings => [qw(screen_name followers friends updates)],
%options
);
}
package main;
my $obj = TwitterOptions->new();
$obj->setOptions('joe', screen_name => 'Joe', followers => 'Freida');
$obj->save();
my $another = TwitterOptions->new();
$another->load();
my %options = $another->getOptions('joe', 'screen_name', 'followers');
print "$options{screen_name}'s followers are: $options{followers}\n";
Prints:
Joe's followers are: Freida
This sample uses object inheritance to provide some common methods that act on option objects using the base class OptionsBase. TwitterOptions inherits those methods (that's what the ISA stuff is about) so all the methods provided in OptionsBase can be used with TwitterOptions objects. The neat thing now is that you can make new options classes that all behave in the same way, but have different sets of headers and file names etc.
True laziness is hard work