# listings wrong syntax highlighting in perl - format specification

Perl uses '#' as an introduction for a comment, but also as a format specification. This produces problems in the syntax highlighting in the listings-package. How can I fix it?

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\begin{document}
\lstset{language=perl,commentstyle=\color{red}}
\begin{lstlisting}
#this is a comment
my $n=89; #the "@###.####" is NOT a comment format OUT= @###.#####$n
.
\end{lstlisting}
\end{document}
-
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 7 at 11:07
add comment

## 1 Answer

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\begin{document}

\lstset{language=perl,morecomment=[l][keywordstyle]{@\#},commentstyle=\color{red}}
\begin{lstlisting}
#this is a comment
my $n=89; #the "@###.####" is NOT a comment format OUT= @###.#####$n
.
\end{lstlisting}
\end{document}
-
Perhaps basicstyle should be preferred to keywordstyle here. –  Jubobs Mar 7 at 11:06
add comment