Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7588019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:49:12+00:00 2026-05-30T19:49:12+00:00

I have to check hashrefs like this one { foo => 65, bar =>

  • 0

I have to check hashrefs like this one

{ foo => 65, bar => 20, baz => 15 }

against an arrayref of hashrefs expressing conditions like this

[
 { foo => { "<=" => 75 } },
 { bar => { "==" => 20 } },
 { baz => { ">=" => 5 } },
]

and return a true value if all conditions are fulfilled.

Neither of the two data structures is pre-determined. One is built from parsing a string in a database, the other from parsing user input.

In the case above, I would return true, but if I checked the hashref against

[
 { foo => { "<=" => 60 } },
 { bar => { "==" => 20 } },
 { baz => { ">=" => 5 } },
]

I would return false, because foo in the first hashref is not <= 60.

The question is: what’s the best strategy for doing that?

I am thinking of

  • building a series of subrefs via eval
  • checking against the appropriate one among 5 different pre-built subrefs (one per case for >, <, <=, >= and ==)

Am I going down the wrong path altogether? and if not, what’s the best, eval or pre-built functions?

I have looked into Params::Validate but I am concerned that it’d be a lot of overhead, and I’d have to build the callbacks anyway.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:49:14+00:00Added an answer on May 30, 2026 at 7:49 pm

    Use code references instead, and you will have ready to go validators. I simplified your condition-structure. There is no need to have an extra array level in there, unless you have duplicate hash keys, which I assume you don’t.

    The simplistic sub { $_[0] <= 75 } will simply compare the first value of the arguments. By default, the last value evaluated in the subroutine will be its return value.

    use v5.10;
    use strict;
    use warnings;
    
    my $in = { foo => 65, bar => 21, baz => 15 };
    
    my $ref = {
        foo => sub { $_[0] <= 75 } ,
        bar => sub { $_[0] == 20 } ,
        baz => sub { $_[0] >= 5 } ,
    };
    
    for my $key (keys %$in) {
        if ($ref->{$key}($in->{$key})) {
            say "$key : Valid";
        } else {
            say "$key : Invalid";
        }
    }
    

    Output:

    bar : Invalid
    baz : Valid
    foo : Valid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

INSERT MODE i have check box in one tr. and another dropdown control in
I have check out this Wikipedia page on it, but I still don't understand
As I have check box in gridview if i dont select any one checkbox
In our application we have check box under one div, so i cant select
have check the documentation but there is no sample to show how this is
Have check boxes 1-300. This JS function alerts user when nothing is selected. Function
I have check this site for solutions but i didn't find something that can
Any one have idea about how to geotag the capture video? I have check
I am getting this exception while retriving data from address book. I have check
I have to check some code and run it. I have the URL: svn+ssh://myuser@www.myclient.com/home/svn/project/trunk

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.