BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Switch Case in Perl

Last Updated: February 7, 2017 by Chaitanya Singh | Filed Under: Perl

switch case is deprecated in Perl 5. If you are wondering why it got deprecated, here is the answer:
Switch case may create syntax errors in other parts of code. On perl 5.10.x may cause syntax error if “case” is present inside heredoc. In general, use given/when instead. It were introduced in perl 5.10.0. Perl 5.10.0 was released in 2007. Source.

However three new keywords: given, when and default got introduced in Perl 5 that provides functionality similar to switch case. This is how it looks:

given (argument) {
  when (condition) { statement(s); }
  when (condition) { statement(s); }
  when (condition) { statement(s); }
  .
  .
  .
  default { statement(s); }
}

Read more about given-when-default here with example.

Top Related Articles:

  1. Perl Tutorial for beginners
  2. Installing Perl on Windows, Mac, Linux and Unix
  3. Until loop in Perl with example
  4. First Perl Program
  5. Use strict and use warnings in Perl

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Perl Tutorial

  • Perl Tutorial
  • Perl Installation
  • First Perl Program
  • Perl Syntax
  • Data types in Perl
  • Perl Variables
  • my keyword
  • Perl Scalars
  • Use strict and use warnings
  • Perl Arrays
  • Perl Hashes
  • Operators in Perl
  • Perl Conditional statements
  • Perl if
  • Perl if-else
  • Perl if-elsif-else
  • Perl unless
  • Perl unless-else
  • Perl unless-elsif-else
  • Perl switch case
  • Perl given-when-default
  • Perl loops
  • Perl subroutines
  • Perl Strings
  • Perl Escape Sequences

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap