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.

After Compline,
Zaxo


In reply to Re^3: Easy find and replace loop. HELP! by Zaxo
in thread Find and replace by five character string by wrml

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.