One neat thing you can play with is a hash-based dispatch table. The idea is you store code references as the hash values, and look up the code by key, as necessary.

Here's a simplistic example that hopefully gives you some idea of the power behind these constructs. Say you have a string of text, and you want to read it word-by-word, reacting to certain words in different ways. A traditional way to do that would be:

my $text = "I am going to walk to the park."; for ($text =~ /(\w+)/g) { if (lc $_ eq "walk") { print "Walking is good for you\n"; } elsif (lc $_ eq "park") { print "Parks are fun\n"; } }

That's good and well, but not very flexible, or scalable. Each new entry would require a new branch in the if/elsif chain, which can really start to add up. A hash-based approach might look something like this:

my %react_to = ( walk => sub { print "Walking is good for you\n" }, park => sub { print "Parks are fun\n" }, ); my $text = "I am going to walk to the park."; for ($text =~ /(\w+)/g) { if (my $reaction = $react_to{lc $_}) { $reaction->(); } }

This one may seem more complicated, but it's moving the logic out of a nested chain of if/elsif statements and into a data structure. All sorts of neat things can then be done with that data structure, including modifying it at runtime. Here's a dispatch table that modifies itself at runtime:

my %react_to = ( a => sub { shift->{b} = sub { print "A before B\n" } }, b => sub { shift->{a} = sub { print "B before A\n" } }, ); my $text = "b is the first word, but a is not the last."; for ($text =~ /(\w+)/g) { print "Read word: $_\n"; if (my $reaction = $react_to{$_}) { $reaction->(\%react_to); } }

This may be a contrived example, and it relies on the calling code passing the dispatch table in as an argument, but the idea is there.


In reply to Re: Mashing with Hashing by revdiablo
in thread Mashing with Hashing by hacker

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.