Skip to content

Instantly share code, notes, and snippets.

@yko

yko/testx.pl Secret

Created April 18, 2011 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yko/a3ee562cd495996b684a to your computer and use it in GitHub Desktop.
Save yko/a3ee562cd495996b684a to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use Mojolicious::Lite;
get '/' => sub {
my $self = shift;
# берется переменная из адресной строки
my $site = $self->param('site');
# 404 если нет параметра 'site'
return $self->render_not_found unless $site;
# запрашиваем страницу по адресу "http://www.google.com/search?q=site%3A$site"
# используя встроенный инстанс Mojo::UserAgent (ua) в неблокирующем режиме:
# ответ обрабатывается в калбеке
$self->ua->get(
"http://www.google.com/search?q=site%3A$site" => sub {
my ($ua, $tx) = @_;
# из полученной страницы берем содержимое блока
my $text = $tx->res->dom->at('div#resultStats');
# Элемент найден/не найден
$text = $text ? $text->text : 'Not found';
# Название шаблона совпадает с именем роута: index
$self->render(result => $text);
}
);
# откладываем генерацию страницы: пока google думает,
# наш сервер обработает запросы от других пользователей
$self->render_later;
} => 'index';
app->start;
# ниже в этом же файле объявляем шаблон index.html.ep
__DATA__
@@ index.html.ep
<%= $result %><br />
<form action="/cgi-bin/get_string.pl">
site name: <input type="text" name="site" value="<%= param('site') %>"/>
<input type="submit" />
</form>
</code>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment