in reply to Re: Easy find and replace loop. HELP!
in thread Find and replace by five character string

Thanks you very much for the help. I'm interested to learn why this works. I think I understand everything except how the module works and what is happening here:

my $regex = qr/(${\join '|', keys %substitute})/;

If you would be so kind as to explicate I would be oh so grateful.

walter

  • Comment on Re^2: Easy find and replace loop. HELP!

Replies are listed 'Best First'.
Re^3: Easy find and replace loop. HELP!
by Zaxo (Archbishop) on Aug 12, 2005 at 06:30 UTC

    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