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

Dear Monnks,

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.

It is a bit frustrating, but we would not be humans if we could not learn from mistakes. Anyway, you kindly offered your help in your last reply and I think I need it (again), since I cannot figure out the current error messages I have been getting

First message is Cygwin-related (not sure if this is the right place, should I open a new post?).

What I can figure out is that Cygwin won't install the module, but I have not idea why, since any other installation of perl modules on this machine worked like a charm.

NE Configuring S/SM/SMUELLER/PathTools-3.47.tar.gz with Makefile.PL Checking if your kit is complete... Looks good Generating a Unix-style Makefile Writing Makefile for Cwd Writing MYMETA.yml and MYMETA.json SMUELLER/PathTools-3.47.tar.gz /usr/bin/perl Makefile.PL -- OK Running make for S/SM/SMUELLER/PathTools-3.47.tar.gz cp lib/File/Spec/OS2.pm blib/lib/File/Spec/OS2.pm cp lib/File/Spec/Mac.pm blib/lib/File/Spec/Mac.pm cp lib/File/Spec/VMS.pm blib/lib/File/Spec/VMS.pm cp lib/File/Spec/Functions.pm blib/lib/File/Spec/Functions.pm cp lib/File/Spec/Epoc.pm blib/lib/File/Spec/Epoc.pm cp lib/File/Spec/Cygwin.pm blib/lib/File/Spec/Cygwin.pm cp lib/File/Spec.pm blib/lib/File/Spec.pm cp Cwd.pm blib/lib/Cwd.pm cp lib/File/Spec/Unix.pm blib/lib/File/Spec/Unix.pm cp lib/File/Spec/Win32.pm blib/lib/File/Spec/Win32.pm Running Mkbootstrap for Cwd () chmod 644 Cwd.bs /usr/bin/perl.exe /usr/lib/perl5/5.14/ExtUtils/xsubpp -typemap /usr/l +ib/perl5/5.14/ExtUtils/typemap Cwd.xs > Cwd.xsc && mv Cwd.xsc Cwd.c gcc-4 -c -DPERL_USE_SAFE_PUTENV -U__STRICT_ANSI__ -g -fno-strict-ali +asing -pipe -fstack-protector -DUSEIMPORTLIB -O3 -DVERSION=\"3.47\" + -DXS_VERSION=\"3.47\" "-I/usr/lib/perl5/5.14/i686-cygwin-threads-64 +int/CORE" -DDOUBLE_SLASHES_SPECIAL=0 Cwd.c /bin/sh: gcc-4: Kommando nicht gefunden. Makefile:367: recipe for target 'Cwd.o' failed make: *** [Cwd.o] Error 127 SMUELLER/PathTools-3.47.tar.gz make -- NOT OK Failed during this command: SMUELLER/PathTools-3.47.tar.gz : make NO cpan[2]>

I switched to my DOS cmd and thought this would work out, however, when running the script, I got this error message

c:\dev>perl script.pl Wide character in die at C:/strawberry/perl/site/lib/Path/Tiny.pm line + 1492. Error opendir on '´╗┐Z:/../file.xml': Invalid argument at +script.pl line 57.

line 57 in the script would be the line with ->children qr/.../(*.xml), which retrieves the xml files:

sub RetrieveAndBackupXML { #~ my( $directory ) = @_; ## same as shift @_ ## same as shift my $directory = shift; my $date = POSIX::strftime( '%Y-%m-%d', localtime ); #sets cu +rrent date and time to be added to backup file my $bak = "$date.bak"; #date added to the .bak file my @xml_files = path( $directory )->children( qr/\.xml$/ ); for my $file ( @xml_files ) { Replace( $file, "$file-$bak" ); #sub Replace using it's 2 para +meters as defined below } }

Those xml-files are unicode, so I am wondering why I am getting this wide character message again

Would be grand if you guys could assist me in this matter, if you need more information about the code or my system let me know.

Kind regards

C.

Replies are listed 'Best First'.
Re^12: search and replace strings in different files in a directory
by Corion (Patriarch) on Sep 01, 2014 at 12:11 UTC
    /bin/sh: gcc-4: Kommando nicht gefunden.

    This means you should install the gcc cygwin package. But unless you really, really need a recent version of File::Spec, don't upgrade File::Spec. It comes with the Perl core distribution and there rarely is a need to upgrade it. I haven't read the previous replies you already got, so there might be a reason to actually upgrade the module, but unless there is a pressing need to do so, I wouldn't.

      Dear Corion,

      Thanks for the hint, I will see to upgrading Cygwin as you pointed out.

      I forgot to mention that my question was related to an error message I got from the shell, i.e.

      File::Spec version 3.4 required--this is only version 3.33 at /usr/lib +/perl5/site_perl/5.14/Path/Tiny.pm line 12.

      So I thought I'd just upgrade the module as the prompt said, but then things got messy (cf. previous post). Or is there another error I failed to spot?

      Thanks a mil in advance for your help

      C.

        Ah, yes, if another module seems to want a more recent version of File::Spec, then upgrading File::Spec is likely the path of least resistance.

Re^12: search and replace strings in different files in a directory
by choroba (Cardinal) on Sep 01, 2014 at 12:18 UTC
    ╗┐Z:/../file.xml

    Seems like BOM.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Dear Choroba,

      Thanks for enlighten me, I was not aware of this particular issue. I found out that there is a module for that in perl, but since the code I am using does not use file handles at the crucial part, I am not sure how to use File::BOM correctly. Is there a way to use File::BOM with Path::Tiny or would I have to create a new script?

      Thanks a mil in advance for your comments

      Kind regards

      C.
        I'd just remove the BOM manually before passing the string to Path::Tiny:
        my $directory = shift; my $BOM = "\x..."; # Fill in the actual problematic string $directory = s/$BOM//;
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^12: search and replace strings in different files in a directory
by Anonymous Monk on Sep 01, 2014 at 22:47 UTC

    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)

    /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

    makes it more obvious what sub feeds the dying one

      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