Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Passing logical operators on as content of scalar

by Eily (Prior)
on Feb 08, 2018 at 17:14 UTC ( #1208724=note: print w/replies, xml ) Need Help??


in reply to Passing logical operators on as content of scalar

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 ^^".

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208724]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-07-16 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (333 votes). Check out past polls.

    Notices?