By the way, I thought I would point here use re 'debug'. You can use it to see how Perl compiles and matches your regular expressions:
$ perl -Mre=debugcolor -e '/abcd^$/'
Compiling REx "abcd^$"
Final program:
1: EXACT <abcd> (3)
3: BOL (4)
4: EOL (5)
5: END (0)
anchored "abcd"$ at 0 (checking anchored) minlen 4
Freeing REx: "abcd^$"
With m:
$ perl -Mre=debugcolor -e '/abcd^$/m'
Compiling REx "abcd^$"
Final program:
1: EXACT <abcd> (3)
3: MBOL (4)
4: MEOL (5)
5: END (0)
anchored "abcd"$ at 0 (checking anchored) minlen 4
Freeing REx: "abcd^$"
You can also try some example data and make sure that nothing matches:
$ perl -Mre=debugcolor -e '"not going to match" =~ /abcd^$/m'
Compiling REx "abcd^$"
Final program:
1: EXACT <abcd> (3)
3: MBOL (4)
4: MEOL (5)
5: END (0)
anchored "abcd"$ at 0 (checking anchored) minlen 4
Guessing start of match in sv for REx "abcd^$" against "not going to match"
Did not find anchored substr "abcd"$...
Match rejected by optimizer
Freeing REx: "abcd^$"
Here the match is not executed twice:
$ perl -Mre=debug -e '"abcd\nabcd\n\n" =~ /abcd^$/m'
...
anchored "abcd"$ at 0 (checking anchored) minlen 4
Guessing start of match in sv for REx "abcd^$" against "abcd%nabcd%n%n"
Found anchored substr "abcd"$ at offset 0...
Guessed: match at offset 0
Matching REx "abcd^$" against "abcd%nabcd%n%n"
0 <> <abcd%nabcd> | 1:EXACT <abcd>(3)
4 <abcd> <%nabcd%n%n> | 3:MBOL(4)
failed...
5 <abcd%n> <abcd%n%n> | 1:EXACT <abcd>(3)
9 <abcd%nabcd> <%n%n> | 3:MBOL(4)
failed...
Match failed
Freeing REx: "abcd^$"
, ,
debugcolor.
.