[Fwd: Re: Phoenix.pm: Inheriting from DateTime]
Douglas E. Miles
doug at phoenixinternet.com
Fri Feb 20 11:30:35 CST 2004
I'm forwarding this for Tim Ayers. Majordomo says he's not a member.
Who am I to argue with Majordomo? :P Tim I'm guessing you're subscribed
under an address other than your reuters.com address. Without further
ado, Tim's message:
Given that DateTime uses "named parameters" I typically do something
like the following
sub new {
my $self = shift;
my $class = ref($self) || $self;
my %args = @_;
if (exists $args{my_arg_1}) {
# do stuff with my_arg_1
delete $args{my_arg_1};
}
if (exists $args{my_arg_2}) {
# do stuff with my_arg_2
delete $args{my_arg_2};
}
return bless $class->SUPER::new(%args), $class;
}
I like Eden's solution better for its trickery, but this is a more
explicit approach.
Hope you have a very nice day, :-)
tim
Douglas E. Miles wrote:
> 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!
>
-----------------------------------------------------------------
Visit our Internet site at http://www.reuters.com
Get closer to the financial markets with Reuters Messaging - for more
information and to register, visit http://www.reuters.com/messaging
Any views expressed in this message are those of the individual
sender, except where the sender specifically states them to be
the views of Reuters Ltd.
More information about the Phoenix-pm
mailing list