Parser combinators vs. regexes, and slurping in Perl
We compare regexes to parser combinators for an early Advent of Code problem, and we learn some Perl peculiarities that help with reading files.
Hello! I hope your week is going well.
New articles
Parser Combinators Beat Regexes
I rarely self-quote but I also rarely manage to write introductions that summarise an article so well:
There seems to be a lack of community cohesion around regex libraries in Haskell. There’s a reason for that. We generally don’t use regexes in Haskell. We use parser combinators instead, because they are almost always better. In other languages, it would be considered overkill to write a full parser when a simple regex can do the same thing. In Haskell, writing a parser is no big deal. We just do it and move on with our lives.
Full article (4–10 minute read): Parser Combinators Beat Regexes
Flashcard of the week
This is a flashcard I wrote early on when re-learning Perl as an adult. (I used Perl as a teenager something like 20 years ago, but re-learned it properly maybe four years ago, when I did a brief stint as a manager with too little time to write code in a real programming language. Perl is still excellent for fast prototyping.)
How does one slurp an entire file in Perl?
Every now and then I do not want to read a file line-by-line. I want a variable that contains a string with all of the file in it. There's an easy way to do it in core Perl.
Localise the record separator $/ to undef and then read as normal.
In a more full example:
open(my $fd, '<', 'some_file.txt') or die $!;
my $contents = do { local $/; <$fd> };
close($fd);
The local
keyword localises a global variable, i.e. provides it with a different value in a limited scope. Here, we don't specify a value at all, which is the same as assigning undef
to it. We make sure to wrap this in a do
block to limit the new scope of $/
to just the file reading.
Back when I didn't know these things about how Perl worked, I would be tempted to pull in the third-party File::Slurp library for something as simple as this!
(But wait is there not the opportunity for a resource leak here, if an exception is thrown before the file is closed? Yeah. What do you expect? It's a quick prototype in Perl! If this script ends up being a permanent temporary hack, it's perfectly fine to refactor it to add better resource management.)
Premium newsletter
One question I have received a lot is if I can put together a short list of the best books I've read, along with brief summaries. I did that, and sent it out in the fourth premium newsletter that went out a couple of weeks ago. (It also has some information on how the list was constructed.)
You can get access to this list (as well as other past and future issues of the premium newsletter) for only $2/month. As interest increases and I learn to hit my stride with publishing these, the price will be set higher for future subscribers. You can cancel at any time, no questions asked.
To upgrade, click the subscription link at the top of this newsletter and fill in your email again.
Your opinions
I cannot improve without feedback. Reply to this email to share your thoughts on any of the topics above, or anything else!Hello! I hope your week is going well.