Well, you might not do much better than that easily actually. The Dispatch table design pattern can help (you associate functions with some keys) which gives a basic example like that:

use strict; use warnings; use feature 'say'; my %operators = ( '>' => sub { $_[0] > $_[1] }, '==' => sub { $_[0] == $_[1] }); sub test { my ($op, $left, $right) = @_; die "Unknown operator: $op" unless exists $operators{$op}; say "$left $op $right" and return if $operators{$op}($left, $right); say "$right $op $left" if $operators{$op}($right, $left); } test "==", 1, 1; test ">", 2, 3; test ">", 4, 5; test "<", 6, 7; __DATA__ 1 == 1 3 > 2 5 > 4 Unknown operator: < at pm_1208722.pl line 12.

But if you want your own sentence to be written you'll need a more complex structure where for each key you have either several functions (one to compare, one to print) or a function and a template or something. I don't have the time to go any further than that at the moment though ^^".


In reply to Re: Passing logical operators on as content of scalar by Eily
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.