perlancar has asked for the wisdom of the Perl Monks concerning the following question:

This should be simple, but I can't seem to find it at the moment. What module (preferably core one) can be used to escape (encode) special characters that might be interpreted as markup by a POD parser, so it will be rendered as literal? Basically the equivalent of HTML::Entities's encode_entities, or String::ShellQuote's shell_quote, for POD.

For example, I have a single-line string "Compare using Perl's <=> instead of cmp" which I want to convert to: "Compare using Perl's E<lt>=E<gt> instead of cmp".
  • Comment on Escaping special characters for POD output

Replies are listed 'Best First'.
Re: Escaping special characters for POD output
by ikegami (Patriarch) on Dec 15, 2019 at 12:29 UTC

    s/</E<lt>/g will do unless you want to build tags dynamically too. (Then you also have to escape ">", and possibly "|" and "/" too.)


    Note that you probably want one of the following here:

    Compare using Perl's C<E<lt>=E<gt>> instead of C<cmp>
    or
    Compare using Perl's C<< <=> >> instead of C<< cmp >>

      These need to be escaped too:

      • = at the start of the string (or line), to avoid interpretation of command.
      • Space or tab at the start of the string (or line), to avoid verbatim interpretation.

      Was hoping for an existing routine to use, but will package this routine into a new CPAN module if there is none.