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 4230862
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T01:54:33+00:00 2026-05-21T01:54:33+00:00

What is the best way to test an if condition in perl that parameter

  • 0

What is the best way to test an if condition in perl that parameter driven?

For eg: if condition 1 is if($str1 eq “abc”) and condition 2 is if($str2 eq “xyz”), and at runtime the decision is made as to which condition needs to be checked, how would you do it?

$str1 = $ARGV[0];
$str2 = $ARGV[1];
$cond_value = $ARGV[2];

$cond1 = "$str1 eq \"abc\"";

$cond2 = "$str2 eq \"xyz\"";

if($cond_value == 1) {
  $return_val = eval($cond1);
}
else {
  $return_val = eval($cond2);
}

Doesn’t seem to work?

  • 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-21T01:54:34+00:00Added an answer on May 21, 2026 at 1:54 am

    Your example may be over-distilled. It worked when I tried it, although it’s probably not doing what you intended (and it will fail with strict/warnings, as Justin says). The values of $str1 and $str2 are expanded when $cond1 and $cond2 are assigned, not inside if ($cond_value). That can be fixed by escaping the dollar signs ($cond1 = "\$str1 eq \"abc\"";).

    eval has a bunch of issues, though, including the trouble of figuring out the right escapes to use and possible security risks if you’re working with untrusted data. You may want to consider using anonymous subroutines:

    $cond1 = sub { $_[0] eq "abc" };
    $cond2 = sub { $_[1] eq "xyz" };
    
    $test = $cond_value ? $cond1 : $cond2;
    
    $return_val = $test->($str1, $str2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the best practices way to test that a model is valid in rails?
I wonder what is the best way to test a method that only checks
What is the best way to test web service using NUnit. I want it
What is the best way to test an implementation of a mutex is indeed
Just wondering whats the best way to test Python CGI while developing a site?
Whats the best/easiest way to test for administrative rights in a PowerShell script? I
What is the best way to unit test heavily database dependant .NET middleware? E.g.
What is the best way to integration test EJBs and REST-Resources?
What is the best way to do a boolean test in C# to determine
What is the best way to deploy a heavily customized SharePoint solution from test

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.