Skip to content

Instantly share code, notes, and snippets.

@dolmen
Created June 27, 2016 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dolmen/f9de389b566751296c1666e417584c58 to your computer and use it in GitHub Desktop.
Save dolmen/f9de389b566751296c1666e417584c58 to your computer and use it in GitHub Desktop.
Equivalents of C++ 2016 "if statement with initializer", in Perl 5
# Equivalents of C++ 2016 "if statement with initializer", in Perl 5
# http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
#
if ((my $p = $m->try_emplace($key, $value)), !$p->second) {
FATAL("Element already registered")
} else {
process($p->second)
}
sub foo() {
if ((my $c = bar()) != SUCCESS) {
return $c
}
...
}
# Other examples
if ((my $x = 0) != 0) {
say $x
} else {
say $x-1
}
# Unfortunately, $x escapes the "if" block, so this is not as strict as C++.
say $x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment