ターミナルで”Google”をカラー表示
まず、ANSIColor.pmを作る。
package ANSIColor; use strict; use warnings; use base 'Exporter'; use constant { # foreground color BLACK => "\033[30m", RED => "\033[31m", GREEN => "\033[32m", YELLOW => "\033[33m", BLUE => "\033[34m", PURPLE => "\033[35m", CYAN => "\033[36m", WHITE => "\033[37m", # background color BLACKB => "\033[40m", REDB => "\033[41m", GREENB => "\033[42m", YELLOWB => "\033[43m", BLUEB => "\033[44m", PURPLEB => "\033[45m", CYANB => "\033[46m", WHITEB => "\033[47m", # bold B => "\033[1m", BOFF => "\033[22m", # italics I => "\033[3m", IOFF => "\033[23m", # underline U => "\033[4m", UOFF => "\033[24m", # invert R => "\033[7m", ROFF => "\033[27m", # reset RESET => "\033[0m", }; our %EXPORT_TAGS = ('foreground' => [qw(BLACK RED GREEN YELLOW BLUE PURPLE CYAN WHITE RESET)], 'background' => [qw(BLACKB REDB GREENB YELLOWB BLUEB PURPLEB CYANB WHITEB RESET)], 'italic' => [qw(I ITON ITALIC ITALICON IOFF ITALICOFF RESET)], 'bold' => [qw(B BOLD BOLDON BOLDOFF BOFF RESET)], 'invert' => [qw(R INVON ROFF INVOFF)], 'underline' => [qw(U UL ULON UNDERLINE UNDERLINEON UOFF UNDERLINEOFF ULOFF)], ); $EXPORT_TAGS{'all'} = [map {@{$_}} values %EXPORT_TAGS]; our @EXPORT_OK = @{$EXPORT_TAGS{'all'}}; 1;
あとは、以下を実行
>perl -e 'use ANSIColor qw(:foreground); print BLUE,G,RED,o,YELLOW,o,BLUE,g,GREEN,l,RED,e,RESET,"\n"' Google
ターミナルがカラフルだと楽しくなります。
正規表現で使いたい場合は、eオプションを付けて、
use ANSIColor qw(:foreground); # Wikipediaのgoogle項目冒頭 (http://en.wikipedia.org/wiki/Google) my $str =<<_EOS_; Google Inc. is an American public corporation, earning revenue from advertising related to its Internet search, e-mail, online mapping, office productivity, social networking, and video sharing services as well as selling advertising-free versions of the same technologies. The Google headquarters, the Googleplex, is located in Mountain View, California. As of March 31, 2009, the company has 20,164 full-time employees. _EOS_ $str =~ s/Google/BLUE.G.RED.o.YELLOW.o.BLUE.g.GREEN.l.RED.e.RESET/eg; print $str;
結果は、
Google Inc. is an American public corporation, earning revenue from advertising related to its Internet search, e-mail, online mapping, office productivity, social networking, and video sharing services as well as selling advertising-free versions of the same technologies. The Google headquarters, the Googleplex, is located in Mountain View, California. As of March 31, 2009, the company has 20,164 full-time employees.