2013-03-19 52 views
1

我正試圖返回今天的生日。這就是我現在所擁有的,這是有效的,但我需要抓住月份和日子來輸入聲明。我想也許我可以從當地時間抓住他們,但那並沒有奏效。任何建議,將不勝感激。DBIx :: Class和搜索

sub author_birth { 
    my ($self) = @_; 
    my ($day,$month) = (localtime())[3..4]; 
    my $author_result = $self->search_like(
     { 
      birth => '%03-20' 
     }, 
     { 
      select => [ 
       'id', 
       'complete_name', 
      ], 

      #result_class => 'DBIx::Class::ResultClass::HashRefInflator' 
     } 
    ); 

    my @author_ids =(); 
    while (my $row = $author_result->next) { 
     push @author_ids, $row->id; 
    } 

    return $self->get_author_info_by_id(\@author_ids); 

} 
+0

它不適用於'$ day'和'$ month',因爲它們不包含數據庫中日期包含的前導零。最好的解決方案(你似乎已經制定出來)是使用Time :: Piece。 – 2013-03-20 09:59:42

+0

感謝您的解釋,直到昨天我才意識到Time :: Piece。乾杯。 – 2013-03-20 16:09:57

回答

2

我最終做了這樣的事情。

my ($self) = @_; 
my $conc = '%'; 
my $datetime = Time::Piece->new->strftime('%m-%d'); 
my $date = $conc . $datetime; 
my $author_result = $self->search_like(
    { 
     birth => $date, 
    }, 
    { 
     select => [ 
      'id', 
      'complete_name', 
     ], 

     #result_class => 'DBIx::Class::ResultClass::HashRefInflator' 
    } 
);