|
Posted by tv on February 7, 2005, 9:33 pm
Please log in for more thread options
I've read about Class::MethodMaker in Damian Conway's 'Object Oriented
Perl'. It uses v1 syntax. The latest v2 documentation is extremely
minimal, not to mention full of typos. I've googled everywhere looking
for v2 examples especially for -init and -default behavior examples,
but no luck.
Could a kind soul help me by converting Damian's v1 example to v2
syntax? It follows below.
Thanks,
-Tom
package CD::Music;
$VERSION = 4.00;
use strict;
use Class::MethodMaker
new_with_init => 'new',
new_hash_init => '_init_args',
static_hash => 'counter',
get_set => [ qw( name artist publisher ISBN tracks rating room
shelf ) ] ;
sub count { $_[0]->counter('count') }
sub _incr_count { $_[0]->counter_tally('count') }
sub _decr_count { $_[0]->counter(count=>$_[0]->counter('count')-1) }
sub init
{
my ($self, %args) = @_;
$self->_init_args(%args);
$self->_incr_count();
return $self;
}
# destructor adjusts object count
sub DESTROY { $_[0]->_decr_count() }
# get or set room and shelf together
sub location
{
my ($self, $room, $shelf) = @_;
$self->room($room) if $room;
$self->shelf($shelf) if $shelf;
return ($self->room, $self->shelf)
}
package main;
# Create an object storing a CD's details
my $cd = CD::Music->new(
name => "Piano Concerto 20",
artist => "Mozart",
rating => 10,
room => 5,
shelf => 1,
publisher => "Salieri Intl.",
ISBN => "1426-43235624-2",
);
# What's the CD called?
print $cd->name, "n";
print $cd->artist, "n";
print $cd->rating, "n";
print $cd->room, "n";
# Where would we find it?
printf "Room %s, shelf %sn", $cd->location;
# Move it to room 5, shelf 3
$cd->location(5,3);
printf "Room %s, shelf %sn", $cd->location;
$cd->name("Tacobell Cannon");
$cd->tracks(1000);
print $cd->name, "n";
print $cd->tracks, "n";
print "There have been ", CD::Music->count(), " CDs createdn";
|
| Similar Threads | Posted | | Feedback requested on alternate interface to Time::Period | March 8, 2007, 3:19 pm |
| Help with XML::Twig xpath syntax, please | October 16, 2005, 6:56 pm |
| Net::SFTP ssh_args=>[ ] syntax question... | August 19, 2004, 9:48 am |
| Pod syntax and hyperlinks to =item entries | December 8, 2005, 8:37 pm |
| Email address syntax check? | December 2, 2006, 2:58 pm |
| Syntax error using cpan leads to bizarre behavior | September 10, 2004, 6:32 pm |
| Class::DBI::Schema2Code V 1.01 | July 29, 2004, 7:14 am |
| Class::Tree V 1.24 | July 29, 2004, 7:15 am |
| [RFC] Class::DataStore | May 13, 2005, 2:45 pm |
| RFC: new module Class::MakeIntrospecMethods | October 18, 2004, 4:30 pm |
|