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

  • Home
  • SEARCH
  • 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 1043395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:39:39+00:00 2026-05-16T15:39:39+00:00

Perl 5.10 introduced a proper switch construct with given/when and it seems like a

  • 0

Perl 5.10 introduced a proper switch construct with given/when and it seems like a powerful tool.

Currently however, perldoc perlsyn lacks some good examples.

One case where I found it handy lately was for using it with file test operators:

given (-d "foo/bar/") {
    when (1) { ... } # defined is wrong as -d returns '' on a file.
    default { ... }
}

or alternatively:

given ("foo/bar/") {
    when (-d) { ... }
    default { ... }
}

For me, especially the first version looks better than an if-else construct or using the ternary operator, when depending on the outcome of the test I need to perform actions in both cases.

It made me wonder though, what else looks neat beyond the simple case of falling back to smart matching and avoiding overlong if-elsif-elsif-…-else structures?

I have a hunch that given/when makes it possible to be clever without losing clarity, but I don’t have any good examples.

One thing that surprised me though, is that you can nest the construct aswell:

given ($filename) {
        when (-e) {
                when (-f) {
                        when (-z) { say "Empty file" }
                        default { say "Nonempty file" }
                }
                when (-d) {
                        when (-o) { say "Directory owned by me"}
                        default { say "Directory owned by someone else" }
                }
                default { say "Special" }
        }
        default { say "No such file or directory" } }
  • 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-16T15:39:39+00:00Added an answer on May 16, 2026 at 3:39 pm

    In a recent answer to Zaid’s question Strategies to handle a file with multiple fixed formats, I ended up with a poor man’s yacc whose main loop searched an array of regular expressions for the first match:

    while (<>) {
      given($_) {
        when (@{[ map $pattern{$_}, @expect ]}) {}
        default {
          die "$0: line $.: expected " . join("|" => @expect) . "; got\n$_";
        }
      }
    }
    

    In another question, David B wanted to match against multiple regexes, and my answer uses smart matching to implicitly loop over the regexes:

    #! /usr/bin/perl
    
    use warnings;
    use strict;
    
    use feature 'switch';
    
    my @patterns = (
      qr/foo/,
      qr/bar/,
      qr/baz/,
    );
    
    for (qw/ blurfl bar quux foo baz /) {
      print "$_: ";
      given ($_) {
        when (@patterns) {
          print "hit!\n";
        }
        default {
          print "miss.\n";
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just been introduced to traits (traits in Smalltalk, roles in Perl). I'd like
In Perl, you are able to call a function by reference (or name) like
I just introduced threads to a Perl program, where one of its modules was
Perl seems to be killing my array whenever I read a file: my @files
I have just been introduced to the Django framework and I would like to
I'm chasing a bug in Perl code that seems to fundamentally be a version
Perl has the -c switch to compile the code without running it. This is
Perl's WWW::Mechanize returns complete garbage (like ðäD=°lc*Ñ2\7 õä1û¼Âc{oî´lDNöÓ8ß5o*F2Õ©-õ£z§2ÜÝ1,³íäþwɯÓQÞÆÁS¿IZDKÁ»,ËmÅS1r4!°s,¥4Jl;\J~í2¼) after retrieving a page. I am
Perl offers this very nice feature: while ( <> ) { # do something
Perl is really good for writing the kind of string/file parsing programs that I

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.