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

I'm trying to format some output and it works fine unless my entries have embedded escape characters. Some of my variables have escaped color sequences and when output using the defined FORMAT, the escapes get munged. How can I properly capture/output the sequences with the format I've specified -- or is there a better way (without installing any modules) to format text output?

code:
format OUT = @<<<< @<<<<<<<<<<<<<<<<<< @<<<<<<< + @<<< $Status, $element, $ms, $count . $~="OUT"; open(OUT,">>tmp"); $Status="\033[32m$Status"; $element="\033[36m$element"; write OUT;

This is the output: [32m [36mtyr [0m 1.24ms

Replies are listed 'Best First'.
Re: Escape characters with FORMAT?
by hossman (Prior) on Feb 10, 2008 at 04:47 UTC

    You have to read all the way to the last paragraph of perlform

    Within strings that are to be displayed in a fixed length text field, each control character is substituted by a space. (But remember the special meaning of "\r" when using fill mode.) This is done to avoid misalignment when control characters "disappear" on some output media.

    It works just fine with a variable length field, but that's probably not very useful for you...

    #!/usr/bin/perl use strict; use warnings; my $Status="\033[32mSTATUS\033[0m"; my $ms = 6; my $count = 42; format STDOUT = ^<<<<< ^<<<<< @* $ms, $count, $Status . write STDOUT;

    My advice: If you are dealing with a single line format, just use sprintf instead.

    UPDATE: Actualy your "data" probably doesn't have control characters in it right? you're just trying to shove control characters into it -- what you relaly want is for your format to have control characters in it. While that's not possible, you can use variable width fields that you know will always get filled with 0 length values (which just happen to be control characters...

    #!/usr/bin/perl use strict; use warnings; my $Status="STATUS"; my $ms = 6; my $count = 42; my $START_COLOR = "\033[32m"; my $CHANGE_COLOR = "\033[36m"; my $END_COLOR = "\033[0m"; format STDOUT = ^<<<<< @* ^<<<<<< @* ^<<<<<<<<<<<<< @* $ms, $START_COLOR, $count, $CHANGE_COLOR, $Status, $END_COLOR . write STDOUT;
Re: Escape characters with FORMAT?
by GrandFather (Saint) on Feb 10, 2008 at 04:25 UTC

    The code you provide doesn't (can't) generate the output you suggest it does - at the very least the width of the last field doesn't allow the text '1.24ms' to be generated.

    I'd use printf instead of format. Maybe something like:

    use strict; use warnings; my $status = 'frst'; my $element = 'tyr'; my $Status = "\033[32m$status"; my $Element = "\033[36m$element"; my $ms = '0ms'; my $count = '1.24ms'; my $format = " %-4s %-18s %-7s %-3 +s\n"; printf $format, $Status, $element, $ms, $count;

    Prints:

    frst tyr 0ms + 1.24ms

    Note that there is an escape character before the first [


    Perl is environmentally friendly - it saves trees
      Thanks, printf works perfectly.
Re: Escape characters with FORMAT?
by shmem (Chancellor) on Feb 10, 2008 at 09:17 UTC
    Put the escapes into your format
    format OUT = ^[@<<<< ^[@<<<<<<<<<<<<<<<<<< ^[@<<<<<<< +@<<<< $Status, $element, $ms, $count . $Status="[32m$Status"; ...

    where ^[ is a literal chr(33).

    update: corrected format declaration

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}