Util::H2O ~ Iterative Refinement of Existing Perl Code

Util::H2O is an incredibly powerful tool for managing HASH references in a more natural way.

This post is the first of several that will explore this awesome module. I've started using it quite a bit in both new code and in existing code. There are several imporant cases where it really shines. Here we explore the power it has to iteratively refine existing code. It's also fun and easy to introduce into existing code.

Util::H2O provides a method called h2o that provides a very powerful way for turning a hash reference to an object. Generally speaking, this means I get accessors with as few keystrokes as possible.

I've been using this module quite a bit recently, and I really do like the improvements it has over the more traditional modules used for generating accessors.

The most basic use of h2o is to provide accessors to a hash reference with a single level of keys. I tend to use hash references a lot.
use strict;
use warnings;
use Util::H2O qw/h2o/;

my $myhashref = {
  key1 => q/some value.../,
  key2 => q/another value/,
  #..
  keyN => [qw/thing1 thing2 thing3/],  
};

# accessing hash values
my $key1 = $myhashref->{key1};                   # get value
$hashref->{someOtherKey} = q/yet another value/; # assign value

# add accessors with a single use of h2o
h2o $myhashref;

# now one may use an accessor
my $key2 = $myhashref->key2;                     # get value
$myhashref->keyN(qw/a new set of things/);       # update value

#...
This provides an extremely easy way to clean up an old Perl script that makes heavy use of hash references. Naturally, the script will contain a lot of use of the dereferening syntax to access the values via the hash keys; in the example above recall this line:
# accessing hash values
my $key1 = $myhashref->{key1};
By making the call to objectify $myhashref in a single line, we're now able to do:
# add accessors with a single use of h2o
h2o $myhashref;
Now we get to remove the curley braces,
# accessing hash values
my $key1 = $myhashref->key1;
And the assignment goes from:
$hashref->{someOtherKey} = q/yet another value/;
To a form that lacks both curley braces and the assignment operator (=).
# assigning value (some call this a "mutator")
$hashref->someOtherKey(q/yet another value/);
Those improvements are both huge wins in my book! If the goal is to clean up an old script that makes heavy use of hash references, the above shows that h2o allows us to:
  • eliminate a pair of braces for each get
  • eliminate a pair of braces and and equal sign for each assignment

Conclusion

I highly recommend looking at Util::H2O if you're looking to refine some older Perl code, particularly if it makes heavy use of simple HASH references. We'll see in future posts that the module had effectively deal with hashes that contain other hash references; but the module stands on its own in its most basic form.

I am also still learning this module, but it seems to have the potential to turn the whole Perl Object Orient Programming (POOP) debate on its head. The most controversial thing I will posit at this time is that one would not be in error to look at h2o as a better bless.

Finally, if you've found this module as helpful in refining old hash reference heavy code; I think I and maybe other readers would enjoy reading about this in the comments below.

References

  1. Util::H2O on CPAN
  2. Util::H2O on GitHub

Leave a comment

About Oodler 577

user-pic Mayor of Falvortown