Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help: Characters with colors

1 view
Skip to first unread message

Amy Lee

unread,
Aug 20, 2008, 9:00:14 AM8/20/08
to
Hello,

How to modify the color of characters when I display them?

Thank you very much~

Regards,

Amy

magloca

unread,
Aug 20, 2008, 9:16:36 AM8/20/08
to
Amy Lee @ Wednesday 20 August 2008 15:00:

In a terminal? Term::ANSIColor. Somewhere else? You'll have to be more
specific.

m.

Amy Lee

unread,
Aug 20, 2008, 9:49:44 AM8/20/08
to

Yes, thank you very much.

Amy

Amy Lee

unread,
Aug 20, 2008, 10:25:30 AM8/20/08
to

Anyway, there's something I feel a little confused. I find that this
module is for 'print', when I use it on 'die' function it's still working
but will output a line like this:

at ./hwinfo.pl line 17, <KER_VER> line 1.

My codes:

use strict;
use warnings;
use Term::ANSIColor qw(:constants);
$Term::ANSIColor::AUTORESET = 1;

open KER_VER, "<", "/root/version";
while (<KER_VER>)
{
unless (/\s+2.4.\d+/)
{
die BOLD RED "Your kernel version is not 2.4.x.\n"
}
}


I hope clean this internal output by perl, how could I do that?

Thank you very much~

Amy

Lars Eighner

unread,
Aug 20, 2008, 10:40:19 AM8/20/08
to
In our last episode, <pan.2008.08.20....@gmail.com>, the lovely
and talented Amy Lee broadcast on comp.lang.perl.misc:

> open KER_VER, "<", "/root/version";
> while (<KER_VER>)
> {
> unless (/\s+2.4.\d+/)
> {
> die BOLD RED "Your kernel version is not 2.4.x.\n"
> }
> }


> I hope clean this internal output by perl, how could I do that?

> Thank you very much~

You can write ansi codes directly, but in spite of ansi being
more or less a standard, there are serious portability problems.
Some terminals are not ansi capable and others may be quirky.

--
Lars Eighner <http://larseighner.com/> use...@larseighner.com
"I believe in God and I believe in free markets,"
-Kenneth Lay, CEO for the now defunct Enron, whose loss of some 50
billion dollars represents the largest corporate bankruptcy in the
history of the US.

Jürgen Exner

unread,
Aug 20, 2008, 10:40:47 AM8/20/08
to
Amy Lee <openlin...@gmail.com> wrote:
>How to modify the color of characters when I display them?

That depends _VERY_ on much how and where you display them. "perldoc -q
color"
How do I print something out in color?
may help get you started.

jue

Sherm Pendley

unread,
Aug 20, 2008, 10:51:17 AM8/20/08
to
Amy Lee <openlin...@gmail.com> writes:

> How to modify the color of characters when I display them?

It depends on how you're displaying them. Are you writing a web app, a
GUI app, an app that runs in a terminal window, etc.?

sherm--

--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net

Amy Lee

unread,
Aug 20, 2008, 10:51:32 AM8/20/08
to

Thank you very much. Anyway, when I want to search some keywords, could I
use "perldoc -q <keyword>"?

Best Regards,

Amy

Amy Lee

unread,
Aug 20, 2008, 10:53:18 AM8/20/08
to
On Wed, 20 Aug 2008 14:40:19 +0000, Lars Eighner wrote:

> In our last episode, <pan.2008.08.20....@gmail.com>, the lovely
> and talented Amy Lee broadcast on comp.lang.perl.misc:
>
>> open KER_VER, "<", "/root/version";
>> while (<KER_VER>)
>> {
>> unless (/\s+2.4.\d+/)
>> {
>> die BOLD RED "Your kernel version is not 2.4.x.\n"
>> }
>> }
>
>
>> I hope clean this internal output by perl, how could I do that?
>
>> Thank you very much~
>
> You can write ansi codes directly, but in spite of ansi being
> more or less a standard, there are serious portability problems.
> Some terminals are not ansi capable and others may be quirky.

Thank you, but it seems that it's for 'print', not for 'die'. How do I
handle with color when I use 'die'?

Regards,

Amy

smallpond

unread,
Aug 20, 2008, 11:44:35 AM8/20/08
to

That's actually answered by:

perldoc perldoc

--S
** Posted from http://www.teranews.com **

Brian Helterlilne

unread,
Aug 20, 2008, 12:47:46 PM8/20/08
to

Read the documentation for the other interface - colored(). It returns a
string with the codes embedded.

die colored( "Your kernel version is not 2.4.x.\n", 'bold red' );

--
-brian

magloca

unread,
Aug 20, 2008, 1:01:00 PM8/20/08
to
Amy Lee @ Wednesday 20 August 2008 16:53:

How about (untested):

import Term::ANSIColor;
# ...interesting stuff happens...
die colored ("Your kernel version is not 2.4.x.", "bold red");

m.

magloca

unread,
Aug 20, 2008, 1:05:36 PM8/20/08
to
magloca @ Wednesday 20 August 2008 19:01:

> How about (untested):
>
> import Term::ANSIColor;
> # ...interesting stuff happens...
> die colored ("Your kernel version is not 2.4.x.", "bold red");

Hmm... That should be 'use Term::ANSIColor', of course... Contamination
from other languages. Sorry.

m.

Tad J McClellan

unread,
Aug 20, 2008, 7:07:56 PM8/20/08
to
Amy Lee <openlin...@gmail.com> wrote:

[ snip: asking a FAQ ]

> when I want to search some keywords, could I
> use "perldoc -q <keyword>"?


Have you seen the Posting Guidelines that are posted here frequently?

It covers "perldoc -q", along with about a dozen or so other tips on
how to get answers to Perl questions.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Amy Lee

unread,
Aug 21, 2008, 4:19:40 AM8/21/08
to

Thank you, I have solved it.

Amy

Lars Eighner

unread,
Aug 21, 2008, 7:17:16 AM8/21/08
to

Exactly the same. Again I warn you, this is not at all portable and
may or may not work on your ANSI-enabled terminal and will not work
on your ANSI-ignorant terminal:

============ tear here ===============
#!/usr/bin/perl

unless ( 1 == 0 ) { die ("\e[1;41;37m Gosh numbers still work\e[0m\n") }

============ tear here ================


The point of the module is to determine the terminal capabilities
and alter the string accordingly. In a non-ANSI terminal, the string
will be printed as you see it above. The ANSI codes do not contribute
to clarity in a non-ANSI terminal. I have no idea how or whether the
module can be made to work with die, but if your script is always going
to run in an environment you are certain of, that's how you get color.


Okay, okay, you made me read the man page. Here is the real way
to do it:

============ tear here ==========================
#!/usr/bin/perl

use Term::ANSIColor;

$boldred = color('bold red');
$reset = color('reset');

unless ( 1 == 0 ) { die ("${boldred}Gosh numbers still work$reset") }

============= tear here ==========================

Evidently you cannot set attributes on the fly with die, but you
can save them as variables, for use as needed.

War on Terrorism: The Difference Between Us and Them
"We should invade their countries, kill their leaders and
convert them to Christianity." -- Ann Coulter

Peter J. Holzer

unread,
Aug 24, 2008, 10:31:57 AM8/24/08
to
On 2008-08-20 16:47, Brian Helterlilne <brian.he...@hp.com> wrote:
> Amy Lee wrote:
>> On Wed, 20 Aug 2008 14:40:19 +0000, Lars Eighner wrote:
>>> In our last episode, <pan.2008.08.20....@gmail.com>, the lovely
>>> and talented Amy Lee broadcast on comp.lang.perl.misc:
>>>
>>>> open KER_VER, "<", "/root/version";
>>>> while (<KER_VER>)
>>>> {
>>>> unless (/\s+2.4.\d+/)
>>>> {
>>>> die BOLD RED "Your kernel version is not 2.4.x.\n"
>>>> }
>>>> }
[...]

>
> Read the documentation for the other interface - colored(). It returns a
> string with the codes embedded.
>
> die colored( "Your kernel version is not 2.4.x.\n", 'bold red' );

That has the same problem as Amys version. You may want

die colored( "Your kernel version is not 2.4.x.", 'bold red' ) . "\n";

hp

Sherm Pendley

unread,
Aug 24, 2008, 10:45:03 AM8/24/08
to

Lack of a trailing newline isn't a problem here. In fact, you get more
information (the line number of the error) if the string you pass to
die() doesn't end in a newline.

Example:

admins-macintosh:~ sherm$ perl -e 'die "Ouch!"'
Ouch! at -e line 1.
admins-macintosh:~ sherm$ perl -e 'die "Ouch!\n"'
Ouch!

Sherm Pendley

unread,
Aug 24, 2008, 11:30:27 AM8/24/08
to
Sherm Pendley <spam...@dot-app.org> writes:

> "Peter J. Holzer" <hjp-u...@hjp.at> writes:
>
>> On 2008-08-20 16:47, Brian Helterlilne <brian.he...@hp.com> wrote:
>>>
>>> die colored( "Your kernel version is not 2.4.x.\n", 'bold red' );
>>
>> That has the same problem as Amys version. You may want
>>
>> die colored( "Your kernel version is not 2.4.x.", 'bold red' ) . "\n";
>
> Lack of a trailing newline isn't a problem here. In fact, you get more
> information (the line number of the error) if the string you pass to
> die() doesn't end in a newline.
>
> Example:
>
> admins-macintosh:~ sherm$ perl -e 'die "Ouch!"'
> Ouch! at -e line 1.
> admins-macintosh:~ sherm$ perl -e 'die "Ouch!\n"'
> Ouch!

On further thought, you're right - that *was* Amy's problem. The const
subs were appending the ANSI color codes *after* the newline in the
message she used, so die() was appending information she didn't
want. Brian's version would do the same thing, with the same result.

Yours works by adding the newline *after* the color codes are added to
the message, so the newline is still the last character in the string
when its passed to die().

0 new messages