Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^28: search and replace strings in different files in a directory

by PitifulProgrammer (Acolyte)
on Sep 18, 2014 at 08:50 UTC ( [id://1101017]=note: print w/replies, xml ) Need Help??


in reply to Re^27: search and replace strings in different files in a directory
in thread search and replace strings in different files in a directory

Dear Anonymous Monk(s)

I recently went back to the script and was trying to implement the changes you kindly provided. Unfortunately, I did not succeed in resolving the issue with umlauts.

I think this is mostly due to the fact that I do not understand what the snippet you referred to really does and how it can be integrated into the present script.

This is the snippet from the other post.

#!/usr/bin/perl -- BEGIN { if ( eval { require Win32; 1 } ) { require ex::override; require Win32::Unicode::Native; ex::override->import( GLOBAL_stat => sub (;*) { &Win32::Unicode::Native::stat }, GLOBAL_lstat => sub (;*) { &Win32::Unicode::Native::stat } +, map({ my $name = $_; my $prototype = prototype("CORE::$name"); "GLOBAL_$name" => eval "sub($prototype){&Win32::Un +icode::Native::$name}"; } qw/ chdir link mkdir open readlink rename rmdir symlink unlink utime closedir opendir readdir /, ) ); } } use Path::Tiny qw/ path /; for my $drive ( @drives ){ my @picdirs = grep /^\d{4}_\d{2}_\d{2}$/, eval { path( $drive, $dir )->children }; if( @picdirs ){ ...; } }

I can see in the sample code from the pathnames_under_windows_8 post that path tiny is called, but I have been unable to decipher the code above and how it should affect path::tiny

Moreover, I was wondering when you mentioned that Tiny had already done the decoding for me. If so, why are there still problems with umlauts, is that OS-related.

I am asking because I figure understanding the underlying problem might give me an insight about how to resolve this. I know I am moving in a more or less theoretical discussion, but it might help.

Your insight and comments are much appreciated. Sorry in advance for all the rookie questions (well there is so much to learn and it is fun).

Looking forward to your reply.

Kind regards

C.

PS: I guess it helps re-posting the current version of the replace script I am talking about

use 5.014; use strict; use warnings; use Path::Tiny qw/ path /; use POSIX(); use autodie qw/ close /; use File::BOM; use Carp::Always; use Data::Dump qw/ dd /; use Encode qw(encode decode); Main( @ARGV ); exit( 0 ); sub Main { #my( $infile_paths ) = @_; #if run via my( $infile_paths ) = 'C:\dev\test_path_names.txt'; chomp $infile_paths; my @paths = GetPaths( $infile_paths ); for my $path ( @paths ){ RetrieveAndBackupXML( $path ); } return @paths; } ## end sub Main sub GetPaths { use File::BOM; ## my @paths = path( shift )->lines_utf8; my @paths = path( shift )->lines( { binmode => ":via(File::BOM)" } + ); s/\s+$// for @paths; # "chomp" return @paths; } ## end sub GetPaths sub RetrieveAndBackupXML { my( $directory ) = shift; ## same as shift @_ ## my $date = POSIX::strftime( '%Y-%m-%d', localtime ); #suffix + for the backup-file, e.g. 2014-08-01 my $bak = "$date.bak"; my @xml_files = path( $directory )->children( qr/\.xml$/ ); for my $file ( @xml_files ) { Replace( $file, "$file-$bak" ); } } ## end sub Main # Fix xml entities and create a copy of the original file before editi +ng sub Replace { my( $in, $bak ) = @_; path( $in )-> copy( $bak ); #create a copy of $in with the ending( +s) specified in $bak my $infh = path( $bak )->openr_raw; my $outfh = path( $in )->openrw_raw; while( <$infh> ) { s{&}{&amp;}g; ## In some case does not match as intended s{\s>\s}{&gt;}g; s{\s<\s}{&lt;}g; print $outfh $_; } close $infh; close $outfh; } ## end sub Replace

Replies are listed 'Best First'.
Re^29: search and replace strings in different files in a directory
by PitifulProgrammer (Acolyte) on Oct 01, 2014 at 09:55 UTC

    Dear Monks

    I have been busy with other things than coding, but the script is still bugging me.

    Since I cannot change how people write their pathnames, I think I need to finish this coding project.

    Well, I have been trying to implement the code that was provided by you via the link to the pathnames_under_windows_8 post.

    Since I not could see a variable or anything related to path::tiny in the new snippet, I figured I just needed to put it into my code (cf. below). The files in the strangely named folders remain untouched though.

    Is this topic covered elsewhere, maybe in a book or so, I really would like to understand the code properly but this is rather a conundrum and surely a bit too advanced for my puny perl skills.

    Thanks a mil for your help

    Kind regards

    C

    #!/usr/bin/perl -- use 5.014; use strict; use warnings; use Path::Tiny qw/ path /; use POSIX(); use autodie qw/ close /; use File::BOM; use Carp::Always; use Data::Dump qw/ dd /; Main( @ARGV ); exit( 0 ); BEGIN { if ( eval { require Win32; 1 } ) { require ex::override; require Win32::Unicode::Native; ex::override->import( GLOBAL_stat => sub (;*) { &Win32::Unicode::Native::stat }, GLOBAL_lstat => sub (;*) { &Win32::Unicode::Native::stat } +, map({ my $name = $_; my $prototype = prototype("CORE::$name"); "GLOBAL_$name" => eval "sub($prototype){&Win32::Un +icode::Native::$name}"; } qw/ chdir link mkdir open readlink rename rmdir symlink unlink utime closedir opendir readdir /, ) ); } } sub Main { #my( $infile_paths ) = @_; #if run via my( $infile_paths ) = 'C:\dev\test_paths_files.txt'; chomp $infile_paths; my @paths = GetPaths( $infile_paths ); for my $path ( @paths ){ RetrieveAndBackupXML( $path ); Win32Checker( $path ); } return @paths; } ## end sub Main sub GetPaths { use File::BOM; ## my @paths = path( shift )->lines_utf8; my @paths = path( shift )->lines( { binmode => ":via(File::BOM)" } + ); s/\s+$// for @paths; # "chomp" return @paths; } ## end sub GetPaths sub RetrieveAndBackupXML { my( $directory ) = shift; ## same as shift @_ ## my $date = POSIX::strftime( '%Y-%m-%d', localtime ); #suffix + for the backup-file, e.g. 2014-08-01 my $bak = "$date.bak"; my @xml_files = path( $directory )->children( qr/\.xml$/ ); for my $file ( @xml_files ) { Replace( $file, "$file-$bak" ); } } ## end sub Main # Fix xml entities and create a copy of the original file before editi +ng sub Replace { my( $in, $bak ) = @_; path( $in )-> copy( $bak ); #create a copy of $in with the ending( +s) specified in $bak my $infh = path( $bak )->openr_raw; my $outfh = path( $in )->openrw_raw; while( <$infh> ) { s{&}{&amp;}g; ## In some case does not match as intended s{&amp;amp;}{&amp;}g; s{\s>\s}{&gt;}g; s{\s<\s}{&lt;}g; print $outfh $_; } close $infh; close $outfh; } ## end sub Replace

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1101017]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-24 05:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found