by davidw · 14y ago · 0 comments
if { $something } {
do this
} else {
do something else
}
The first one is actually an expression, but the do this and do something else are simply blocks of code passed to the 'if' command.PS. Here's an example of creating something similar with Perl:
sub IF (&@) {
my ($block, $flow) = @_;
[ map { $flow->{$_} || sub {} } qw/else then/ ]->[ $block->() ]->();
}
sub THEN (&@) {
my $block = shift;
my $flow = shift || {};
$flow->{then} = $block;
$flow;
}
sub ELSE (&) {
my $block = shift;
+{ else => $block };
}
IF { 1 == 0 } THEN { say "True" } ELSE { say "False" };
And with these being functions and not statements we can also do: my $result = IF { 1 == 1 } THEN { "True" } ELSE { "False" };