There are two ways to use the new method. They are:
$obj2 = new CLASS ($obj1,$string,@parse_opts,\@opts);
$obj2 = $obj1->new($string,@parse_opts,\@opts)
In both cases, all arguments are optional.
Both methods are used to create a new object of a given class.
In the first case, CLASS is the class of the new object. For
example:
$date = new Date::Manip::Date;
$delta = new Date::Manip::Delta;
In the second method, the class of the new object will be
derived from the first object. For example:
$date1 = new Date::Manip::Date;
$date2 = $date1->new();
the class of the second object
($date2) is Date::Manip::Date because that is
the class of the object ($date1) used to create
it.
In both first method (when a $obj1 is
passed in) and always in the second method, the new object will share as
much information from the old object ($obj1) as
possible.
For example, if you call either of these:
$date2 = new Date::Manip::Date $date1;
$date2 = $date1->new();
the new date object will use the same embedded Date::Manip::TZ
and Date::Manip::Base objects.
When specifying CLASS and including an old object, objects do
not need to be of the same class. For example, the following are all
valid:
$date = new Date::Manip::Date $delta;
$date = new Date::Manip::Date $tz;
You can even do:
$date = new Date::Manip::Date $base;
but this will have to create a completely new Date::Manip::TZ
object, which means that optimal performance may not be achieved if a
Date::Manip::TZ object already exists.
There are two special cases. Either of the following will
create a new Date::Manip::Base object for handling multiple
configurations:
$base2 = new Date::Manip::Base $base1;
$base2 = $base1->new();
Either of the following will create a new Date::Manip::TZ
object with the same Date::Manip::Base object embedded in it:
$tz2 = new Date::Manip::TZ $tz1;
$tz2 = $tz1->new();
The new base object will initially have the same configuration
as the original base object, but changing it's configuration will not
affect the original base object.
If the "\@opts" argument is
passed in, it is a list reference containing a list suitable for passing
to the config method (described below). In this case, a new
Date::Manip::Base object (and perhaps Date::Manip::TZ object) will be
created. The new Base object will start as identical to the original one
(if a previously defined object was used to create the new object) with
the additional options in @opts added.
In other words, the following are equivalent:
$date = new Date::Manip::Date $obj,\@opts;
$base = $obj->base();
$base2 = $base->new();
$date = new Date::Manip::Date $base2;
$date->config(@opts);
It should be noted that the options are applied to the NEW
Date::Manip::Base object, not the old one.
An optional string ($string and parse
opts @parse_opts) may be passed in only when
creating a Date::Manip::Date, Date::Manip::Delta, or Date::Manip::Recur
object. If passed in when creating a Date::Manip::TZ or
Date::Manip::Base object, a warning will be issued, but execution will
continue.
If the string is included, it will be parsed to give an
initial value to the object. This will only be done AFTER any options
are handled, so the following are equivalent:
$date = new Date::Manip::Date $string,@parse_opts,\@opts;
$date = new Date::Manip::Date;
$date->config(@opts);
$date->parse($string,@parse_opts);
Once a Date::Manip::Date object (or any object in any other
Date::Manip class) is created, it should always be used to create
additional objects in order to preserve cached data for optimal
performance and memory usage.
The one caveat is if you are working with multiple
configurations as described in the Date::Manip::Objects document. In
that case, you may need to create completely new objects to allow
multiple Date::Manip::Base objects to be used.