Badger
view release on metacpan or search on metacpan
lib/Badger/Class/Methods.pm view on Meta::CPAN
$bus->size('large');
=head2 auto_can($class,$method)
This can be used to define a method that automatically generates other
methods on demand.
Suppose you have a view class that renders a view of a tree. In classic
I<double dispatch> style, each node in the tree calls a method against the
view object corresponding to the node's type. A C<text> node calls
C<$view-E<gt>view_text($self)>, a C<bold> node calls
C<$view-E<gt>view_bold($self)>, and so on (we're assuming that this is some
kind of document object model we're rendering, but it could apply to
anything).
Our view methods might look something like this:
sub view_text {
my ($self, $node) = @_;
print "TEXT: $node\n";
}
sub view_bold {
my ($self, $node) = @_;
print "BOLD: $node\n";
}
This can get rather repetitive and boring if you've got lots of different
node types. So instead of defining all the methods manually, you can declare
an C<auto_can> method that will create methods on demand.
use Badger::Class
auto_can => 'can_view';
lib/Badger/Debug.pm view on Meta::CPAN
#
# AUTHOR
# Andy Wardley <abw@wardley.org>
#
#========================================================================
package Badger::Debug;
use Carp;
use Badger::Rainbow
ANSI => 'bold red yellow green cyan white';
use Scalar::Util qw( blessed refaddr );
use Badger::Class
base => 'Badger::Exporter',
version => 0.01,
constants => 'PKG REFS SCALAR ARRAY HASH CODE REGEX DELIMITER',
words => 'DEBUG',
import => 'class',
constant => {
UNDEF => '<undef>',
},
lib/Badger/Debug.pm view on Meta::CPAN
# used with the 'colour' or 'color' option. It redefines the formats
# for $Badger::Base::DEBUG_FORMAT and $Badger::Exception::FORMAT
# to display in glorious ANSI technicolor.
#-----------------------------------------------------------------------
sub enable_colour {
my ($class, $target, $symbol) = @_;
$target ||= (caller())[0];
$symbol ||= 'colour';
print bold green "Enabling debug in $symbol from $target\n";
# colour the debug format
$MESSAGE = cyan($PROMPT) . yellow('%s');
$FORMAT
= cyan('[<where> line <line>]')
. "\n<msg>";
# exceptions are in red
$Badger::Exception::FORMAT
= bold red $Badger::Exception::FORMAT;
$Badger::Exception::MESSAGES->{ caller }
= yellow('<4>') . cyan(' called from ')
. yellow("<1>\n") . cyan(' in ')
. white('<2>') . cyan(' at line ')
. white('<3>');
}
lib/Badger/Rainbow.pm view on Meta::CPAN
base => 'Badger::Exporter',
constants => 'DELIMITER ARRAY REFS PKG ALL',
exports => {
any => 'ANSI_escape ANSI_colours strip_ANSI_escapes',
hooks => {
ANSI => \&_export_ANSI_colours,
},
},
constant => {
ANSI_colours => {
bold => 1,
dark => 2,
black => 30,
red => 31,
green => 32,
yellow => 33,
blue => 34,
magenta => 35,
cyan => 36,
grey => 37,
white => 38,
lib/Badger/Rainbow.pm view on Meta::CPAN
apply the correct ANSI escape codes to render text in colour on compatible
terminals.
use Badger::Rainbox ANSI => 'red green blue';
print red("This is red");
print green("This is green");
print blue("This is blue");
Available colours are: C<black>, C<red>, C<green>, C<yellow>, C<blue>,
C<magenta>, C<cyan>, C<white> and C<grey>. The C<bold> and C<dark> styles can
also be specified.
use Badger::Rainbox ANSI => 'dark bold red green blue';
print bold red "Hello World\n";
print dark blue "Hello Badger\n";
Colours and styles can be specified as a single whitespace-delimited string
or as a reference to a list of individual items.
use Badger::Rainbox ANSI => 'red green blue';
use Badger::Rainbox ANSI => ['red', 'green', 'blue'];
All ANSI colours can be loaded by specifying C<all>.
lib/Badger/Reporter.pm view on Meta::CPAN
messages => {
bad_colour => 'Invalid colour specified for %s event: %s',
};
use Badger::Debug ':dump';
use Badger::Rainbow
ANSI => 'all',
import => 'strip_ANSI_escapes';
our $COLOURS = {
bold => \&bold,
dark => \&dark,
black => \&black,
red => \&red,
green => \&green,
blue => \&blue,
cyan => \&cyan,
magenta => \&magenta,
yellow => \&yellow,
grey => \&grey,
white => \&white,
t/core/rainbow.t view on Meta::CPAN
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
#========================================================================
use strict;
use warnings;
use lib qw( t/core/lib ../t/core/lib ./lib ../lib ../../lib );
use Badger::Rainbow ANSI => 'dark bold red green blue yellow';
use Badger::Test
tests => 3,
debug => 'Badger::Rainbow',
args => \@ARGV;
my $red = red("This is red");
my $green = green("This is green");
my $blue = blue("This is blue");
( run in 0.412 second using v1.01-cache-2.11-cpan-99c4e6809bf )