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

In reply to Re: Why won't a hash in a hash work as a hash reference to create an object? by GrandFather
in thread Why won't a hash in a hash work as a hash reference to create an object? by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.