June 2011 Archives

Smarter matching

Smartmatching is complicated. Anyone suggesting otherwise simply hasn't used it much. Something that appears to be as simple as when @array can have 5 rather different meanings depending on the value of $_, the two most important being «each element in @$_ matches the corresponding element in @array» and «$_ matches any of @array». This is madness. I believe there's a lot of value in being able to explicitly differentiate when list(@array) and when any(@array).

Hence Smart::Match. It's a module that does exactly that and more. Not only does it allow you to disambiguate smartmatches, it also allows you to use powerful new matches by using higher order matchers. For example it doesn't just allow you to match against string_length(4), but also string_length(even) or string_length(range(13,42)). This makes smartmatching a lot easier and saner than doing it raw.

About Leon Timmermans