2

I have this code:

my $variable = $c->forward(qw/Curate::Controller::Utils _get_timestamp/);
$c->log->debug("variable is now $variable");

And the _get_timestamp looks like this:

sub _get_timestamp :Private {

    my ( $self, $c, $arg ) = @_;

    my $current_timestamp = "0000-00-00 00:00:00";

    my $coderef = sub {
        my $sth = $c->model('DB')->storage->dbh->prepare('SELECT CURRENT_TIMESTAMP') 
            || die "Couldn't prepare statement: " .  $c->model('DB')->storage->dbh->errstr;

        $sth->execute || die "Couldn't execute statement: " . $sth->errstr;
        $current_timestamp =  $sth->fetchrow_array;
    };

    try {
        $c->model('DB')->schema->txn_do($coderef);
    } catch {
        $c->stash->{error} = "$_";  
    };

    $c->log->debug("returning $current_timestamp");

    return $current_timestamp;

}

When I run this code in the logs I can see:

      returning 2017-06-29 12:34:23
      variable is now 

I don't understand why the variable did not get assigned to the value returned by the forwarded function. This code works correctly with the old version of Catalyst. I am having this issued with the latest one downloaded from CPAN. Are you aware about any changes in forwarding protocol?

BTW: I know I can stash the result but a direct assignment seems more natural in this case.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
mnowotka
  • 16,430
  • 18
  • 88
  • 134
  • What about `$c->error`? – choroba Jun 29 '17 at 12:09
  • Nothing, there is no error as you see the last but one line of the function code prints the correct value. – mnowotka Jun 29 '17 at 12:10
  • 1
    Where's the semicolon after the 1st line? – choroba Jun 29 '17 at 12:18
  • Sorry just a typo here, not in the code. It's corrected now. – mnowotka Jun 29 '17 at 12:34
  • I guess I do. I don't really know much about this code, because it's not mine. All I know is it works correctly on the server and on my dev machine any function invoked using `forward` return nothing, regardless it it connects to DB or do something else. – mnowotka Jun 29 '17 at 12:36
  • mysql on both. As I said, I can give an example of other function, that does not connect to DB and still the variable does not get assigned when the function is invoked via forward. – mnowotka Jun 29 '17 at 12:40
  • 2
    "*I can give an example of other function, that does not connect to DB*" ... then do that. While you are at it, put together something we can run which still exhibits the problem. – Sinan Ünür Jun 29 '17 at 12:44
  • I'm sure there's a perfectly good reason for it, but this method seems an awfully long-winded way to write `DateTime->now->format_cldr("yyyy-MM-dd hh:mm:ss")` :-) – RET Jul 03 '17 at 00:22

0 Answers0