/ domm

I hack Perl for fun and
profit.

Follow me on twitter!
Atom Icom ... on Atom!
<<<<<<<<<<
02.09.2013: OpenSource++
16.08.2013: Things I learned at YAPC::Europe 2013 in Kiev
30.05.2013: given & smartmatch in Perl 5.18

As you might have heard, smartmatch (and thus given/when) have been sort of deprecated in Perl 5.18. Well, there not really deprecated, but converted to an experimental feature and thus issue a warning.

While I understand that some cases of smartmatch might be fuzzy / hard to understand, some cases are very simple, straightforward and lead to cleaner code. Especially if combined with give/when.

But as I did not participate in the discussions on p5p regarding the perceived problems with smartmatch, it's a bit late and lame to complain now (c.f. Fresse halten, selber machen).

Anyways...

Here is an example of some nice and simple code powered by smartmatch (from Blio::Node)

  given ($converter) {
      when ('html') { return $raw_content }
      when ([qw(textile markdown bbcode)]) {
          my $o = Markup::Unified->new();
          return $o->format($raw_content, 'textile')->formatted;
      }
      default {
          my $method = 'convert_'.$converter;
          if ($self->can($method)) {
              return $self->$method($raw_content);
          }
          else {
              return "No such converter: $converter".$raw_content;
          }
      }
  }

Here I compare a string against a string (or a list of strings, in the second when). Turning this into an old-school if-else block is of course possible & easy, but ugly.

  if ($converter eq 'html') {
      return $raw_content
  }
  elsif (   $converter eq 'textile'
         || $converter eq 'markdown'
         || $converter eq 'bbcode') {
      my $o = Markup::Unified->new();
      return $o->format($raw_content, 'textile')->formatted;
  }
  else {
      my $method = 'convert_'.$converter;
      if ($self->can($method)) {
          return $self->$method($raw_content);
      }
      else {
          return "No such converter: $converter".$raw_content;
      }
  }

So I decided to keep the given/when and use the also new(?) conditional no warnings thingy:

no if $] >= 5.018, 'warnings', "experimental::feature_name";

Unfortunately, there's a small error in the relevant section of perldelta, where it says

no if $] >= 5.018, "experimental::smartmatch";

But it should say

no if $] >= 5.018, 'warnings', "experimental::smartmatch";

I've already submitted a patch (my first contribution to the Perl core, albeit a very tiny one..)

So what's the point?

Hm, well, I just wanted to say that I like smartmatch and given/when and for now will rather use the no-warnings hack than downgrade back to if/else. Maybe this opinion can be considered in future discussions on smartmatch.

Comments (via disqus)

05.05.2013: 4 TPF Grant Proposals are waiting for some review
21.04.2013: Bread::Board is the right tool for this job
12.04.2013: Is Bread::Board the right tool for this job?
30.03.2013: Acme::ReturnValue 1.001
28.03.2013: Blio featured on Perl maven
18.03.2013: Things I learned at German Perl Workshop 2013
05.03.2013: Vienna.pm Techmeet
>>>>>>>>>>