Help language development. Donate to The Perl Foundation

Slang::Otherwise zef:elcaro last updated on 2023-05-25



Slang::Otherwise - Slang to add 'otherwise' block to 'for' loops


This slang adds syntax for an otherwise block that will run if the for loop is not entered.

use Slang::Otherwise;

for dir.grep(*.basename.contains: 'xyx') -> $f {
    say "Found $f"
otherwise {
    say 'Got nothing'


This code is shamelessly taken from a blog post by Damian Conway.

Damian's slang called the block else, but the post spawned some discussion about whether that is a good name, also taking into consideration that Python's for/else does something entirely different. In commenting on the article, several people suggested otherwise, which is a good a name as any.


The Artistic License 2.0

See LICENSE file in the repository for the full license text.