perlquestion
LanX
Honorable Monks,<P><P>
After this discussion -> [id://826587], I'm trying to understand the extended functionality of the new [doc://perlsyn#Switch-statements|Given/When] compared to the old workaround with For/Next.<P><P>
As far as I understand,
<li>In <c>given/when</c> I have to write a <c>continue</c> (statement not block!) where I omit the <c>next</c> statement. I need to "invert" the fall through.
<li> In <c>given/when</c> I lose the continue-block which is always executed.
<li> The <c>when</c> has an automatically built in smart match <c>~~</c>. Nice ...but how can I take profit from this smart match if the parameter passed into <c>given</c> can only be a scalar?<P><P>
Maybe I missed something, please see the example code to understand what I mean:<P><P>
CODE:
<readmore>
<c>
use feature "switch";
$,=$\="\t";
@test=qw/abc def abcdef nnn/;
print "\n\n=== Given/When";
for (@test){
print "\nGIVEN($_):";
given($_) {
when (/abc/) { print "abc";continue }
when (/def/) { print "def" }
when (/xyz/) { print "xyz" }
default { print "default" }
}
}
print "\n\n=== For";
for (@test){
for ($_){
print "\nFOR($_):";
if (/abc/) { print "abc"}
if (/def/) { print "def" ;next}
if (/xyz/) { print "xyz" ;next}
print "default";
}
}
print "\n\n=== For/Continue";
# "simplifying" the former with post-ifs and &&
# using continue-block
my @res;
for (@test){
push (@res, "abc") if (/abc/);
push (@res, "def") && next if (/def/);
push (@res, "xyz") && next if (/xyz/);
push (@res, "default");
} continue {
print "\nFOR/CONT($_):",@res;
@res=();
}
print "\n\n === When smartmatch\n";
print "\n= GIVEN(\@test):\n";
given(@test) {
print "whats tested is:",$_;
when (/abc/) { print "abc";continue }
when (/def/) { print "def" }
when (/xyz/) { print "xyz" }
default { print "default" }
}
print "\n= GIVEN(\\\@test):\n";
# does when act differently when a arr_reff is passed?
given(\@test) {
print "whats tested is:",$_;
when (/abc/) { print "abc";continue }
when (/def/) { print "def" }
when (/xyz/) { print "xyz" }
default { print "default" }
}
# whats the point of implicit smartmatch if only scalars can be testet??? smartmatchingan array is different
print "\nBUT: def ~~ \@test!" if @test ~~ /def/;
</c>
</readmore>
OUTPUT:
<c>
=== Given/When
GIVEN(abc): abc default
GIVEN(def): def
GIVEN(abcdef): abc def
GIVEN(nnn): default
=== For
FOR(abc): abc default
FOR(def): def
FOR(abcdef): abc def
FOR(nnn): default
=== For/Continue
FOR/CONT(abc): abc default
FOR/CONT(def): def
FOR/CONT(abcdef): abc def
FOR/CONT(nnn): default
=== When smartmatch
= GIVEN(@test):
whats tested is: ARRAY(0x8a08a38) default
= GIVEN(\@test):
whats tested is: ARRAY(0x8a08a38) default
BUT: def ~~ @test!
</c><P><P>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-708738">
<p>Cheers Rolf
</div></div><P><P>
UPDATE: extended <c>given(Array)</c> and <c>given(Array_ref)</c><P><P>
Since <c>given</c> accepts arrays and automatically passes the ref without <c>when</c> reacting accordingly, I suppose it's a BUG! <P><P>