Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-16 11:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found