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:

#!/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" } } }
Of course, we may avoid the inner given:
#!/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!

--
If you can't understand the incipit, then please check the IPB Campaign.

In reply to Re: Bring Out Your New Perl Code (humour) by blazar
in thread Bring Out Your New Perl Code by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.