SmokeyB has asked for the wisdom of the Perl Monks concerning the following question:

Is there such as thing as being hash happy? By that I mean can you have too many hashes of hashes of hashes.. and so on, or is there a better way to do it? For example, here's how I have criteria setup for my program:
my %MasterType = ( 'TEXT' => { 'function' => \&run_default, 'command' => 'SIIA', 'linkedHeaders' => { 'r#' => 'INO', 'rf' => 'IS1', 'rt' => 'ITE', }, }, );
and from that I call the following function accordingly:
&{%MasterType->{'TEXT'}->{'function'}}( %MasterType->{'TEXT'}->{'command'}, %MasterType->{'TEXT'}->{'linkedHeaders'} );
I'm just curious to know if I'm on the right track of things.

Replies are listed 'Best First'.
Re: Too Hash Happy?
by bart (Canon) on May 01, 2003 at 19:38 UTC
    Sure you can do it that way, but you really ought to be using $MasterType{'TEXT'} instead of %MasterType->{'TEXT'}.

    You appear to be using the same hash entry, containing a record, over and over again. It might not be a bad idea to use an alias to that hash entry, or, since it's a reference, make a copy. A copy of a reference points to the same thing as the original reference, so you still have read/write access to its contents.

    So: using an alias, you can do:

    for my $r ($masterType{'TEXT'}) { $r->{'function'}->($r->{'command'}, $r->{'linkedHeaders'}); }
    or, using a copy:
    my $r = $masterType{'TEXT'}; $r->{'function'}->($r->{'command'}, $r->{'linkedHeaders'});
    Note that I use the $subref->(PARAMS) syntax to do the sub call, instead of &{$subref}(PARAMS). It does the same thing, but I just like it better. It's less cluttered.

    p.s. For cases like this, I see nothing wrong with extremely short names for the variables. $r stands for "current record", for me, anyway.

Re: Too Hash Happy?
by diotalevi (Canon) on May 01, 2003 at 19:31 UTC

    No, that's perfectly fine. I'd clean up the syntax and only dereference TEXT once. Also, you're misusing the '%' sigil. It changes depending on what you get back out - $ for scalars, @ for slices.

    my $type = $MasterType{TEXT}; $type->{function}( $type->{command}, $type->{linkedHeaders} );

    Added: Mr. Muskrat and bart noticed that I used $type and $text but really I should have said only $type. So I fixed it.

      Thanks alot! That really clears things up for me! And it's good to know I'm on the right track.

      Cheers!
Re: Too Hash Happy?
by gjb (Vicar) on May 01, 2003 at 19:29 UTC

    You might consider introducing classes such as MaterType and (since I guess there's more than only text) TextType. The way you structure your data suggests you're thinking of it as objects, so why not go all the way?

    Just my 2 cents, -gjb-

Re: Too Hash Happy?
by hardburn (Abbot) on May 02, 2003 at 14:19 UTC

    Since Perl's data structures are just references to references to references to . . . you can have as many levels as your brain can keep track of.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated