Hi folks,

Perl
===

Please advise how to add 'bold', 'italic', 'colour', etc. such features to the printout. Can HTML syntax work on perl? I tried seeming not applicable.

TIA

B.R.
satimis

Dani AI

Generated

got the right idea using Term::ANSIColor (as hinted), but the sticking point is how Perl formats treat ANSI escape sequences. Perl formats are fixed‑width templates and they treat the escape bytes from ANSI codes as ordinary characters — so escapes get counted against your picture widths and can be truncated or shift fields, which is why putting color codes "inside" a format heading often fails. See the Perl docs on formats for the details: perlform documentation.

Practical ways to fix this:

  • Print the colored heading outside the format. That is the simplest and most reliable approach: emit the ANSI start/stop codes just before calling write (or before your formatted output), then let write handle plain text. Example pattern:
use Term::ANSIColor qw(color);

print color('bold white on_magenta'), "REMARK :\n", color('reset');

# follow with your existing formats / write() calls for the body
write;
  • If the heading must appear as part of STDOUT_TOP on every page, wrap calls to write in a small routine that prints the colored top-of-page header first, then calls the real write. That keeps the ANSI sequences out of the format field calculations.

  • If embedding ANSI sequences inside formatted fields is unavoidable, use a helper that strips ANSI escapes to compute visible widths and pad strings accordingly, or use a table library that is ANSI-aware (for example, CPAN modules like Text::ANSITable or helpers on CPAN that handle ANSI stripping). Also remember older Windows consoles need an ANSI translation layer (for example Win32::Console::ANSI) for these sequences to work.

For reference, the Term::ANSIColor documentation and CPAN utilities are useful starting points: Term::ANSIColor and perlform.

Recommended Answers

All 2 Replies

What exactly are you printing?

If you're generating HTML, you can have Perl generate HTML tags. But, if you're just outputting to the console, there may be methods of using ASCII escape codes to display the desired text effects, similar to how you can do when working with the bash shell.

Hi alc6379,

Tks for your advice.

If you're generating HTML, you can have Perl generate HTML tags. But, if you're just outputting to the console, there may be methods of using ASCII escape codes to display the desired text effects, similar to how you can do when working with the bash shell.

Major problem already solved with "use Term::ANSIColor qw(:constants);"

!/usr/bin/perl -w
use strict;
use warnings;
use Term::ANSIColor qw(:constants);

print "\n";
print BOLD WHITE ON_MAGENTA "REMARK :", RESET "\n";
print RED "OLINENO=Old Line No. NLINENO=New Line No.", RESET "\n";
print RED "OWORDNO=Old Word No. NWORDNO=New Word No.", RESET "\n\n";
...
...
format   STDOUT_TOP=
       ORGINAL         MISTAKE      OLINENO    NLINENO    OWORDNO    NWORDNO
#       ORGINAL         MISTAKE      LINENO    WORDNO
.
format   STDOUT_MID=
       @<<<<<<<<<<     @<<<<<<<<<<  @##       @##       @##       @##
#       @<<<<<<<<<<     @<<<<<<<<<<  @##       @##
       $org,           $mis,        $lno1,    $lno2,    $wno1,    $wno2.

.

....

But I still could not resolve printing the heading inside "format" with colour. I tried many places adding the codes without success.

Please advise. TIA

B.R.
satimis

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.