The Take-away for Me

These comments have been so helpful. I've learned some important things about interpolation, and DATA blocks.

At the end of the day, it seems the use of a parseable DATA block was probably too lazy and fancy at the same time, just to be able to write down and then use an easily editable list of regular expressions, which I would use a text editor to change in the future. The more straightforward way is to code the list of regular expressions into a Perl data structure in the script, in much the same way that one would code the value of a constant towards the top of the script, so that it can be spotted and edited in the future.

my @reg_exes = ( q|Reach Holly Smith for help by sending an email\nto hollysmith@nosu +chdomain\.com\.|, q|For more information, contact Holly Smith at\nhollysmith@nosuchdom +ain\.com\.|, q|For more information, contact Holly Smith at \(800\) 555-1212 or\n +via email to hollysmith@nosuchdomain\.com|, ); my $new_string = 'For more information, contact Holly Smith.'; # Then, after creating a list of eligible files in a particular direct +ory # and then opening each file to slurp its contents into $slurped file # (code not shown here)... for my $reg_ex ( @reg_exes ) { if ( $slurped_file =~ s/$reg_ex/$new_string/g ) { print "Matched one or more occurrences of reg ex '$reg_ex' and + substituted '$new_string' each time\n"; } } # Then store the changed file, etc.

Given that the substitution operation potentially will be applied to several thousand files, it's probably better to precompile the regular expressions:

my @patterns = ( q|Reach Holly Smith for help by sending an email\nto hollysmith@nosu +chdomain\.com\.|, q|For more information, contact Holly Smith at\nhollysmith@nosuchdom +ain\.com\.|, q|For more information, contact Holly Smith at \(800\) 555-1212 or\n +via email to hollysmith@nosuchdomain\.com|, ); my @reg_exes; for my $pat ( @patterns ) { push @reg_exes, qr/$pat/; } my $new_string = 'For more information, contact Holly Smith.'; # Then, after creating a list of eligible files in a particular direct +ory # and then opening each file to slurp its contents into $slurped file # (code not shown here)... for my $reg_ex ( @reg_exes ) { if ( $slurped_file =~ s/$reg_ex/$new_string/g ) { print "Matched one or more occurrences of reg ex '$reg_ex' and + substituted '$new_string' each time\n"; } } # Then store the changed file, etc.

In reply to Re: Can't get \n or other character/translation escapes to interpolate if originally read from a data file by davebaker
in thread Can't get \n or other character/translation escapes to interpolate if originally read from a data file by davebaker

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.