What is the recommended Perl replacement, or when?

Now that the Perl developers have decided to sort the obsolete data / instructions, is there a recommended replacement, except that you return to if / elsif / else?

+6
source share
2 answers
Chains

if / elsif / else most preferred in most cases - except when something completely different is better than if / elsif / else and given / when , which is actually quite common. Examples of “completely different” approaches are the creation of various types of objects to handle various scenarios, as well as the ability to send a method to do your work for you or to find ways to make your code more data-driven. Both of them, if appropriate, and you do them right, can significantly reduce the number of "switch statement" constructs in your code.

+7
source

As an addition, I found that the combination of "for" and if / elsif / else is good if you have code with a given / when / default that needs to be updated quickly. Just replace given with for and replace the when if cascade of if and elsif and replace default with else . This allows all of your tests to continue to use $_ implicitly, requiring less rewriting. (But keep in mind that other special intelligence matching features will no longer work.)

This is only for rewriting code that already uses the given / when. To write new code, @hobbs has the correct answer.

0
source

Source: https://habr.com/ru/post/955455/


All Articles