=encoding utf8 =head1 NAME =begin original Mojolicious::Guides::Tutorial - Get started with Mojolicious =end original Mojolicious::Guides::Tutorial - Mojoliciousをはじめよう =head1 TUTORIAL (説明) =begin original A quick example-driven introduction to the wonders of L. Almost everything you'll learn here also applies to full L applications. =end original Lの特徴を簡単なサンプルで紹介していきます。 ここで学んだことは、ほとんどすべてがフルバージョンのLアプリケーションにも当てはまります。 =begin original This is only the first of the L. Other guides delve deeper into topics like L a L prototype into a well-structured L application, L, L and more. It is highly encouraged that readers continue on to the remaining guides after reading this one. =end original チュートリアルはLのはじめの章です。その他のガイドでは、各トピックについて深く掘り下げます。たとえば、Lのプロトタイプをしっかり構築したL アプリケーションに育てるためのL、またはL、 L、その他にもたくさんあります。これを読み終わった後は、残りのガイドを読むことををおすすめします。 =head2 Hello World =begin original A simple Hello World application can look like this, L, L, L and Perl 5.10 L are automatically enabled and a few L imported, when you use L, turning your script into a full featured web application. =end original かんたんなHello Worldアプリケーションはこのようになります。L, L, L 、Perl 5.10のL が自動的に有効になり、 L がいくつかインポートされます。 Lを使うと、あなたのスクリプトは完全な機能を備えたウェブアプリケーションになります。 #!/usr/bin/env perl use Mojolicious::Lite; get '/' => sub { my $c = shift; $c->render(text => 'Hello World!'); }; app->start; =begin original With L there is also a helper command to generate a small example application. =end original L には、小さなアプリケーションサンプルを生成するためのヘルパーコマンドがあります。 $ mojo generate lite_app myapp.pl =head2 Commands (コマンド) =begin original Many different L are automatically available from the command line. CGI and L environments can even be detected and will usually just work without commands. =end original 多くのL が自動的にコマンドラインから利用できるようになります。CGIまたはL のどちらの環境であるかは自動的に検知されるため、通常はコマンドで指定しなくても動作します。 $ ./myapp.pl daemon Server available at http://127.0.0.1:3000 $ ./myapp.pl daemon -l http://*:8080 Server available at http://127.0.0.1:8080 $ ./myapp.pl cgi ...CGI output... $ ./myapp.pl get / Hello World! =begin original $ ./myapp.pl ...List of available commands (or automatically detected environment)... =end original $ ./myapp.pl ...利用可能なコマンドが表示(または自動的に環境が検知される)... =begin original A call to L (Cstart>), which starts the command system, should be the last expression in your application, because its return value can be significant. =end original L (Cstart>)を呼び出すと、コマンドシステムが開始されます。この呼び出しはアプリケーションの最後に置くべきです。というのは、返り値が大きな影響をもつ場合があるからです。 =begin original # Use @ARGV to pick a command app->start; =end original # @ARGVを使ってコマンドを取得する app->start; =begin original # Start the "daemon" command app->start('daemon', '-l', 'http://*:8080'); =end original # "daemon"コマンドを開始する app->start('daemon', '-l', 'http://*:8080'); =head2 Reloading (リロード) =begin original Your application will automatically reload itself if you start it with the L development web server, so you don't have to restart the server after every change. =end original 開発用サーバーのLでアプリケーションを起動すれば、 アプリケーションは自動的にリロードされます。ソースコードを変更した後に毎回サーバを再起動させる必要はありません。 $ morbo ./myapp.pl Server available at http://127.0.0.1:3000 =begin original For more information about how to deploy your application see also L. =end original アプリケーションのデプロイ方法について詳しい情報は、 Lの項目を見てください。 =head2 Routes (ルーティング(Routes)) =begin original Routes are basically just fancy paths that can contain different kinds of placeholders and usually lead to an action, if they match the path part of the request URL. The first argument passed to all actions (C<$c>) is a L object, containing both the HTTP request and response. =end original ルーティング(routes)とは、一般的にいうと異なる種類のプレースホルダを含むことのできる仮想的なパスのことです。通常はアクションにつながっており、リクエストURLのパス部分にマッチしたときに実行されます。すべてのアクション (C<$c>) に渡される第一引数は、Lのインスタンスです。これにはHTTPリクエストとレスポンスが含まれています。 use Mojolicious::Lite; =begin original # Route leading to an action that renders some text get '/foo' => sub { my $c = shift; $c->render(text => 'Hello World!'); }; =end original # テキストをレンダリングするアクションへのルーティング get '/foo' => sub { my $c = shift; $c->render(text => 'Hello World!'); }; app->start; =begin original Response content is often generated by actions with L, but more about that later. =end original レスポンスコンテントの多くは Lを使ったアクションによって生成されます。詳しくは後ほど。 =head2 GET/POST parameters (GET/POSTパラメーター) =begin original All C and C parameters sent with the request are accessible via L. =end original すべてのCとCパラメーターはLを通じてアクセスできます。 use Mojolicious::Lite; # /foo?user=sri get '/foo' => sub { my $c = shift; my $user = $c->param('user'); $c->render(text => "Hello $user."); }; app->start; =head2 Stash and templates (スタッシュとテンプレート) =begin original The L is used to pass data to templates, which can be inlined in the C section. A few stash values like C