This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)
Task #2 this week is another one of mine. The idea is to take any number of pathnames on the command line (and we must support at least three), and output a side-by-side diff of the files that are different. This is a skin-deep analysis, so there is no recursion, and no comparison of file sizes, modification times, or contents, although those would be worthy additions if someone wants to take it further.
I will be implementing a fairly basic version, although I have a more complex version I’ve been using for years that I might be persuaded to release if there is interest.
Data Design
I am going to have one main data structure, %dirs
, that will be built up as we traverse the directories. It will hold all directory and file information, as well as a maxlen
value for each directory, for ease of formatting columns later. Here’s what %dirs
might look like, given a very simple set of input directories:
%dirs = (
dir_a => {
files => {
"Arial.ttf" => 1,
"Comic_Sans.ttf" => 1,
},
maxlen => 14,
},
dir_b => {
files => {
"Arial.ttf" => 1,
"Comic_Sans.ttf" => 1,
"Courier_New.ttf" => 1,
},
maxlen => 15,
},
)