Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Don't know if below is a <em>neat use case</em> or just a tip of the hat to Perl's linguistic lineage :)</p> <pre><code># things todo (or should have done!) at this time of the day: given (TheTime-&gt;of_day) { when ('morning') { breakfast(); make_packed_lunch() if $_-&gt;is_work_day; } lunch() when 'afternoon'; when ('evening') { goto_pub() if $_-&gt;is_friday; dinner(); } default { say "Should be sleeping if its " . $_-&gt;{dt}-&gt;ymd } } </code></pre> <p>And if you view <code>$_</code> has <em>"it"</em> then it works particular well (IMHO).</p> <p>The above works by <a href="http://perldoc.perl.org/overload.html" rel="nofollow noreferrer">overloading</a> the smart match operator which <code>given/when</code> rely on. Here is how <code>TheTime</code> class could be written to make my example work:</p> <pre><code>{ package TheTime; use DateTime; use overload '~~' =&gt; '_check_hour', fallback =&gt; 1; our %day_time = ( morning =&gt; [0..11], afternoon =&gt; [12..17], evening =&gt; [18..23], ); sub of_day { my $class = shift; bless { dt =&gt; DateTime-&gt;now, }, $class; } sub is_work_day { shift-&gt;{dt}-&gt;day_of_week ~~ [1..5] } sub is_friday { shift-&gt;{dt}-&gt;day_of_week == 5 } sub _check_hour { my ($self, $greeting) = @_; $self-&gt;{dt}-&gt;hour ~~ $day_time{$greeting}; } } </code></pre> <p>/I3az/</p> <p>PS. Also see this blog post i did recently: <a href="http://transfixedbutnotdead.com/2010/08/30/givenwhen-the-perl-switch-statement/" rel="nofollow noreferrer">given/when – the Perl switch statement</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload