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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:09:03+00:00 2026-06-12T01:09:03+00:00

In my program I am passing a list of file names from command-line to

  • 0

In my program I am passing a list of file names from command-line to my program, and checking whether each file is – executable, readable and writable..

I am using foreach-when statement for the above problem.. But there seems to be some problem in the use of when and default statements, which may be I’m not using correctly, but its giving me unexpected result..

Here’s my code: –

#!/perl/bin
use v5.14;
use warnings;

foreach (@ARGV) {
    say "*************Checking file $_ *******************";
    when (-r $_) { say "File is Readable"; continue; }
    when (-w $_) { say "File is Writable"; continue; }   # This condition is true
    when (-x $_) { say "File is Executable" }   # This condition is false
    default      { say "None of them" }      # Executed
}

I have added a continue, only to the first two when to make perl check for all the conditions regardless of the name of the file..

Also, I haven’t added a continue to the second last when, because I only want my default to be executed if none of the when is executed..

The problem here is, if the last when condition is false, it will not enter the block, and then it goes on to execute the default even though my first two when statements are satisfied.

I checked the reason of this problem by changing the order of my when, and saw that if only the last when is executed, it will see that there is no continue, and hence it will not execute the default statement..

So, in the above code, I have swapped -x and -r.. My file is readable, so last when in this case will be executed.. And then my default statement is not executed..

#!/perl/bin
use v5.14;
use warnings;

foreach (@ARGV) {
    say "*************Checking file $_ *******************";
    when (-x $_) { say "File is Executable"; continue; }
    when (-w $_) { say "File is Writable"; continue; }
    when (-r $_) { say "File is Readable" }   # This condition is true
    default      { say "None of them" }   # Not executed
}

So, I want to ask, how to handle these kinds of situation.. I want it to work like the way for which given-when statement was added to Perl..
It should check all the when, and skip the default if at least one when is executed..

  • 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-12T01:09:04+00:00Added an answer on June 12, 2026 at 1:09 am

    Since default isn’t an “else condition” but can be seen as a when that always matches, it’s not really a good match for what you’re trying to do. In your default condition, you don’t know anything about earlier matches in that block, and you can’t break out of the topicalizer earlier without knowing if any later when will match, so either you have to “hack it” with a boolean that says one of the earlier matched, or just exchange it for a when that takes care of the “left over” condition;

    foreach (@ARGV) {
        say "*************Checking file $_ *******************";
        when (-r $_)             { say "File is Readable";   continue; }
        when (-w $_)             { say "File is Writable";   continue; }
        when (-x $_)             { say "File is Executable"; continue; }
        when (!-r && !-w && !-x) { say "None of them" }      
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is about passing data from kernel to a user space program. I
I'm writing a program that's parsing an XML file to java objects using smooks.
I'm having trouble implementing a Stack using a linked list with struct. The program
Aside from parsing the function file, is there a way to get the names
I am parsing a file for particular keyword matching by C program, here is
I'm trying to create a parsing system for c#, to block my program from
I've used following code for parsing XML file in c++. http://www.codeproject.com/Articles/176236/Parsing-an-XML-file-in-a-C-C-program . Now I
I am trying to read a large list of English words from a text
Writing a program in which I need to split strings from a struct linked
I want to parse a text file in a C program. The file contains

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.