in reply to Re: Conditional within Hash definition
in thread Conditional within Hash definition

if(...){...}else{...} is considered a statement rather than an expression. Any expression can be used as a statement, but many statements cannot be used as an expression.

In a list (which is what a hash is initialized with), the list items are expected to be given as expressions, not statements.

The ternary operator is an expression, which is why that works.

A do{...} block allows you to provide a block of statements and wrap it up into an expression:

my %weather = ( precipitation => do { if ($rain == 0) { "dry"; } else { "wet"; } }, );
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Replies are listed 'Best First'.
Re^3: Conditional within Hash definition
by AnomalousMonk (Archbishop) on Jan 29, 2014 at 00:04 UTC

    A couple other examples along these lines:

    my ($x, $y, $z) = ( ... ); ... my %weather = ( 'precip' => get_precip_function($x, $y, $z), 'wind_speed' => sub { ... }->($z, $y, $x), ..., );