Paste #126991

   
pasted on 26.09.2019 13:51
  • Edit to this paste
  • Print
  • Raw
  • Compare with paste
    #  
  • Toggle line numbers
  • Syntax highlighting  
Text paste
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#####################################
package Model::Base;
use Mojo::Base -base;

sub bar {
  my $self = shift;
  return {"id"=>shift};
}

1;
#####################################
package Model::Test;
use Mojo::Base 'Model::Base';

sub test1 {
  my ($self, $id, $req_id) = @_;
  my $s = $self->foo($id)
    if $id;
  # вот эта строчка запускается только один раз
  # а нужно для каждого запроса без $id
  $s ||= $self->bar($req_id); 
  return $s;
}

1;
################################
package main;
use Mojo::Base 'Mojolicious';
use Mojo::Util qw(dumper);

has model => sub { Model::Test->new };

sub startup {
  my $app = shift;
  $app->secrets(['My secret pa$$phrase']);
  my $r = $app->routes;
  $r->get('/' => sub {
    my $c = shift;
    my $r = $c->app->model->test1($c->session->{foo}, $c->req->request_id);
    $c->app->log->debug(dumper $r);
    $c->render(text => $r->{id});
  });
}

__PACKAGE__->new->start;
Add Comment
Author