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"; } }, );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Conditional within Hash definition
by AnomalousMonk (Archbishop) on Jan 29, 2014 at 00:04 UTC |