Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

s/\r\n/\n/ passed as command line parameters not working

by ggrise (Initiate)
on Jan 17, 2013 at 17:49 UTC ( #1013841=perlquestion: print w/ replies, xml ) Need Help??
ggrise has asked for the wisdom of the Perl Monks concerning the following question:

Hi wise perl monks, I have a question:
zentara in a prior post had supplied some code (below) which recursively replaced text in files. You called the program for example:
perl zsr.pl 'gaggle' 'geese' txt
and it works just fine replacing gaggle with geese in .txt files. However if I try to do a dos to unix file conversion
perl zsr.pl '\r\n' '\n' txt
it finds the windows "carriage return line feed" just fine but replaces that with the text \n instead of a "line feed".
if I replace the line:
$_ =~ s/$search/$replace/g;
with
$_ =~ s/\r\n/\n/g;
it does the dos to unix conversion ok
why ?
Thanks for your time.

#!/usr/bin/perl # by zentara # Recursively searches down thru directories replacing patterns in fil +es. # usage zsr 'search' 'replace' 'ext'(optional with no .) # use '' for null string in $replace # ex: zsr 'type1' 'series A' use warnings; use strict; use File::Find; my ($search,$replace,$ext) = @ARGV; if (defined $ext) {$ext = ".$ext"} else {$ext = '.*'}; die "Usage : zsr 'search' 'replace' 'extension' (extension optional)\n +" if ($search eq ""); find (\&wanted, "."); sub wanted { my $open = $_; my $tempfile = 0; if (!($open =~ /$ext$/i) or (-d||-B||-l)) {return} print $open,"\n"; my $mode = (stat $open)[2]; #print $mode,"\n"; #printf "Permissions are %04o\n", $mode & 07777; open (TEMP,">> $tempfile"); open (FH, "< $open") or die "Can't open $open: $!\n"; while (<FH>) { $_ =~ s/$search/$replace/g; print TEMP $_; } close FH; close TEMP; rename ($tempfile, $open) or die "Can't rename $open: $!\n"; chmod ($mode,$open) or die "Can't restore permissions to $open, possib +ly wrong owner: $!\n"; }

Comment on s/\r\n/\n/ passed as command line parameters not working
Download Code
Replies are listed 'Best First'.
Re: s/\r\n/\n/ passed as command line parameters not working
by choroba (Canon) on Jan 17, 2013 at 18:03 UTC
    The variable $replace contains two characters, \ and n. You can create a Perl expression from its value and evaluate it:
    s/$search/'"'.$replace.'"'/eeg;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks, that fixed the code. I appreciate your time and I learned something.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013841]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2015-07-08 02:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls