in reply to Strange behavior with STDIN

G'day slugger415,

The effect you're seeing with the four characters of ".mp3" overwriting the first four characters of "newfile", generally indicates an embedded carriage return. Such as:

$ perl -e 'my $x = "newfile"; my $y = "\r.mp3"; print "$x$y\n"' .mp3ile

You haven't shown your whole code, or cut it down to something that reproduces the behaviour (see SSCCE).

I suspect you haven't used the strict and warnings pragmata: they're certainly not included with the code you posted.

Your $str appears to be a package variable: action-at-a-distance is a possibility.

Interspersing \n and $/ is questionable. Why are you doing this?

— Ken

Replies are listed 'Best First'.
Re^2: Strange behavior with STDIN
by slugger415 (Monk) on Oct 26, 2021 at 16:56 UTC

    Thank you for the helpful response. I was using strict but not warnings; adding the latter uncovered a couple of minor issues unrelated to this.

    Oddly what did work was adding two chops (not chomps).

    chop $str; chop $str;

    Odd because I've never run across this problem before.

    As to why I'm interspersing \n and $/, well let's just call it lazy typing, sometimes one seems easier than the other. :-)

    Thanks again.

      $str =~ s/\s+\z//; is a far better (and more portable) solution.