3 ms·
given/when is also available in Perl 5 from 5.10.1 onward[1]. There are times when it is very helpful. [1] https://perldoc.perl.org/perlsyn.html#Switch-Statem
by ohithereyou 7y ago
given/when is also available in Perl 5 from 5.10.1 onward[1]. There are times when it is very helpful.
[1] https://perldoc.perl.org/perlsyn.html#Switch-Statements https://perldoc.perl.org/perlsyn.html#Switch-Statements
- Grinnz 7y agoDon't use it in Perl 5. You have to first read the whole documentation on what when does[1] and then the whole documentation on what smartmatch does[2] and then you probably still won't know what will happen if you compare two values that might or might not be numbers, because Perl does not make this distinction for you, so smartmatch guesses. There is a reason every other operator has explicit stringy and numeric comparison versions, and optional new stringy bitwise operators were added because they had the same ambiguity[3]. [1] https://perldoc.pl/perlsyn#Experimental-Details-on-given-and-when https://perldoc.pl/perlsyn#Experimental-Details-on-given-and... [2] https://perldoc.pl/perlop#Smartmatch-Operator https://perldoc.pl/perlop#Smartmatch-Operator [3] https://perldoc.pl/feature#The-'bitwise'-feature https://perldoc.pl/feature#The-'bitwise'-feature
- Grinnz 7y agoA more explicit switch statement for only strings or numbers is available from CPAN: https://metacpan.org/pod/Switch::Plain https://metacpan.org/pod/Switch::Plain