in reply to Re: search and replace strings in different files in a directory (Path::Tiny)
in thread search and replace strings in different files in a directory
Sorry for not having replied earlier. I was working on something else. However, last week, I was asked to have a go at the script again.
What has changed is that the directories and files should now be accessed individually.
The idea was to have a text file containing the pathnames in a network folder.
My issue and the reason for reopening the question is how to add the different pathnames from the text-file, which are stored in the array to the subroutine
I am having some difficulties with the line containing 'directory', because none of the changes I made actually worked
I figure the pathnames have to go in there.
I've been getting the following error messages
c:\dev>perl search_and_replace.pl Global symbol "$directory" requires explicit package name at search_an +d_replace.pl line 33. Execution of search_and_replace.pl aborted due to compilation errors.
c:\dev>perl search_and_replace.pl Path::Tiny paths require defined, positive-length parts at search_and_ +replace.pl line 33
c:\dev>perl search_and_replace.pl Variable "$path" is not imported at hornbach_mit_verz_suche.pl line 34 +. (Did you mean &path instead?) Global symbol "$path" requires explicit package name at search_and_rep +lace.pl line 34.
I can explain some of the errors such as variable scope (I don't think I can just $path inside the sub), but the question remains how to get the pathnames into the sub, but I have not yet touched subroutines in detail yet.c:\dev>perl search_and_replace.pl ': Filename too long at search_and_replace.pl line 34.
I am sorry for all these questions, this script has evolved in a way I could not have aniticipated and the guys at work are eager for a solution.
Oh yes, before I forget, this is the latest version of the script
use 5.014; use strict; use warnings; use File::Slurp qw(:all); use Path::Tiny qw/ path /; use POSIX(); use autodie qw/ close /; my $infile_paths = 'C:\temp\test_folder\paths.txt'; my @paths = read_file $infile_paths, { binmode => ':utf8' } or die $!; chomp(@paths); # say for @paths; foreach my $path (@paths){ Main($path), } Main( @ARGV ); exit( 0 ); sub Main { my $directory = ""; my $date = POSIX::strftime( '%Y-%m-%d', localtime ); my $bak = "$date.bak"; my @xml_files = path( @paths )->children( qr/\.xml$/ ); for my $file ( @xml_files ) { Replace( $file, "$file-$bak" ); } } ## end sub Main sub Replace { my( $in, $bak ) = @_; path( $in )->move( $bak ); my $infh = path( $bak )->openr_raw; my $outfh = path( $in )->openrw_raw; while( <$infh> ) { s{&}{&}g; ## will match more than what you want fix it s{&amp;}{&}g; s{\s>\s}{>}g; s{\s<\s}{<}g; print $outfh $_; } close $infh; close $outfh; } ## end sub Replace
Thank you all so much for going through the trouble and explaining stuff to beginner. You guys rock, keep it going! I wish I had more time to play with the code and read up on certain aspects.
Thank you so much. Looking forward to your reply.
Please let me know if you need additional information, I still need to get used describing my problems in detail
Kind regards C.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: search and replace strings in different files in a directory (Path::Tiny)
by Anonymous Monk on Aug 19, 2014 at 09:49 UTC | |
by PitifulProgrammer (Acolyte) on Aug 19, 2014 at 13:51 UTC | |
by Anonymous Monk on Aug 19, 2014 at 23:40 UTC | |
by PitifulProgrammer (Acolyte) on Aug 20, 2014 at 08:57 UTC | |
by PitifulProgrammer (Acolyte) on Aug 27, 2014 at 14:13 UTC | |
|