Version 5.36 of perl was recently released. Amid the new features
it contains, there is a very interesting, and maybe lesser-know one: the new defer keyword.
What does it do? It simply defers the execution of some code to the end of the enclosing block. It’s a very useful
construct, because (in my opinion) it allows to make code much cleaner.
I put down a simple example which renders a checkout page for an fictional e-commerce site. There are some
conditions which bring to different result, with instruction
The first version uses a classic if/else approach. Since there are functions to be called between evaluation
of conditions, the if/elsif approach on a single level does not fit, so a nesting of conditional statements is
Not he best, uh?
The second attempt moves everything to a single level, using return statements. Without nesting, code is more
straightforward but longer, especially the render_template('checkout.html', $tplargs); instruction.
Enter defer, which allows to define at the top (or somewhere else) in the sub something which must happen at
the end of the execution scope.
Cleaner and elegant, isn’t it? It is also informative, as reading the beginning of the sub, it’s immediately
obvious what happens at the end.
I think defer will become a very appreciated addition to the Perl language.