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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:42:10+00:00 2026-06-08T08:42:10+00:00

Basically I want to get user input and decide what to do, and I

  • 0

Basically I want to get user input and decide what to do, and I need to have the same outcome even for different user inputs sometimes. Example code:

given ($command) {
      when ('a' || 'add') {add}
      when ('s' || 'subtract') {subtract}
      when ('m' || 'multiply') {multiply}
      when ('d' || 'divide') {divide}
      default { print "try again, usage: add, subtract, multiply, divide (a, s, m, d)\n" }

Now the trouble with this is that it only works if I type the single letter commands at the beginning of the or statements. If I type something like “add” into the prompt, I get the default message telling me to try again.

I could make separate cases for these, but it would just have the same body as the single letters, which is just redundant.

  • 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-06-08T08:42:11+00:00Added an answer on June 8, 2026 at 8:42 am

    The given/when construct uses the smart match operator, which doesn’t have any special-case behavior for the || operator. The expression

    'a' || 'add'
    

    evaluates to 'a', so it just matches $command against the string 'a'.

    If you want to match $command against any of a list of values, you can use an array value:

    given ($command) {
        when ([qw(a add)]) { add }
    ...
    

    (As usual, there are several ways to express this.)

    References:

    perldoc -f given

    perldoc perlsyn; search for “Switch statements” and “Smart matching in detail”.

    EDIT :

    Another approach for this problem, if you want to allow $command to be any prefix of the command name (e.g., "s", "su", "sub", …, "subtract") is something like this:

    sub match {
        my($s, $target) = @_;
        return index($target, $s) == 0;
    }
    
    ...
    
    if (match($command, 'add') { ... }
    elsif (match ($command, 'subtract') { ... }
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I want get data I already have accessed from javascript and passing it
I have an SPListItemCollection. I basically want to get one of the items (randomly)
I want to get all the Applications that have intentlisteners to Intent.CATEGORY_HOME so basically
So, basically I have an NSArray . I want to get an array with
I am basically trying to get a user to input a hexadecimal input via
I have an HTML input field that I want the user to be able
summary: Basically, I want the user to input the title of a parter to
I want to get the current GPS Location from the user's device, and basically
Basically I want to get the value of a textbox which is to select
What i basically want to do is to get content from a website and

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.