Print::Colored - print, say, prompt with predefined colors
use Print::Colored; use Print::Colored ':all'; # color use Print::Colored ':color'; $colored_text = color_error $text; # bright red $colored_text = color_info $text; # bright blue $colored_text = color_input $text; # bright cyan $colored_text = color_ok $text; # bright green $colored_text = color_warn $text; # bright magenta # print use Print::Colored ':print'; print_error $text; print_info $text; print_input $text; print_ok $text; print_warn $text; # prompt use Print::Colored ':prompt'; $input = prompt_error $text, @params; $input = prompt_info $text, @params; $input = prompt_input $text, @params; $input = prompt_ok $text, @params; $input = prompt_warn $text, @params; $password = password_error $text, @params; $password = password_info $text, @params; $password = password_input $text, @params; $password = password_ok $text, @params; $password = password_warn $text, @params; # say use Print::Colored ':say'; say_error $text; say_info $text; say_input $text; say_ok $text; say_warn $text;
Print::Colored provides functions to print, say, prompt with predefined colors.
error
info
input
ok
warn
We should use colors all the time we write sripts that run in the terminal. Read Use terminal colors to distinguish information by brian d foy to get some more ideas about it.
But experience shows that the more commands and constants we have to use the less colors our scripts have. This was the reason to build this rather simple module.
Because the colors are predefined, there isn't much to configure. If you don't like them (and quite sure you don't) and until we come up with a better solution, you can use "coloralias" in Term::ANSIColor to modify them.
use Term::ANSIColor 'coloralias'; coloralias('error', 'yellow'); # default: bright_red coloralias('info', 'white'); # default: bright_blue coloralias('input', 'bright_white'); # default: bright_cyan coloralias('ok', 'black'); # default: bright_green coloralias('warn', 'red'); # default: bright_blue
All the commands except "color_" write directly to STDOUT.
STDOUT
print_ok $filehandle 'Everything okay.'; # ✗ no say_ok $filehandle 'Everything okay.'; # ✗ no
You can't "print" and "say" to filehandles.
print $filehandle color_ok 'Everything okay.'; # ✓ say $filehandle color_ok 'Everything okay.'; # ✓
Instead you have to use one of the "color" functions.
use Print::Colored ':color';
Imports the functions "color_error", "color_info", "color_input", "color_ok", and "color_warn".
$colored_text = color_error 'There was an error';
Returns a text colored as error.
$colored_text = color_info 'This is an info';
Returns a text colored as info.
$colored_text = color_input 'Waiting for an input...';
Returns a text colored as input.
$colored_text = color_ok 'Everything okay';
Returns a text colored as ok.
$colored_text = color_warn 'Last warning';
Returns a text colored as warn.
use Print::Colored ':print';
Imports the functions "print_error", "print_info", "print_input", "print_ok", and "print_warn".
print_error 'There was an error';
Prints a text colored as error.
print_info 'This is an info';
Prints a text colored as info.
print_input 'Waiting for an input...';
Prints a text colored as input.
print_ok 'Everything okay';
Prints a text colored as ok.
print_warn 'Last warning';
Prints a text colored as warn.
use Print::Colored ':prompt';
Imports the functions "prompt_error", "prompt_info", "prompt_input", "prompt_ok", "prompt_warn", "password_error", "password_info", "password_input", "password_ok", and "password_warn". Internally they call "prompt" in IO::Prompter.
$password = prompt_input 'Enter your password: ', -echo => '*'; $password = password_input 'Enter your password: ';
password functions ask for a password and are identical to prompt with parameter <-echo = '*'>>.
password
prompt
<-echo =
$input = prompt_error 'Enter your data: ';
Prompts colored as error and returns the input.
$input = prompt_info 'Enter your data: ';
Prompts colored as info and returns the input.
$input = prompt_input 'Enter your data: ';
Prompts colored as input and returns the input.
$input = prompt_ok 'Enter your data: ';
Prompts colored as ok and returns the input.
$input = prompt_warn 'Enter your data: ';
Prompts colored as warn and returns the input.
$password = password_error 'Enter your password: ';
Prompts colored as error for a password and returns the input.
$password = password_info 'Enter your password: ';
Prompts colored as info for a password and returns the input.
$password = password_input 'Enter your password: ';
Prompts colored as input for a password and returns the input.
$password = password_ok 'Enter your password: ';
Prompts colored as ok for a password and returns the input.
$password = password_warn 'Enter your password: ';
Prompts colored as warn for a password and returns the input.
use Print::Colored ':say';
Imports the functions "say_error", "say_info", "say_input", "say_ok", and "say_warn".
say_error 'There was an error';
Prints a text with appended newline colored as error.
say_info 'This is an info';
Prints a text with appended newline colored as info.
say_input 'Waiting for an input...';
Prints a text with appended newline colored as input.
say_ok 'Everything okay';
Prints a text with appended newline colored as ok.
say_warn 'Last warning';
Prints a text with appended newline colored as warn.
© 2019-2020 by Tekki (Rolf Stöckli).
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
IO::Prompter, Term::ANSIColor.
To install Print::Colored, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Print::Colored
CPAN shell
perl -MCPAN -e shell install Print::Colored
For more information on module installation, please visit the detailed CPAN module installation guide.