OVERVIEW
Pony::Object is an object system, which provides simple way to use cute
objects.
SYNOPSIS
use Pony::Object;
DESCRIPTION
When some package uses Pony::Object, it's becomes strict (and shows
warnings) and modern (can use perl 5.10 features like as "say"). Also
"dump" function is redefined and shows data structure. It's useful for
debugging.
Specific moments
Besides new function "dump" Pony::Object has other specific moments.
has
Keyword "has" declares new fields. All fields are public. You can also
describe object methods via "has"... If you want.
package News;
use Pony::Object;
# Fields
has 'title';
has text => '';
has authors => [ qw/Alice Bob/ ];
# Methods
sub printTitle
{
my $this = shift;
say $this->title;
}
sub printAuthors
{
my $this = shift;
print @{ $this->authors };
}
1;
package main;
my $news = new News;
$news->printAuthors();
$news->title = 'Something important';
$news->printTitle();
Pony::Object fields assigned via "=". For example: $obj->field = 'a'.
new
Pony::Object doesn't have method "new". In fact, of course it has. But
"new" is an internal function, so you should not use it if you want not
have additional fun. Instead of this Pony::Object has "init" function,
where you can write the same, what you wish write in "new". "init" is
after-hook for "new".
package News;
use Pony::Object;
has title => undef;
has lower => undef;
sub init
{
my $this = shift;
$this->title = shift;
$this->lower = lc $this->title;
}
1;
package main;
my $news = new News('Big Event!');
print $news->lower;
ALL
If you wanna get all default values of Pony::Object-based class (fields,
of course), you can call "ALL" method. I don't know why you need them,
but you can do it.
package News;
use Pony::Object;
has 'title';
has text => '';
has authors => [ qw/Alice Bob/ ];
1;
package main;
my $news = new News;
print for keys %{ $news->ALL() };
toHash
Get object's data structure and return it in hash.
package News;
use Pony::Object;
has title => 'World';
has text => 'Hello';
1;
package main;
my $news = new News;
print $news->toHash()->{text};
print $news->toHash()->{title};
dump
Return string which shows object current struct.
package News;
use Pony::Object;
has title => 'World';
has text => 'Hello';
1;
package main;
my $news = new News;
$news->text = 'Hi';
print $news->dump();