fREWdiculous!
25 Apr
Yesterday I was reading this post by chromatic and I finally understood what state does. If you look at the perldoc for state you will see why. There is quite a dearth of examples there.
Anyway, here’s a real world example from our code base which uses state in a slightly different way from what is probably typical.
Before:
1 2 3 4 5 6 7 8 9 10 11 12 | { # predeclare a day's duration as well # as the set of weekdays to save time my $day = DateTime::Duration->new(days => 1); my $weekdays = none(1..5); method date_due($start_date, $max_days) { my $ret = $start_date + DateTime::Duration->new(days => $max_days); while($ret->dow eq $weekdays) { $ret -= $day } return $ret; } } |
After:
1 2 3 4 5 6 7 8 9 | method date_due($start_date, $min_days) { # state declares the variables the first time that date_due is run state $day = DateTime::Duration->new(days => 1); state $weekdays = none(1..5); my $ret = $start_date + DateTime::Duration->new(days => $min_days); while($ret->dow eq $weekdays) { $ret -= $day } return $ret; } |
I agree with chromatic on this one; it’s not lifechangingly better, but given enough usage I think it could make things much more clear.
Leave a reply