I am new to perl.
Currently, my task is to loop through all the .htm files in the folder and do two things:
- Replace all the “.htm” extension to “.xml”
- Replace all the white space in the file name to underscore.
In my perl script, I put in these two lines:
@pub=`ls $sourceDir | grep '\.htm' | grep -v Default | head -550`;
foreach (@pub) {
my $docName = $_;
chomp($docName);
$docName =~ s/.htm$//g;
$docName =~ s/ /_/g;
....}
$docName is the variable that holds the current full name of the file (including extension). Interestingly, those two $docName replacement statements, at any time, only the above one worked. For example, the example code here, will only replace the extension but leaves out the space, but if I comment out the first, then white space turns into underscore perfectly, but extensions remain “.htm”.
Could experts help me? And could also suggest where I went wrong? Thanks in advance!
Are you sure that’s all
is doing?
You need to escape the
.and add your replacement string. You also don’t needgsince right anchor guarantees it can only happen once.Other than that, you should be fine.