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.


Hi Leon


I thought it was just me having to read the smart match docs a few times in order to be confused :-).

I don't know how many times I've consulted that table for how smatch match works with different argument types - too many times. I also was bitten by a change to in between 5.10 and 5.10.1 where a simple if @a contains $b now needs to be written is $b in @a. Because of that I had to go through all my code changing them around.

Leave a comment

About Leon Timmermans