Yet Another Perl Switch Statement
source link: https://dev.to/matthewpersico/yet-another-perl-switch-statement-54h3
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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);
}
Enter fullscreen mode
Exit fullscreen mode
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';
}
Enter fullscreen mode
Exit fullscreen mode
with output
we got foo
Enter fullscreen mode
Exit fullscreen mode
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?
Discussion (4)
Collapse
Expand
That’s exactly what for
does, except it’s lexical instead of local
.
Perl v5.10 introduced support for explicitly lexical $_
, but it was removed in v5.24 after being relegated to experimental status in v5.18. (See that last link for the issues it introduced.)
Collapse
Expand
Right, but the Python person I was talking to said that for their reading, the explicit assignment to $_
made a lot more sense to him than implying it from a for
loop. Again, a Python person, "explicit is better than implicit". However, to be honest, I like explicit setting of $_
better than a for loop. So much cleaner IFF you really are using only one value. In fact, I would improve this code with
SWITCH: {
local $_ = 'foo';
...
Enter fullscreen mode
Exit fullscreen mode
And I think I avoid the lexical issues with the local
modifier, yes?
Collapse
Expand
Sure, use a label and then the last statement of your conditions can say last SWITCH;
.
If you like your Perl more “Pythonic” then go for it. Personally I prefer to adopt language idioms rather than adapt one language to another. Similar arguments have occurred about C-style for ( ; ; )
loops vs. foreach
loops that eschew an index variable when it’s not necessary.
There’s an old saying: “You can write FORTRAN in any language.” The Sapir-Whorf hypothesis says that language influences thought. Limiting yourself to the idioms of one language means limiting yourself to solutions that can be expressed in it, and that doesn’t make sense if you’re actually using a different language.
Collapse
Expand
I was going to say that you can't get fall-through with /match/ && do {...}
, but then I started thinking "When was the last time I wanted fall-through? That wasn't parlor-trick code?"
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK