Phoenix.pm: Inheriting from DateTime
Douglas E. Miles
doug at phoenixinternet.com
Wed Feb 18 18:43:56 CST 2004
Hi All!
I'm trying to create a sub-class of DateTime. I want to pass a few new
attributes in the constructor. DateTime validates the parameters passed
to new(), so I can't just pass my new()s @_. I have a few ideas about
how to handle this, but none seem particularly elegant. Any thoughts on
how to approach this? Here is that code that doesn't work, just to give
you an idea:
#!/usr/bin/perl
package EventCalendar::Event;
use DateTime;
our @ISA = qw(DateTime);
sub new
{
my $package = shift;
my $class = ref($package) || $package;
my $self = $class->SUPER::new(@_);
bless($self, $class);
return $self;
} # END: new
package main;
my $event =
EventCalendar::Event->
new
(
event_name => 'Test',
year => 2004,
month => 2,
day => 1,
);
print $event->event_name . "\n";
print $event->day . "\n";
print $event->month . "\n";
print $event->year . "\n";
Here is the output:
The following parameter was passed in the call to DateTime::new but was
not listed in the validation options: event_name
at ./test line 24
Any feedback is appreciated!
More information about the Phoenix-pm
mailing list