Using String Variables Containing Literal Escapes in Perl Substitution

I am new to Perl and I have found behavior that I do not understand and cannot solve.

I am doing a small search and replace program, and I need to do something. I have a bunch of files that I need to process. Then I have a list of search / replace rules in an external text file. When replacing there, I need three special things:

  • UTF-8 character substitution (Czech diacritics)

  • Work with adding / removing lines (this is how it works in slurp mode)

  • Use regular expressions

I want the program to work alone, so I wrote it so that it takes three arguments:

  • File to work
  • What to search
  • What to replace.

I am sending parameters in a loop from a bash script that parse a list of rules and load other files.

, "\n" , Perl script. ( ), , ( ), \n .

hardcoding "\n" , , .

, Perl "\n", ?

:

list.txt -

1\. ?\\n?NÁZEV PŘÍPRAVKU;\\n<<K1>> NÁZEV PŘÍPRAVKU;

farkapitoly.sh - bash script list.txt Perl script

...
FILE="/home/tmp.txt"
while read LINE
do
   FIND=`echo "$LINE" | awk -F $';' 'BEGIN {OFS = FS} {print $1}'`
   REPLACE=`echo "$LINE" | awk -F $';' 'BEGIN {OFS = FS} {print $2}'`
   perl -CA ./pathtiny.pl "$FILE" "$FIND" "$REPLACE" 
done < list.txt
...

pathtiny.pl - Perl script

#!/usr/bin/perl
use strict;
use warnings;
use Modern::Perl;
use utf8; # Enable typing Unicode in Perl strings
use open qw(:std :utf8); # Enable Unicode to STDIN/OUT/ERR and filehandles

use Path::Tiny;

my $file       = path("$ARGV[0]");
my $searchStr  = "$ARGV[1]";
my $replaceStr = "$ARGV[2]";

# $replaceStr="\n<<K1>> NÁZEV PRÍPRAVKU";       # if I hardcode it here \n is replaced right away
print("Search String:",  "$searchStr",  "\n");
print("Replace String:", "$replaceStr", "\n\n");

my $guts = $file->slurp_utf8;
$guts =~ s/$searchStr/$replaceStr/gi;
$file->spew_utf8($guts);

, Linux Mint 13 64-bit VirtualBox ( Win 8.1), Perl v5.14.2. UTF-8 Linux.

pastebin. this.

. , .

+4
3

, , ,

xx\ny

. , , . , "$replaceStr", , xx\nyy . (, , Perl, , Module::Names.)

eval - /e .

my $str = '<b>';
my $r = 'xx\ny';

$str =~ s/b/$r/;

xx\ny, .

/e , , , $r, xx\ny .

, /e, , /e, eval . , qq{ .. }, .

$str =~ s/b/qq{"$r"}/ee

perl qq{"$r"} , "xx\nyy", , - , 'xx' . "\n" . 'yy'.

use strict;
use warnings;

my $s = '<b>';
my $r = 'xx\nyy';

$s =~ s/b/qq{"$r"}/ee;

print $s;

<xx
yy>

, , ,

my $r = 'xx\n"yy"'

, , .

, String::Escape, unbackslash, \n ( escape-) "\n". , , , .

, , unbackslash $r, , . $r - , .

, String::Escape,

use strict;
use warnings;

use String::Escape 'unbackslash';

my $s = '<b>';
my $r = 'xx\nyy';

$s =~ s/b/unbackslash $r/e;

print $s;

.


, String::Escape. Path::Tiny, , inplace-edit Perl, perlvar.

#!/usr/bin/perl

use utf8;
use strict;
use warnings;
use 5.010;
use open qw/ :std :utf8 /;

use String::Escape qw/ unbackslash /;

our @ARGV;

my ($file, $search, $replace) = @ARGV;

print "Search String: $search\n";
print "Replace String: $replace\n\n";

@ARGV = ($file);
$^I = '';

while (<>) {
   s/$search/unbackslash $replace/eg;
   print;
}
+3

\n . ( 1: \ n, newline.

Perl \n , (, ).

:

my $replaceStr=eval qq("$ARGV[2]"); #evaling a string causes interpreting the \n as literal

, eval, String-Escape cpan module. ( unbackslash)

+2

, , . .

, ( , eval ). , String::Escape, ( , ).

, , :

use strict;
use warnings;

my $r = 'xx\nyy';

$r =~ s/(\\.)/qq{"$1"}/eeg;  # Translate \. as a double quoted string would

print $r;

:

xx
yy
0

All Articles