Q:

Package listings and Perl here documents with apostrophes

I sometimes write ordinary English prose in my here documents in a Perl usage sub, for example
 --[no]alias    [don't] use the alias list in the ini file (default noalias)

The problem is the a single apostrophe seems to disturb the listings package. A short example:
\documentclass[article]{memoir}
\usepackage{listings}
\lstset{language=Perl,stringstyle=\slshape}

\begin{document}
\begin{lstlisting}
my $commentA =   "Please, don't do this";
my $commentB =   "But do this";

print <<HERE;
Please, don't do this
But do this
HERE

my $commentC =   "Please, don't do this";
my $commentD =   "But do this";
\end{lstlisting}
\end{document}

This results (with MiKTEX/TeXstudio) in

The apostrophe in commentA is ignored, while the one in the here document seems to be interpreted as an opening quote.
I have tried to escape the here terminator, but to no avail. Any suggestions (short of avoiding apostrophes in here documents)?

A:

You can add a morestring for these here statements.  With the s option you can have a pair of delimiters, though this is not quite clear in the documentation:

\documentclass[article]{memoir}
\usepackage{listings}
\lstset{language=Perl,stringstyle=\slshape}

\begin{document}
\begin{lstlisting}[morestring={[s]{<<HERE;}{HERE}}]
my $commentA =   "Please, don't do this";
my $commentB =   "But do this";

print <<HERE;
Please, don't do this
But do this
HERE

my $commentC =   "Please, don't do this";
my $commentD =   "But do this";
\end{lstlisting}
\end{document}

