How do I use "when" to check for "one of two options"? - Page 3

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Re: How do I use "when" to check for "one of two options"?

El 28/04/15 a las 10:54, Martijn Lievaart escribió:
Quoted text here. Click to load it

Functionally equivalent if $_ is string of only one character.
But if you want to discuss for the pleasure of discussing...
Do you want speed tests?

The generation of random numbers is too important to be left to chance

Re: How do I use "when" to check for "one of two options"?

Quoted text here. Click to load it

This is something which probably happened to be true for the original
example but it isn't necessarily true for other conceivable uses of
given/ when.

use feature 'switch';

sub finde_affen
    given (lc($_[0])) {
    when ('affe') {
        print("Es ist ein Affe!\n");

    default {
        print("Ein $_ ist kein Affe.\n");

sub finde_affe
    given ($_[0]) {
    when (/affe/i) {
        print("Ist ein $_ ein Affe?\n");
    default {
        print("Ein $_ ist kein Affe.\n");

my @dingsbumse = qw(affe Kamel Nashorn Affe Affenbrotbaum Zahnstocherzerspanungsmachine affenzahn);

finde_affen($_) for @dingsbumse;


finde_affe($_) for @dingsbumse;

As to the more general given/when problem: It's sometimes sensible to
avoid reinventing the square wheel over and over again by looking at
'how other people do it' instead of experimenting wildly. Multiway
conditionals are by no way a new invention, rather a standard feature of
higher-level programming languages pretty much since they came into

According to a quick, informal survy, LISP had some sort of
multiway conditional since ever in the form of cond. More restricted
constructs more similar to non-LISP mulitways appeared in InterLISP and
MacLISP (caseq/ selectq). Algol 68 had a multiway conditional but I
wasn't able to determine its intended semantics (or even syntax) within
a reasonable amount of time from the Algol 68 language report[*]. COBOL
gained support for multiway conditionals with COBOL 85. All of these are
fairly alien when seen in the context of Perl.

Considering more closely related languages, C had switch since its
conception (and Pascal case). The "programming language simulated by the
cfront preprocessor" obviously shares all good things with C. Java has a
switch statement. I'm purposely ignoring PHP.

This incomplete list should be sufficient to demonstrate that this is
not some "dubious new concept" while people with properly working (or
"properly restricted") brains have always been contempt with if - then -
else cascades.

As to the properties, in C, the controlling expression of a switch
statements has to be of integer type. case-labels must use compile-time
constants. There's no syntax for ranges or sets of values but more than
one case label may be attached to the same block.

The Pascal case supports ordinal types, enumerated types, chars and (as
an extension) strings for case  cases which must be constants. Ranges or
sets of values are also supported.

The Java switch used to support integer or enum types for 'case cases',
meanwhile, it also supports strings. They must be compile-time
constants. Ranges or sets are not supported but - similar to C - more
than one case may be attached to a statement block.

The common denominator of all of these (which are actually used in
practice instead of being considered experiments) is that there's some
kind of controlling expression which has to evualuate to something which
can be compared for equality using built-in language features and that
it is compared with a disjunct set of constant expressions. Comparing
with a set of constants for a single case is always supported, only
Pascal provides the additional syntactic sugar of supporting range
notation for such sets.

This would provide a suitable, basic set of features for a
non-experimental multiway conditional which could then be extended as
consensus on sensible extensions is reached, as opposed to the
"everything we can think of pushed into it upfront somehow plus a couple
of kitchen sinks to be on the safe side" approach behind 'smart

Possibly offensive (and possibly non-funny) joke below:
[*] In order to emphasize the proud legacy of languages which don't
exist because of their specification, of the committee-me-harder
organization necessary to create such a specification, of the throw out the
baby to distill the bathwater approach taken by such committees and -
last but not least - to hint at the fact that - ultimatively - the thing
has to be unplugged for the good of mankind in case it refuses to accept
its own fallibillity, we hereby announce that the language erronously
referred to as 'Perl 6' shall be known as ALGOL 2001 everhereafter.

Re: How do I use "when" to check for "one of two options"?

How about just starting with

my $c = lc(get_character);

Quoted text here. Click to load it

Site Timeline