DevHeads.net

perl script prints parts of strings in the wrong order

hello,

I am trying to reuse an old perl script I wrote years ago, on an Ubuntu
16.-04LTS x86_64 box.
It behaves in a very odd way now, and I cannot figure out if it is the
code that is not
compatible with current versions of Perl, or if there is something VERY
strange happening
between the script, and the terminal it runs in.

The part of the script that works badly is this:

while (<INPUTFILE>) {
chomp;
my $LINE = $_;
many lines that "clean up" $LINE, removing certain substrings etc...

@F = split /:/, $LINE;
print "CURL:==> $F[0] ++ $CURLRES{$F[0]} ;;\n";
}

when I run it, SOME of the printed lines (say 1 every 30) have the
expected format:

CURL:==> string_a ++ string_b ;;

but all the others are like this:

;;L:==> string_c ++ string_d ;;

that is, the three initial "CUR" characters are replaced by " ;;"

It's as if something had pasted the last three characters over the first
three ones.

I have no idea what is going wrong, and why. Any help is appreciated,

TIA,

Marco

Comments

SOLVED (sort of): perl script prints parts of strings in the wro

By M. Fioretti at 08/07/2018 - 00:43

as usual, I stumped in the solution one second after posting to the
list, but I
can't say I completely understand it, so any comment is welcome.

I piped the output of the script through "od -c", and saw lots of \r
characters
right in the places where pieces of strings were "swapped".

So I changed this:

chomp;

to this:

chomp;
s/\r//g;

and now everything works as intended. Problem is, *why* did I have to do
this?
I thought the "chomp" command in Perl also strips those \r characters
out, and
I am pretty sure it did, earlier.

Thanks,
Marco

On 2018-08-07 07:28, M. Fioretti wrote:

Re: SOLVED (sort of): perl script prints parts of strings in the

By Olivier Nicole at 08/07/2018 - 00:56

"M. Fioretti" < ... at nexaima dot net> writes:

According to perl documentation, chomp removes the eding of a line that
corresponds to teh current value of $/ so it really depends what you set
into $/

Bests,

livier

Re: SOLVED (sort of): perl script prints parts of strings in the

By M. Fioretti at 08/07/2018 - 01:07

I know that. That is the reason why I was caught off guard. I have
become
so accustomed, for many years, to have $/ = "\r\n" by default on any
Linux
box I worked on, that I thought to anything but a different value for
it.

Of course, my memory may very well fail me on this. At this point, mine
is just a curiosity like "did $/ change at some point in Ubuntu, or
better:
in Perl as packaged for Ubuntu?" But just an unimportant curiosity,
really, no need to
reply unless you really have the answer on the top of your head.

Thanks,
Marco

Re: SOLVED (sort of): perl script prints parts of strings in the

By Colin Watson at 08/07/2018 - 03:08

On Tue, Aug 07, 2018 at 08:07:05AM +0200, M. Fioretti wrote:
That certainly isn't my memory, and I'd find it extremely surprising for
a Unix build of perl to have $/ set to anything but "\n" by default. I
really think you're misremembering something here.

Perhaps you're used to doing file I/O via the :crlf layer (see
PerlIO(3perl)), which would convert raw \r\n to \n when reading it from
files? But on Unix you'd have to be a bit careful to apply that only on
input, not output.

A quick web search finds plenty of old posts asking similar questions,
indicating that this isn't a recent change. For instance:

<a href="https://www.perlmonks.org/?node_id=549385" title="https://www.perlmonks.org/?node_id=549385">https://www.perlmonks.org/?node_id=549385</a>
<a href="https://www.perlmonks.org/?node_id=504626" title="https://www.perlmonks.org/?node_id=504626">https://www.perlmonks.org/?node_id=504626</a>
<a href="https://stackoverflow.com/questions/650743/in-perl-how-to-remove-m-from-a-file" title="https://stackoverflow.com/questions/650743/in-perl-how-to-remove-m-from-a-file">https://stackoverflow.com/questions/650743/in-perl-how-to-remove-m-from-...</a>
<a href="https://perlmaven.com/chomp" title="https://perlmaven.com/chomp">https://perlmaven.com/chomp</a>

I don't have any properly old environments to hand right now, but at
least Debian 7 and Ubuntu 12.04 behaved the same way. A quick test
case:

$ perl -e '$_ = "foo\r\n"; chomp; print' | od -tx1
0000000 66 6f 6f 0d
0000004

Re: SOLVED (sort of): perl script prints parts of strings in the

By M. Fioretti at 08/07/2018 - 04:32

On 2018-08-07 10:08, Colin Watson wrote:
probably yes, and this explanation you gave sounds quite plausible:

thanks again!

Marco

Re: perl script prints parts of strings in the wrong order

By Olivier Nicole at 08/07/2018 - 00:37

"M. Fioretti" < ... at nexaima dot net> writes:

A wild guess, but there may be something strange in the input file,
like some lines contain a \r.

I would manually check the input file and also try to print the
resulting $F[0] alone on a line:

print "--$LINE--\n";
print "++$F[0]++\n";

Olivier

Re: perl script prints parts of strings in the wrong order

By M. Fioretti at 08/07/2018 - 00:49

indeed, that was the case, thanks! See my other "SOLVED" reply.

What I don't understand is why the perl "chomp" function did not remove
those characters too, together with the \n ones.

I am pretty sure it worked that way, in the past.

Marco