in reply to hashref return fails
from the package where foo is declared. Do you use DateTime in the package?use DateTime;
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; { package MyClass; use DateTime; sub foo { my ($self,$input) = @_; my @ymd = split("-",$input); my $date = { day => $ymd[2], month => $ymd[1], year => $ymd[0], }; my $dt = eval{DateTime->new($date)}; return unless $dt; $date->{dt} = $dt; return $date } sub new { bless {}, shift } } my $dt = 'MyClass'->new; my $result = $dt->foo('2001-01-01'); print Dumper $result;
Output:
$VAR1 = { 'dt' => bless( { 'utc_rd_days' => 730486, 'rd_nanosecs' => 0, 'local_rd_days' => 730486, 'utc_rd_secs' => 0, 'locale' => bless( { 'native_complete_name' + => 'English United States', 'en_territory' => 'Uni +ted States', 'en_language' => 'Engl +ish', 'en_complete_name' => +'English United States', 'native_territory' => +'United States', 'default_time_format_l +ength' => 'medium', 'default_date_format_l +ength' => 'medium', 'id' => 'en_US', 'native_language' => ' +English' }, 'DateTime::Locale::en +_US' ), 'utc_year' => 2002, 'formatter' => undef, 'offset_modifier' => 0, 'local_c' => { 'day_of_quarter' => 1, 'day' => 1, 'day_of_year' => 1, 'hour' => 0, 'second' => 0, 'quarter' => 1, 'minute' => 0, 'year' => 2001, 'month' => 1, 'day_of_week' => 1 }, 'local_rd_secs' => 0, 'tz' => bless( { 'offset' => 0, 'name' => 'floating' }, 'DateTime::TimeZone::Floa +ting' ) }, 'DateTime' ), 'month' => '01', 'day' => '01', 'year' => '2001' };
|
|---|