[Tex/LaTex] How to get lines to break automatically using the LaTeX listings package

listings

I am using the listings package to include sample Perl source code in LaTeX. I included the package with \usepackage{listings}, and used the following code:

\lstset{language=Perl}
\lstset{commentstyle=\textit}
\lstset{frame=shadowbox}
\begin{lstlisting}[numbers=left]
$subject = "<owamp:subject xmlns:owamp=\"http://ggf.org/ns/nmwg/tools/owamp/2.0/\" id=\"subject\">\n";
\end{lstlisting}

The resulting output is:

Screenshot of the output

The line doesn't seem to breaking, but I read that lines break automatically when using the listings package. This line is the entire PDF snapshot of the code.

Am I doing anything wrong as far as the code is concerned?

Best Answer

Use breaklines=true:

enter image description here

\documentclass{article}

\usepackage{listings} 
\lstset{language=Perl}
\lstset{commentstyle=\textit}
\lstset{frame=shadowbox}

\begin{document}
\begin{lstlisting}[numbers=left, breaklines=true]
$subject = "<owamp:subject xmlns:owamp=\"http://ggf.org/ns/nmwg/tools/owamp/2.0/\" id=\"subject\">\n";
\end{lstlisting}\end{document}