shilo has asked for the wisdom of the Perl Monks concerning the following question:

The loop below is used in a script that updates the version line in perl scripts that are version controlled in git. The script is in .git/hooks/pre-commit.

I am running windows, and this script is executed by c:\Perl\bin\perl.exe. However, it is called from within git's shell. Here is my uname -a from within the git shell:

$ uname -a MINGW64_NT-6.1 w72-station 2.4.0(0.292/5/3) 2015-12-12 15:39 x86_64 Ms +ys

The problem is the print statements within this code are putting '. in the front of some of the strings and leaving off the first few characters...

LINE: while (my $line = <$input_fh>) { chomp $line; #print '.'; if ($line =~ m{(.+)\$Id:.+v.*(\d+\.\d+\.\d+)-(\d+).*\$}xms +) { my $string_to_print = "Found version line: '$line'.\n" +; $string_to_print =~ s{[^[:ascii:]]}{}g; print $string_to_print; my $preamble = $1; my $current_version = $2; my $new_build_number = $3; $new_build_number++; my $date = serial_stamp({without_seconds => 1}); my ($sec,$mins,$hours,@rest) = localtime(time); my $time = sprintf "%02d:%02d", $hours, $mins; my $new_line = "$preamble\$Id: $file, v $current_versi +on-${new_build_number} $date $time dbotham Exp \$;"; print "Orig Version: '$line'.\n"; STDOUT->autoflush(1); print "New Version: '$new_line'.\n"; STDOUT->autoflush(1); print $output_fh "$new_line\r\n"; next LINE; } print $output_fh "$line\n"; }

In the paragraph below I use ${} to quote strings, as the single quote character is actually part of the messed up output and I need to quote things somehow...

Notice that ${Found version line:} becomes ${'.und version line:}, which is really strange. Any and all google searchers I can think of wall point me to chomp, chop, substr, etc...

Also notice that ${New Version:} does not exhibit the problem. This is one of the strangest perl problems I have ever encountered...

$ git commit -m "Updated line endings git-push-all-remotes.pl." '.und version line: 'Readonly my $ID => q$Id: git-push-all +-remotes.pl, v 0.2.11-002495 20160114 13:35 dbotham Exp $; '.ig Version: 'Readonly my $ID => q$Id: git-push-all-remot +es.pl, v 0.2.11-002495 20160114 13:35 dbotham Exp $; New Version: 'Readonly my $ID => q$Id: git-push-all-remot +es.pl, v 0.2.11-002496 20160114 13:57 dbotham Exp $;'. Done. Adding updated file, 'git-push-all-remotes.pl', back for commit. Updated build numbers in 1 files. [master f444ca1] Updated line endings git-push-all-remotes.pl. 1 file changed, 1 insertion(+), 5 deletions(-)

Thank you in advance!

Any help will really appreciated.

If you need more of the script to help, please let me know...

Thanks again... david...

Replies are listed 'Best First'.
Re: print Is Substituting Characters At Beginning Of String
by Corion (Patriarch) on Jan 14, 2016 at 19:18 UTC
    chomp $line;

    You still have the carriage returns (\r) at the end of your strings, which will move the cursor to the start of the line again and then overwrite stuff.

    I recommend stripping all whitespace from the end of the strings:

    $line =~ s!\s*$!!;

      Perfect! That was exactly it. Thank you!

        Hello shilo,

        As an aid to debugging, you can configure Data::Dumper to print whitespace characters (other than spaces) as backslash sequences by setting the $Data::Dumper::Useqq configuration variable:

        18:58 >perl -MData::Dumper -wE "my $s = qq[\tabc\r\n]; print Dumper($s +); $Data::Dumper::Useqq = 1; print Dumper($s);" $VAR1 = ' abc '; $VAR1 = "\tabc\r\n"; 18:58 >

        But I generally prefer Data::Dump, which does this by default:

        18:58 >perl -MData::Dump -wE "my $s = qq[\tabc\r\n]; dd $s;" "\tabc\r\n" 19:00 >

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,