3 ms·
This is something Ruby likely got form Perl,but they cribbed it wrong. Perl's post-conditionals only work on single statements, not blocks. You can use them f
by kbenson 10y ago
This is something Ruby likely got form Perl,but they cribbed it wrong. Perl's post-conditionals only work on single statements, not blocks. You can use them for functional style statements though. I.e.
# Not allowed
for ( 1 .. 10 ) {
say $_;
} if 1;
# Allowed
map { say $_ } ( 1 .. 10 ) if 1;
The idea seems to be that a post-conditional should be simple and obvious by the time you've parsed the statement. A block makes it confusing, as does mixing post control structures
Post-loop structures have the same limitation:
# Not allowed
{ say "foo"; say $_ } for ( 1 .. 10 );
# Allowed
say $_ for ( 1 .. 10 );
When used correctly, these can become very succinct and clear.
my %data = get_data_record_hash("foo");
$data{$_} = update_field(data{$_}) for ('field1','field4',other_field');
# Compare to map, which normally you expect to return values
map { $data{$_} = update_field(data{$_}) } ('field1','field4',other_field');
The single statement limitation keeps you from going wild in ways that are probably not useful to future readers of the code (including you).