NAME

Cond::Expr - Conditionals as expressions

VERSION

version 0.05

SYNOPSIS

my %args = (
    foo => 'bar',
    (cond
        ($answer == 42) { answer => $answer }
        ($answer)       { wrong_answer => 1 }
        otherwise       { no_answer    => 1 }
    ),
);

DESCRIPTION

This module implements a Lisp-alike cond control structure.

How is this different from…

  • given/when

    given is a statement, not an expression, and is therefore not readily usable as part of an expression unless its use is wrapped within a do block, which is cumbersome.

    Additionally, this module avoids all the, possibly unwanted, side effects given/when and its underlying smart matching mechanism happen to impose.

  • if/elsif/else

    Similar to given, if is a statement, needing special care in order to be useful as part of a surrounding expression.

  • Nested ternary ?:

    Using nested ternary ?: expressions, such as in

    my %args = (
        foo => 'bar',
        (
              ($answer == 42) ? (answer => $answer)
            : ($answer)       ? (wrong_answer => 1)
            :                   (no_answer => 1)
        ),
    );

    can be used to achieve functionality similar to what this module provides. In fact, the above use of ?: is exactly what the "SYNOPSIS" for this module will compile into. The main difference is the cond syntax provided by this module being easier on the eye.

FUNCTIONS

cond

Takes a set of test/expression pairs. It evaluates each test one at a time. If a test returns logical true, cond evaluates and returns the value of the corresponding expression and doesn't evaluate any of the other tests or expressions. When none of the provided tests yield a true value, () or undef is returned in list and scalar context, respectively.

PERL REQUIREMENTS

Due to the particular XS interfaces being used, this module requires a minimum Perl version of 5.014.

SUPPORT

Bugs may be submitted through the RT bug tracker (or bug-Cond-Expr@rt.cpan.org).

AUTHOR

Florian Ragwitz <rafl@debian.org>

CONTRIBUTORS

  • Karen Etheridge <ether@cpan.org>

  • David Mitchell <davem@iabyn.com>

  • Olivier Mengué <dolmen@cpan.org>

  • Sawyer X <xsawyerx@cpan.org>

COPYRIGHT AND LICENCE

This software is copyright (c) 2012 by Florian Ragwitz.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.