NAME
Devel::Ladybug::Class - Root-level "Class" class
SYNOPSIS
Class Allocation
#
# File: lib/Devel/Ladybug/Example.pm
#
use strict;
use warnings;
use Devel::Ladybug qw| :all |;
create "YourApp::Example" => {
#
# This is an empty class prototype
#
};
Class Consumer
#
# File: testscript.pl
#
use strict;
use warnings;
use YourApp::Example;
my $exa = YourApp::Example->new();
$exa->setName("Hello World");
# This would also work:
# $exa->set("name", "Hello World");
#
# or, just:
# my $exa = YourApp::Example->new(name=>"Hello World");
$exa->save();
$exa->print();
DESCRIPTION
Devel::Ladybug::Class is the root-level parent class in Devel::Ladybug, and also provides the class prototyping function create()
.
METHODS
Public Class Methods
get(Devel::Ladybug::Class $class: Str $key)
Get the named class variable
my $class = "YourApp::Example"; my $scalar = $class->get($key); my @array = $class->get($key); my %hash = $class->get($key);
set(Devel::Ladybug::Class $class: Str $key, *@value)
Set the named class variable to the received value
my $class = "YourApp::Example"; $class->set($key, $scalar); $class->set($key, @array); $class->set($key, %hash);
pretty(Devel::Ladybug::Class $class: Str $key)
Transform camelCase to Not Camel Case
my $class = "YourApp::Example"; my $uglyStr = "betterGetThatLookedAt"; my $prettyStr = $class->pretty($uglyStr);
members(Devel::Ladybug::Class $class:)
Class introspection method.
Return an array ref of all messages supported by this class.
Does not include messages from superclasses.
my $members = YourApp::Example->members();
membersHash(Devel::Ladybug::Class $class:)
Class introspection method.
Return a hash ref of all messages supported by this class.
Does not include messages from superclasses.
my $membersHash = YourApp::Example->membersHash();
Private Class Methods
init(Devel::Ladybug::Class $class:)
Abstract callback method invoked immediately after a new class is allocated via create().
Override in subclass with additional logic, if necessary.
__checkVarName(Devel::Ladybug::Class $class: Str $varName)
Checks the "safeness" of a class variable name.
PROTOTYPE COMPONENTS
Class (Package) Name
The name of the class being created is the first argument sent to create()
.
use Devel::Ladybug qw| :all |;
#
# The class name will be "YourApp::Example":
#
create "YourApp::Example" => {
};
Class Prototype
A class prototype is a hash describing all fundamental characteristics of an object class. It's the second argument sent to create()
.
create "YourApp::Example" => {
#
# This is an empty prototype (perfectly valid)
#
};
Instance Variables
Instance variables are declared with the assert
class method:
create "YourApp::Example" => {
favoriteNumber => Devel::Ladybug::Int->assert()
};
The allowed values for a given instance variable may be specified as arguments to the assert
method.
Instance variables may be augmented with subtyping rules using the subtype
function, which is also sent as an argument to assert
. See Devel::Ladybug::Subtype for a list of allowed subtype arguments.
create "YourApp::Example" => {
favoriteColor => Devel::Ladybug::Str->assert(
qw| red green blue |,
subtype(
optional => true
)
),
};
Instance Methods
Instance methods are declared as keys in the class prototype. The name of the method is the key, and its value in the prototype is a Perl 5 sub{}
.
create "YourApp::Example" => {
#
# Add a public instance method, $self->handleFoo()
#
handleFoo => sub {
my $self = shift;
printf 'The value of foo is %s', $self->foo();
print "\n";
return true;
}
}
my $exa = YourApp::Example->new();
$exa->setFoo("Bar");
$exa->handleFoo();
#
# Expected output:
#
# The value of foo is Bar
#
The Devel::Ladybug convention for private or protected instance methods is to prefix them with a single underscore.
create "YourApp::Example" => {
#
# private instance method
#
_handleFoo => sub {
my $self = shift;
say "The value of foo is $self->{foo}";
}
};
Class Variables
Class variables are declared as keys in the class prototype. They should be prepended with double underscores (__). The value in the prototype is the literal value to be used for the class variable.
use Devel::Ladybug qw| :all |;
create "YourApp::Example" => {
#
# Override a few class variables
#
__useYaml => false,
__dbiType => Devel::Ladybug::DBIType::MySQL
};
Devel::Ladybug class variables are just Perl package variables, scoped in list context.
Class Methods
Class methods are declared in the same manner as instance methods. The only difference is that the class will be the receiver.
create "YourApp::Example" => {
#
# Add a public class method
#
loadXml => sub {
my $class = shift;
my $xml = shift;
# ...
}
};
The Devel::Ladybug convention for private or protected class methods is to prefix them with double underscores.
create "YourApp::Example" => {
#
# Override a private class method
#
__basePath => sub {
my $class = shift;
return join('/', '/tmp', $class);
}
};
Inheritance
By default, classes created with create()
inherit from Devel::Ladybug::Node. To override this, include a __BASE__
attribute, specifying the parent class name.
create "YourApp::Example" => {
#
# Override parent class
#
__BASE__ => "Acme::CustomClass"
};
OPTIONAL EXPORTS
Constants
true
,false
Constants provided by Devel::Ladybug::Enum::Bool
Functions
create(Str $class: Hash $prototype)
Allocate a new Devel::Ladybug-derived class.
Objects instantiated from classes allocated with
create()
have built-in runtime assertions-- simple but powerful rules in the class prototype which define runtime and schema attributes. See the Devel::Ladybug::Type module for more about assertions.Devel::Ladybug classes are regular old Perl packages.
create()
is just a wrapper to thepackage
keyword, with some shortcuts thrown in.use Devel::Ladybug qw| :all |; create "YourApp::Example" => { __someClassVar => true, someInstanceVar => Devel::Ladybug::Str->assert(), anotherInstanceVar => Devel::Ladybug::Str->assert(), publicInstanceMethod => sub { my $self = shift; # ... }, _privateInstanceMethod => sub { my $self = shift; # ... }, publicClassMethod => sub { my $class = shift; # ... }, __privateClassMethod => sub { my $class = shift; # ... }, };
SEE ALSO
Devel::Ladybug::Type, Devel::Ladybug::Subtype
This file is part of Devel::Ladybug.