Your code as written is easy to understand at a glance. However, I think you are missing a case in the '<' situation, where $number1 is equal to $number2?

I liked the post by Eily++. If I went that way, I likely would go further by incorporating the result of $number1 <=> $number2 into the hash key. That way there are no "if" statements at all in the calling code. Maybe resulting in a table like this?:

my $dispatch = {"< -1" => "num1 less than num2", "< 0" => \&actionX, # blah...blah 6 or 9 entries to cover all cases, <, >, +== times 3 };
In your "==" case, the dispatch action for $num1<$num2 and $num1>$num2 would be the same.
Note: I would have to read the spec carefully on the spaceship operator. Instead of it returning -1,0,1 which is what it actually does, it might be spec'd as only negative number,0,positive number. I would allow whatever the spec says is allowed (convert to -1,0,1 so that hash lookup works no matter what exact spaceship implementation is).

However having said that, the code structure that you have is easy to understand and it will execute very quickly (faster than a dispatch table). It took me a bit more "brainpower" to understand the Eily code whereas I understood your code at a glance. I wouldn't be overly concerned about "wordiness". If the pattern shown in your code repeats often elsewhere in the code, then the "understanding overhead" associated with the dispatch table is perhaps worth it. Simple, albeit it wordy code is often a good way to go. It is also possible that I might implement the three cases <,>,== with the same if logic (no special handling of ==). That would make the code even more wordy, but more "regular". Coding is part art and part science - lot's of ways to do things.


In reply to Re: Passing logical operators on as content of scalar by Marshall
in thread Passing logical operators on as content of scalar by gm40

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.