Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have class with a method ( non moose, just blessed ), that fills a hash, basically so:
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; print "date: ".dump($date)."\n"; return $date; }
and call it so in another package:
my $result = $date_thingy->foo("2000-01-01");

Amazingly, dump outputs all as expected ( hash with year,month,day set to the expected values, and datetime with expected values ). But $result is undef in the calling function. How could that possibly happen?

$date_thingy exists btw, after and before the call.

using perl 5.18.2

This is driving me insane, and no, there is absolutely no further line between print and return, and removing the print changes nothing.

Replies are listed 'Best First'.
Re: hashref return fails
by choroba (Cardinal) on May 27, 2015 at 21:53 UTC
    I can reproduce a similar behaviour if I remove
    use DateTime;
    from the package where foo is declared. Do you use DateTime in the package?
    #!/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' };
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: hashref return fails
by Anonymous Monk on May 27, 2015 at 21:59 UTC

    Given only your code and your description that the print statement works, that does seem quite strange. Have you stepped through the code in the debugger? Can you reduce your code down to an example that reproduces the problem that you can post here for others to run?

    (By the way, DateTime::Format::Strptime can handle what your sub foo is doing.)

      return unless $dt;

      Exactly what are you returning here unless $dt is true?

      -- FloydATC

      Time flies when you don't know what you're doing

        But look at where the print statement is which the OP claims works...
Re: hashref return fails
by Anonymous Monk on May 28, 2015 at 15:25 UTC

    What is the class of $date_thingy and in which class is sub foo defined? Does the class in which sub foo is defined have a subclass that overrides sub foo? Are there any other things in your code that share the name foo?