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

I'm having difficulty matching the last character in a string if it happens to be a \n. I would have naively thought m@(.)$@s would work, but instead if the last character is \n it matches the character preceding. If I explicitly use \n it will match, but I can't seem to do it conditionally. I tried using an OR, but behaves the same as using the . (dot). I tried using m flag (without g), thinking perl starts evaluation at the end of the string and I might get it that way.

Here is what I've tried:

print('String: a\nb\nc\n' . "\n"); my $text_1 = "a\nb\nc\n"; $text_1 =~ m@(.)$@s; print("----------\n>" . $1 . "<\n"); $text_1 =~ m@(\n)$@s; print("----------\n>" . $1 . "<\n"); $text_1 =~ m@(\n|.)$@s; print("----------\n>" . $1 . "<\n"); print("\n" . 'String: a\nb\nc\n - multiline match:' . "\n"); $text_1 = "a\nb\nc\n"; $text_1 =~ m@(.)$@sm; print("----------\n>" . $1 . "<\n"); $text_1 =~ m@(\n)$@sm; print("----------\n>" . $1 . "<\n"); $text_1 =~ m@(\n|.)$@sm; print("----------\n>" . $1 . "<\n");
Here's what I get:
String: a\nb\nc\n ---------- >c< ---------- > < ---------- >c< String: a\nb\nc\n - multiline match: ---------- >a< ---------- > < ---------- >a<
My searching on this issue has not come up with any answers.