in reply to Perl appears to be dropping last character of line
G'day Carol,
Welcome to the Monastery.
What you're describing is typically caused by embedded characters. A carriage-return ("\r") is the most usual culprit:
$ perl -e 'my $x = "abc\r"; print "|$x|"' |abc
Without the carriage-return ("\r") the problem of losing the final character ("|") disappears:
$ perl -e 'my $x = "abc"; print "|$x|"' |abc|
The problem usually arises when processing data from one OS (operating system) on a different OS. They have different line-endings: "\n" (Unix-style systems, including Linux & Mac OS X); "\r" (Mac versions earlier than Mac OS X); "\r\n" (MSWin).
The problem may not be a carriage-return but some other control character; for instance, something like an embedded backspace could potentially cause similar problems. Use ord to identify which, if any, embedded characters may exist:
$ perl -E 'my @x = ("A", "B\n", "C\r", "D\r\n"); for (@x) { say ord fo +r split //; }' 65 66 10 67 13 68 13 10
Say, on a Unix-like system, you have an entire, unconverted record from an MSWin file that looks like: "abc\r\n". Using chomp (which, on a Unix-like system, will consider a newline to be the line-ending character) will only remove the terminal "\n" resulting in "abc\r" (which I used in the original example).
$ perl -E 'my @x = ("A", "B\n", "C\r", "D\r\n"); for (@x) { chomp; say + ord for split //; }' 65 66 67 13 68 13
In these cases, I often find it useful to remove the generic line-ending (\R) — see perlrebackslash: \R for more information.
$ perl -E 'my @x = ("A", "B\n", "C\r", "D\r\n"); for (@x) { s/\R$//; s +ay ord for split //; }' 65 66 67 68
Do note that you'll need Perl 5.10 to use \R. If you have an older version of Perl, you can do this:
$ perl -E 'my @x = ("A", "B\n", "C\r", "D\r\n"); for (@x) { s/[\r\n]*$ +//; say ord for split //; }' 65 66 67 68
Finally, as this is your first post, I won't harp on it too much but, if you provide more information, you'll generally get a better answer that doesn't involve a lot of guesswork. See "How do I post a question effectively?" and "SSCCE".
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl appears to be dropping last character of line
by cmarra (Initiate) on Dec 06, 2019 at 18:34 UTC |