{"id":2084003,"date":"2022-01-20T02:36:00","date_gmt":"2022-01-20T00:36:00","guid":{"rendered":"http:\/\/nhub.news\/?p=2084003"},"modified":"2022-01-20T06:03:26","modified_gmt":"2022-01-20T04:03:26","slug":"get-out-early-with-perl-statement-modifiers","status":"publish","type":"post","link":"http:\/\/nhub.news\/fr\/2022\/01\/get-out-early-with-perl-statement-modifiers\/","title":{"rendered":"Get Out Early With Perl Statement Modifiers"},"content":{"rendered":"<p style=\"text-align: justify;\"><b>Learn how to avoid deeply-nested conditions in your code with guard clauses using Perl\u2019s statement modifiers.<\/b><br \/>\nJoin the DZone community and get the full member experience. When I first started writing Perl in my early 20\u2019s, I tended to follow a lot of the structured programming conventions I had learned in school through Pascal, especially the notion that every function has a single point of exit. For example: This could get pretty convoluted, especially if I was doing something like validating multiple arguments. And at the time I didn\u2019t yet grok how to handle exceptions with eval and die, so I\u2019d end up with code like: What a mess. Want to count all those braces to make sure they\u2019re balanced? This is sometimes called the arrow anti-pattern, with the arrowhead(s) being the most nested statement. The default ProhibitDeepNests perlcritic policy is meant to keep you from doing that. The way out (literally) is guard clauses: checking early if something is valid and bailing out quickly if not. The above example could be written: With Perl\u2019s statement modifiers (sometimes called postfix controls) we can do even better: (Why if instead of unless? Because the latter can be confusing with double-negatives.) Guard clauses aren\u2019t limited to the beginnings of functions or even exiting functions entirely. Often you\u2019ll want to skip or even exit early conditions in a loop, like this example that processes files from standard input or the command line: Of course, if you are validating function arguments, you should consider using actual subroutine signatures if you have a Perl newer than v5.20 (released in 2014), or one of the other type validation solutions if not. Today I would write that postal function like this, using Type::Params for validation and named arguments: Note that was this part of a larger program, I\u2019d wrap that print_postal_address call in a try block and catch exceptions such as those thrown by the code reference $check generated by compile_named. This highlights one concern of guard clauses and other \u201creturn early\u201d patterns: depending on how much has already occurred in your program, you may have to perform some resource cleanup either in a catch block or something like Syntax::Keyword::Try\u2019s finally block if you need to tidy up after both success and failure. Published at DZone with permission of Mark Gardner, DZone MVB. See the original article here. Opinions expressed by DZone contributors are their own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to avoid deeply-nested conditions in your code with guard clauses using Perl\u2019s statement modifiers. Join the DZone community and get the full member experience. When I first started writing Perl in my early 20\u2019s, I tended to follow a lot of the structured programming conventions I had learned in school through Pascal, especially [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2084002,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[93],"tags":[],"_links":{"self":[{"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/posts\/2084003"}],"collection":[{"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/comments?post=2084003"}],"version-history":[{"count":0,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/posts\/2084003\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/media\/2084002"}],"wp:attachment":[{"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/media?parent=2084003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/categories?post=2084003"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nhub.news\/fr\/wp-json\/wp\/v2\/tags?post=2084003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}