The File::Basename module is a library which just defines a number of functions which are useful for extracting pieces of file paths. By giving 'basename' as an argument to the use statement which loads the module, we get the basename() function imported to our main:: namespace. That lets us call it without its fully qualified name, File::Basename::basename, in constructing the output file path. The module is standard, it has shipped with perl for years.
The regex construction is somewhat more compact than you should usually expect to see. The qr// operator is a quote which produces a compiled regex out of its contents. It interpolates variables (things starting with $ or @) in the same way double quotes or qq// do. Instead of producing a named variable with the regex text in it, I used dereference (${}) of a reference to the regex text to get an "anonymous variable" to be interpolated. All that because interpolation in quotes does not call functions, but just fills in things with $ or @ sigils. That line could have been split in two in a way that many would prefer,
my $alt = join '|', keys %substitute; # make one string of all
# keys with pipes between
my $regex = qr/($alt)/; # compile the regex
# with parens to capture
but I prefer having the assignment made in a single statement without superfluous variables.
|