So what does your new code look like?
I personally believe that I should first warn any newbie possibly reading this post in the future that its content is deliberately humorous in a sarcastic way. So please don't even think of picking up the code shown here and use it in anything serious...
I... huh... personally believe that with 5.10's new features we can eventually implement in Perl the FOR-CASE paradigm:
Of course, we may avoid the inner given:#!/usr/bin/perl use strict; use warnings; use 5.010; for my $count (0..4) { given ($count) { when (0) { say "Whoa, it's $count" } when (1) { say "Then it becomes $count" } when (2) { say "Then, $count" } when (3) { say "Only to further increase to $count" } when (4) { say "And eventualy stop at $count" } } }
#!/usr/bin/perl use strict; use warnings; use 5.010; for (0..4) { when (0) { say "Whoa, it's $_" } when (1) { say "Then it becomes $_" } when (2) { say "Then, $_" } when (3) { say "Only to further increase to $_" } when (4) { say "And eventualy stop at $_" } } __END__
But we care about efficiency all the time, don't we? Furthermore, this may begin to give a wrong output the day a number which is equal to 0 will also be equal to 1, for example.
Last, prior to 5.10, one of the standard answers for those in search of a switch statement was to use a dispatch table instead, so for backwards compatibility reasons, let's see how it may look like:
#!/usr/bin/perl use strict; use warnings; local $\="\n"; for my $count (0..4) { print +([ sub { my $n=shift; "Whoa, it's $n" }, sub { my $n=shift; "Then it becomes $n" }, sub { my $n=shift; "Then, $n" }, sub { my $n=shift; "Only to further increase to $n" }, sub { my $n=shift; "And eventualy stop at $n" } ]->[$cou +nt] || sub {} )->($count); } __END__
Or at most, using closures:
#!/usr/bin/perl use strict; use warnings; local $\="\n"; for my $count (0..4) { print +([ sub () { "Whoa, it's $count" }, sub () { "Then it becomes $count" }, sub () { "Then, $count" }, sub () { "Only to further increase to $count" }, sub () { "And eventualy stop at $count" } ]->[$count] || sub () {} )->(); } __END__
Anyway, with 5.10 it's much slicker!
In reply to Re: Bring Out Your New Perl Code (humour)
by blazar
in thread Bring Out Your New Perl Code
by Limbic~Region
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |