An Interest In:
Web News this Week
- March 22, 2024
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
March 23, 2022 03:56 pm GMT
Original Link: https://dev.to/matthewpersico/yet-another-perl-switch-statement-54h3
Yet Another Perl Switch Statement
I am in the middle of a project at my job where we are converting some Perl to Python :-(. In the conversion I was explaining this invocation of a switch
statement:
for (ref($thing)) { /ARRAY/ && do { some_array_thing($thing); last; }; /LIST/ && do { some_list_thing($thing); last; }; ## default; some_scalar_thing($thing);}
After explaining how the for
statement sets $_
, I was asked, "Why not just set $_
?"
Indeed, why not? You can play with the following code here: http://tpcg.io/KN9H82
use strict;use warnings;{ local $_ = 'foo'; /bar/ && do { print 'we got bar'; last }; /foo/ && do { print 'we got foo'; last }; /eek/ && do { print 'we got eek'; last }; print 'we dropped through';}
with output
we got foo
So, my questions are:
- Why have I never seen this in any discussion of Perl's
switch
synonyms? - Is there any inherent problem with this?
Original Link: https://dev.to/matthewpersico/yet-another-perl-switch-statement-54h3
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To