Hi there :-)

I've just used Term::ANSIColor in an app for the first time, and found it really awesome. I didn't have to change anything at all in my code: I just declared the colors on some variables.

my $name = colored('Jon', 'red');
and almost all the rest worked as if nothing had been changed, but now my terminal outputs are much more readable.

My problem starts when some commands do string comparison:

do_something() if $name eq 'Jon';
This breaks because $name is not just Jon now, it's red Jon.

One workaraound is to use colorstrip():

do_something() if colorstrip($name) eq 'Jon';
That works, but it means I have to make this change everywhere in the code, which is a pain. Especially when these variables get sent to various objects who do all the hard work.

Is there any way to make this work? Some way of saying: "You're being compared? Wait, I'll colorstrip you first".

If that was included in the Term::ANSIColor module, that would be even better. That's why I just wrote to the module author, to come and read this message.

Any idea how to take advantage of Perl's flexibility and perform this trick?


In reply to Term::ANSIColor is awesome, but.. (need help) by mascip

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.