in reply to Can't get \n or other character/translation escapes to interpolate if originally read from a data file

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.
  • Comment on Re: Can't get \n or other character/translation escapes to interpolate if originally read from a data file
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Can't get \n or other character/translation escapes to interpolate if originally read from a data file
by LanX (Saint) on Mar 16, 2021 at 22:56 UTC
    I'd recommend
    my @here_docs = ( <<'__RE__', <<'__RE__', <<'__RE__' ); Reach Holly Smith for help by sending an email to hollysmith@nosuchdomain.com. __RE__ For more information, contact Holly Smith at hollysmith@nosuchdomain.com. __RE__ For more information, contact Holly Smith at (800) 555-1212 or via email to hollysmith@nosuchdomain.com __RE__

    in combination with quotemeta

    not sure what the q|...\n...| in single quotes in your code is supposed to do, do you plan to match \ and n literally?

    update

    ok I got the literal \n part

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery