Fix #9465.

It turned out the sed(1) expressions are not fully portable.  So revist my
earlier attempt for getting GHC_LDFLAGS in the configure script and rewrite
it in Perl instead.