How to modify the color of characters when I display them?
Thank you very much~
Regards,
Amy
In a terminal? Term::ANSIColor. Somewhere else? You'll have to be more
specific.
m.
Yes, thank you very much.
Amy
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
> 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.
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
> 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
Thank you very much. Anyway, when I want to search some keywords, could I
use "perldoc -q <keyword>"?
Best Regards,
Amy
> 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
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
How about (untested):
import Term::ANSIColor;
# ...interesting stuff happens...
die colored ("Your kernel version is not 2.4.x.", "bold red");
m.
> 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.
[ 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/"
Thank you, I have solved it.
Amy
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.
--
Lars Eighner <http://larseighner.com/> use...@larseighner.com
War on Terrorism: The Difference Between Us and Them
"We should invade their countries, kill their leaders and
convert them to Christianity." -- Ann Coulter
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
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!
> "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().