
Fred
User
Aug 20, 2012, 3:34 AM
Post #1 of 9
(17398 views)
|
Strange behavior of variable in substitution expression
|
Can't Post
|
|
Hello, I am working on a simple search/replace program to replace some characters by another ones in a file. The program ask the user the substitution he wants to make. I would like to replace a newline by a tabulation with this program but I never succeeded If the user enters with the keyboard <\n> as a search character and then <\t> as a replacement character, the newline \n is well recognized by the program, but the tabulation \t is not. As a result, the program replace each newline by the character <\t>. If in my program, I set : $with = "\t" ; Then the program works perfectly, but I did not found how to enter \t with the keyboard (I tried "\t", '\t', \t ...), nothing work... Many thanks in advance for your help !
# Simple search / replace program print "Replace: " ; my $replace = (<>) ; chomp $replace ; $replace = qr/$replace/ ; # use $replace as a regexp print "with: " ; my $with = (<>); chomp $with ; open(FILEIN, $file_in) ; open(FILEOUT, "> $file_out") ; while (<FILEIN>) { s/$replace/$with/g ; print FILEOUT "$_" ; }
|