Please edit your post to correct the formatting. See Markup in the Monastery. You should at least use <code> tags around your config file so that it can be displayed properly.

The "search and replace from a list of replacements" part can be done using a hash, and the substitution operator.

use strict; use warnings; my $text = "I will not stir from this place, do what they can."; my %replacements = ( "will not" => "won't", "stir" => "move", "this place" => "here", "do what they can" => "let them try"); my $pattern = join "|", map quotemeta, keys %replacements; print "The pattern is: $pattern\n"; my $replaced = $text =~ s/($pattern)/$replacements{$1}/gr; print $replaced;
The pattern part is a regular expression if you don't know about them you can look at those tutorials.
The quotemeta escapes the special characters in the search items so that they are interpreted litteraly in the regular expression.
The s/search/replace/ part is what does the actual job, the search part will capture the strings it finds (because of the parenthesis) into $1, and $replacements{$1} will return the matching replacement. The g in /gr means "general", and will apply the change everywhere in the string, not just once, and the r means "return", so the result is returned to $replaced, instead of changing $text itself.

As for reading your config file, one of the config modules might do what you want.


In reply to Re: string replacement by Eily
in thread string replacement by colox

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.