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

I am getting the strange feeling that my undertaking of writing a "simple" script turns out to be a much larger project than I had anticipated.

Its not that much larger, its just that there is a lot of details :) Its like learning to sing in a new language, there is new sentence structure and new word list ... and you still have to practice hitting your notes :)(reading about it is different than doing it)

Think pre-teen versus a mother versus a car mechanic -- pre-teen probably knows that the wheels go round and round, mom knows when to drive for service, and mechanic knows how to replace parts (including what manual to read )

You're at the mom stage studying for mechanic -- you can get there if you're willing :) its just details

probably student driver stage trying to pass the written exam (analogies are hard:)

/bin/sh: gcc-4: Kommando nicht gefunden. Makefile:367: recipe for target 'Cwd.o' failed make: *** Cwd.o Error 127

Like Corion already said, looks like you're missing gcc-4 :)

Wide character in die at ... Path/Tiny. ... line 57 in the script

Isn't that wonderfully convenient? I think so (problem detected, error reported informatively)

So there is a problem with the filename, its got extra stuff. So, to solve the cause you work your way backwards, which function feeds RetrieveAndBackupXML? Which function feeds that function? Is it still called GetPaths? That is where you employ File::BOM, like

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

other way without File::BOM

sub GetPaths { my @paths = path( shift )->lines_utf8; s/\x{feff}//g for @paths; # "de-bom" s/\s+$// for @paths; # "chomp" return @paths; } ## end sub GetPaths

A tip: if you add use Carp::Always; you will get a stack of error messages, like

use Carp::Always; use Path::Tiny qw/ path /; sub f{g(@_);} sub g { r( @_ ); } sub r { path(shift)->lines } f(666); __END__ Error open (<) on '666': No such file or directory at C:/citrusperl/si +te/lib/Path/Tiny.pm line 1469. Path::Tiny::Error::throw('Path::Tiny::Error', 'open (<)', 666, 'No + such file or directory') called at C:/citrusperl/site/lib/Path/Tiny. +pm line 126 Path::Tiny::_throw('Path::Tiny=ARRAY(0xabe0a4)', 'open (<)') calle +d at C:/citrusperl/site/lib/Path/Tiny.pm line 740 Path::Tiny::filehandle('Path::Tiny=ARRAY(0xabe0a4)', 'HASH(0xae824 +c)', '<', undef) called at C:/citrusperl/site/lib/Path/Tiny.pm line 9 +00 Path::Tiny::lines('Path::Tiny=ARRAY(0xabe0a4)') called at - line 5 main::r(666) called at - line 4 main::g(666) called at - line 3 main::f(666) called at - line 6
makes it more obvious what sub feeds the dying one

Replies are listed 'Best First'.
Re^13: search and replace strings in different files in a directory
by PitifulProgrammer (Acolyte) on Sep 02, 2014 at 09:33 UTC

    Dear Anonymous Monk

    Thank you very much for your explanation and your encouraging words. You are right, creating code for a particular task is different than reading the book(s) and going through the exercises.

    I think I still lack this eye for detail and sometimes I am aware of the problem but cannot figure out how to put the solution into code syntax.

    Such was the case with File::Bom. I just did not know how to add to that particular part of the subroutine (not enough practice with modules and objects).

    Thank you also for introducing Carp, I will have a go at it.

    Thanks a mil again for your support, makes the whole a bit easier and it surely is more fun. (This goes for all contributors btw)

    Kind regards

    C.

      Dear Monks

      Sorry for pestering you once again with my noob questions. I changed the script according to your recommendations. So far no more BOM errors and everything seems to be working as planned.

      However, I got the following error message the meaning of which I am not quite sure of. Maybe one of you could shed some light on this (I used Carp::Always, as one of you suggested). I had to anonymise the code for I guess you know why....

      Error opendir on 'Z:/123/xyz/xyz/xyz_xyz_xyz_/01_abc': No such file or directory at C:/strawberry/perl/site/lib/Path/Tiny.pm +line 1490. Path::Tiny::Error::throw("Path::Tiny::Error", "opendir", "Z:/2014/xyz/ +xyz/xyz_xyz_xyz_/01_abc"") called at C:/strawberry/perl/site/lib/Path/Tiny.pm line 126 Path::Tiny::_throw(Path::Tiny=ARRAY(0x2774f74), "opendir") called at C +:/strawberry/perl/site/lib/Path/Tiny.pm line 532 Path::Tiny::children(Path::Tiny=ARRAY(0x2774f74), qr(\.xml$)u) called +at script.pl line 59 main::RetrieveAndBackupXML("Z:/123/xyz/xyz/xyz_xyz_xyz_/01_abc\\0"...) + called at script.pl line 31 main::Main() called at script.pl line 20

      In my opinion, the error is telling me that neither the folder nor the files exist (well, you cannot access the files if you cannot access the folder.

      Question is why is the programm throwing this error message, the folders do exist as indicated by the path names I am reading out.

      Is it problem if I access a network-drive? Would that be another building block that would bloat the script and provide with me with yet another topic to read about?

      A penny for your thoughts.

      Kind regards

      C

        Hmm, you say it exists as a directory, does the path contain any unicode codepoints ? What do you get with this program

        #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; use Win32; use Win32::Unicode qw/ statW /; my $path = 'Z:/123/xyz/xyz/xyz_xyz_xyz_/01_abc'; dd( $path ); dd({ -ansi, Win32::GetANSIPathName( $path ) }); dd({ -full, scalar Win32::GetFullPathName( $path ) }); dd({ -short, Win32::GetShortPathName( $path ) }); dd( statW( $path ) ); __END__