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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:53:45+00:00 2026-05-11T16:53:45+00:00

Is there a neat way of making a case or switch statement in Perl

  • 0

Is there a neat way of making a case or switch statement in Perl 5?. It seems to me they should include a switch on version 6.

I need this control structure in a script, and I’ve heard you can import a "switch module". But how can I achieve it without imports to minimize dependencies and acquire portability?

  • 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-11T16:53:45+00:00Added an answer on May 11, 2026 at 4:53 pm

    If you are using Perl 5.10 you have given/when which is a switch statement (note, it can do more than compare with regexes, read the linked docs to see its full potential):

    #or any of the dozen other ways to tell 5.10 to use its new features
    use feature qw/switch/; 
    
    given($string) {
        when (/^abc/) { $abc     = 1; }
        when (/^def/) { $def     = 1; }
        when (/^xyz/) { $xyz     = 1; }
        default       { $nothing = 1; }
    }
    

    If you are using Perl 5.8 or earlier you must make do with if/elsif/else statements:

    if    ($string =~ /^abc/) { $abc     = 1; }
    elsif ($string =~ /^def/) { $def     = 1; }
    elsif ($string =~ /^zyz/) { $xyz     = 1; }
    else                      { $nothing = 1; }
    

    or nested condition operators (?:):

    $string =~ /^abc/ ? $abc     = 1  :
    $string =~ /^def/ ? $def     = 1  :
    $string =~ /^xyz/ ? $xyz     = 1  :
                        $nothing = 1;
    

    There is a module in Core Perl (Switch) that gives you fake switch statements via source filters, but it is my understanding that it is fragile:

    use Switch;
    
    switch ($string) {
        case /^abc/ {
        case /^abc/ { $abc     = 1 }
        case /^def/ { $def     = 1 }
        case /^xyz/ { $xyz     = 1 } 
        else        { $nothing = 1 }
    }
    

    or the alternate syntax

    use Switch 'Perl6';
    
    given ($string) {  
        when /^abc/ { $abc     = 1; }
        when /^def/ { $def     = 1; }
        when /^xyz/ { $xyz     = 1; }
        default     { $nothing = 1; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a neat way to do this using the Interlocked class? Or should
Is there a neat way of getting a Locale instance from its programmatic name
On Android there's neat way to browse between several pages of data with the
There seems to be a similar question to this on here but with the
Is there a neat way to bind an event handler to an element irrespective
Is it possible (is there a neat way) to get the class comment using
Is there a neat way to apply a mask to a string in a
is there a neat way to add html with jQuery instead of $(#elm).html($(#elm).html() +
Is there a neat way in rails to get a hash of the params
I am making a simplified version of the ball physics app found at this

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.