Changeset 30720

Show
Ignore:
Timestamp:
03/03/09 13:34:34 (4 years ago)
Author:
tokuhirom
Message:

added crushing perl

Location:
websites/perl-users.jp
Files:
2 added
3 modified

Legend:

Unmodified
Added
Removed
  • websites/perl-users.jp/html/articles/mojo-intro.html

    r23752 r30720  
    2828        <div id="head-navigation"><a href="/">Perl-users.jp</a></div> 
    2929<!-- content start --> 
     30<div class="section"> 
    3031<p>LL温泉2008用の資料です。</p> 
    3132 
    3233<p>最近でたばかりのフレームワークである Mojolicious をつかってみましょう。</p> 
    33  
     34</div> 
     35<div class="section"> 
    3436<h3>Mojolicious?</h3> 
    3537 
    3638<p>オールインワンなウェブアプリケーションフレームワークです。依存がほとんどないです</p> 
    37  
     39</div> 
     40<div class="section"> 
    3841<h3>インストール</h3> 
    3942 
    4043<p>すごいかんたんです。</p> 
    4144 
    42 <pre><code># Mojo をダウンロード 
    43 % wget http://mojolicious.org/Mojo-latest.tar.gz 
    44  
    45 # 解凍 
    46 % tar xzvf Mojo-latest.tar.gz 
    47 % cd Mojo 
    48  
    49 # Makefile 生成 
    50 % perl Makefile.PL 
    51  
    52 # make する 
    53 % make 
    54  
    55 # テストスィーツをうごかしてみる 
    56 % make test 
    57  
    58 # インストール 
    59 % sudo make install 
    60  
    61 # うまくいったか確認 
    62 % mojolicious daemon 
    63 Server available at http://127.0.0.1:3000. 
    64  
    65 # アクセスしたら、こんな画面がでます 
    66 </code></pre> 
    67  
    68 <p><img src="http://gyazo.com/ef11a052ae49b35fd69825287f3ae26f.png " /></p> 
    69  
     45<pre> 
     46    # Mojo をダウンロード 
     47    % wget http://mojolicious.org/Mojo-latest.tar.gz 
     48 
     49    # 解凍 
     50    % tar xzvf Mojo-latest.tar.gz 
     51    % cd Mojo 
     52 
     53    # Makefile 生成 
     54    % perl Makefile.PL 
     55 
     56    # make する 
     57    % make 
     58 
     59    # テストスィーツをうごかしてみる 
     60    % make test 
     61 
     62    # インストール 
     63    % sudo make install 
     64 
     65    # うまくいったか確認 
     66    % mojolicious daemon 
     67    Server available at http://127.0.0.1:3000. 
     68</pre> 
     69 
     70<p>アクセスしたら、こんな画面がでます</p> 
     71 
     72<p><img src="<a href="http://gyazo.com/ef11a052ae49b35fd69825287f3ae26f.png">http://gyazo.com/ef11a052ae49b35fd69825287f3ae26f.png</a> " /></p> 
     73</div> 
     74<div class="section"> 
    7075<h3>つかってみる</h3> 
    7176 
    72 <pre><code># スケルトン生成 
    73 % mojolicious generate app BBS 
    74   [mkdir] /private/tmp/bbs/bin 
    75   [write] /private/tmp/bbs/bin/bbs 
    76   [chmod] bbs/bin/bbs 744 
    77   [mkdir] /private/tmp/bbs/lib 
    78   [write] /private/tmp/bbs/lib/BBS.pm               # ディスパッチャ 
    79   [mkdir] /private/tmp/bbs/lib/BBS 
    80   [write] /private/tmp/bbs/lib/BBS/Example.pm       # controller 
    81   [exist] /private/tmp/bbs/lib/BBS 
    82   [write] /private/tmp/bbs/lib/BBS/Context.pm       # コンテキストクラス 
    83   [mkdir] /private/tmp/bbs/t 
    84   [write] /private/tmp/bbs/t/basic.t 
    85   [mkdir] /private/tmp/bbs/public 
    86   [write] /private/tmp/bbs/public/404.html 
    87   [exist] /private/tmp/bbs/public 
    88   [write] /private/tmp/bbs/public/index.html 
    89   [mkdir] /private/tmp/bbs/templates/example 
    90   [write] /private/tmp/bbs/templates/example/welcome.phtml 
    91 # はしらせてみる 
    92 % cd bbs 
    93 % perl bin/bbs daemon 
    94 Server available at http://127.0.0.1:3000. 
    95 </code></pre> 
    96  
    97 <p><img src="http://gyazo.com/90a6a75811d3fbbb5294e48ea32d655e.png" /></p> 
    98  
     77<pre> 
     78    # スケルトン生成 
     79    % mojolicious generate app BBS 
     80      [mkdir] /private/tmp/bbs/bin 
     81      [write] /private/tmp/bbs/bin/bbs 
     82      [chmod] bbs/bin/bbs 744 
     83      [mkdir] /private/tmp/bbs/lib 
     84      [write] /private/tmp/bbs/lib/BBS.pm               # ディスパッチャ 
     85      [mkdir] /private/tmp/bbs/lib/BBS 
     86      [write] /private/tmp/bbs/lib/BBS/Example.pm       # controller 
     87      [exist] /private/tmp/bbs/lib/BBS 
     88      [write] /private/tmp/bbs/lib/BBS/Context.pm       # コンテキストクラス 
     89      [mkdir] /private/tmp/bbs/t 
     90      [write] /private/tmp/bbs/t/basic.t 
     91      [mkdir] /private/tmp/bbs/public 
     92      [write] /private/tmp/bbs/public/404.html 
     93      [exist] /private/tmp/bbs/public 
     94      [write] /private/tmp/bbs/public/index.html 
     95      [mkdir] /private/tmp/bbs/templates/example 
     96      [write] /private/tmp/bbs/templates/example/welcome.phtml 
     97    # はしらせてみる 
     98    % cd bbs 
     99    % perl bin/bbs daemon 
     100    Server available at http://127.0.0.1:3000. 
     101</pre> 
     102 
     103<p><img src="<a href="http://gyazo.com/90a6a75811d3fbbb5294e48ea32d655e.png">http://gyazo.com/90a6a75811d3fbbb5294e48ea32d655e.png</a>" /></p> 
     104</div> 
     105<div class="section"> 
    99106<h3>イジッテミル</h3> 
    100107 
     
    103110<p>embedded perl っぽい記法なので、初心者でも安心です。</p> 
    104111 
    105 <pre><code>% my $c = shift; 
    106 &lt;!doctype html&gt; 
    107     &lt;head&gt;&lt;title&gt;Welcome to the Mojolicious Web Framework!&lt;/title&gt;&lt;/head&gt; 
    108     &lt;body&gt; 
    109         &lt;h2&gt;Welcome to the Mojolicious Web Framework!&lt;/h2&gt; 
    110         This page was generated from the template 
    111         "templates/example/welcome.phtml", 
    112         &lt;a href="&lt;%= $c-&gt;url_for %&gt;"&gt; 
    113             click here 
    114         &lt;/a&gt;  
    115         to reload the page or 
    116         &lt;a href="/index.html"&gt; 
    117             here 
    118         &lt;/a&gt; 
    119         to move forward to a static page. 
    120     &lt;/body&gt; 
    121 &lt;/html&gt; 
    122 </code></pre> 
     112<pre> 
     113    % my $c = shift; 
     114    <!doctype html> 
     115        <head><title>Welcome to the Mojolicious Web Framework!</title></head> 
     116        <body> 
     117            <h2>Welcome to the Mojolicious Web Framework!</h2> 
     118            This page was generated from the template 
     119            "templates/example/welcome.phtml", 
     120            <a href="<%= $c->url_for %>"> 
     121                click here 
     122            </a>  
     123            to reload the page or 
     124            <a href="/index.html"> 
     125                here 
     126            </a> 
     127            to move forward to a static page. 
     128        </body> 
     129    </html> 
     130</pre> 
    123131 
    124132<p>このファイルを下記のようにかきかえてしまう</p> 
    125133 
    126 <pre><code>% my $c = shift 
    127 &lt;!doctype html&gt; 
    128 &lt;head&gt;&lt;title&gt;Mojolicious 掲示板&lt;/title&gt;&lt;/head&gt; 
    129 &lt;body&gt; 
    130     &lt;h1&gt;Mojolicious 掲示板&lt;/h1&gt; 
    131     &lt;form method="post" action="&lt;%= $c-&gt;url_for %&gt;"&gt; 
    132         &lt;input type="text" name="body"&gt; 
    133         &lt;input type="submit" value="送信"&gt; 
    134     &lt;/form&gt; 
    135  
    136     &lt;ul&gt; 
    137 % for my $entry (@{$c-&gt;stash-&gt;{rows}}) { 
    138         &lt;li&gt;&lt;%= $entry-&gt;[0] %&gt; &lt;%= $entry-&gt;[1] %&gt;&lt;/li&gt; 
    139 % } 
    140     &lt;/ul&gt; 
    141 &lt;/body&gt; 
    142 &lt;/html&gt; 
    143 </code></pre> 
     134<pre> 
     135    % my $c = shift 
     136    <!doctype html> 
     137    <head><title>Mojolicious 掲示板</title></head> 
     138    <body> 
     139        <h1>Mojolicious 掲示板</h1> 
     140        <form method="post" action="<%= $c->url_for %>"> 
     141            <input type="text" name="body"> 
     142            <input type="submit" value="送信"> 
     143        </form> 
     144 
     145        <ul> 
     146    % for my $entry (@{$c->stash->{rows}}) { 
     147            <li><%= $entry->[0] %> <%= $entry->[1] %></li> 
     148    % } 
     149        </ul> 
     150    </body> 
     151    </html> 
     152</pre> 
    144153 
    145154<p>コントローラをかく。</p> 
    146155 
    147 <pre><code>package BBS::Example; 
    148 use strict; 
    149 use warnings; 
    150  
    151 use base 'Mojolicious::Controller'; 
    152 use DBI; 
    153  
    154 sub welcome { 
    155     my ($self, $c) = @_; 
    156     my $path = '/tmp/bbs.sqlite'; 
    157  
    158     # データベースハンドラをひらく 
    159     my $dbh = DBI-&gt;connect("dbi:SQLite:dbname=${path}", '', '') or die $DBI::errstr; 
    160     # schema を定義する 
    161     $dbh-&gt;do(q{CREATE TABLE IF NOT EXISTS entries (id INTEGER PRIMARY KEY, body VARCHAR(255))}) or die $dbh-&gt;errstr; 
    162  
    163     if ($c-&gt;tx-&gt;req-&gt;method eq 'POST') { 
    164         # POST のときは投稿。 
    165  
    166         # クエリをとりだす 
    167         my $body = $c-&gt;tx-&gt;req-&gt;params-&gt;param('body') || ''; 
    168  
    169         # INSERT する 
    170         if ($body) { 
    171             my $sth = $dbh-&gt;prepare('INSERT INTO entries (body) VALUES (?)') or die $dbh-&gt;errstr; 
    172             $sth-&gt;execute($body) or die $dbh-&gt;errstr; 
     156<p>DBI をつかうので</p> 
     157<pre> 
     158    sudo -H cpan DBI DBD::SQLite 
     159</pre> 
     160<p>しておきます</p> 
     161 
     162<pre> 
     163    package BBS::Example; 
     164    use strict; 
     165    use warnings; 
     166     
     167    use base 'Mojolicious::Controller'; 
     168    use DBI; 
     169     
     170    sub welcome { 
     171        my ($self, $c) = @_; 
     172        my $path = '/tmp/bbs.sqlite'; 
     173     
     174        # データベースハンドラをひらく 
     175        my $dbh = DBI->connect("dbi:SQLite:dbname=${path}", '', '') or die $DBI::errstr; 
     176        # schema を定義する 
     177        $dbh->do(q{CREATE TABLE IF NOT EXISTS entries (id INTEGER PRIMARY KEY, body VARCHAR(255))}) or die $dbh->errstr; 
     178     
     179        if ($c->tx->req->method eq 'POST') { 
     180            # POST のときは投稿。 
     181 
     182            # クエリをとりだす 
     183            my $body = $c->tx->req->params->param('body') || ''; 
     184 
     185            # INSERT する 
     186            if ($body) { 
     187                my $sth = $dbh->prepare('INSERT INTO entries (body) VALUES (?)') or die $dbh->errstr; 
     188                $sth->execute($body) or die $dbh->errstr; 
     189            } 
     190     
     191            # リダイレクトする 
     192            my $res = $c->tx->res; 
     193            $res->code(302); 
     194            $res->headers->header('Location' => $c->tx->req->url); # 本当は URL をとりだすコードのかきかたがあるとおもうのだけど、うまくうごかない 
     195        } else { 
     196            # GET のときは掲示板表示 
     197            my $sth = $dbh->prepare('SELECT id, body FROM entries ORDER BY id DESC LIMIT 30') or die $dbh->errstr; 
     198            $sth->execute() or die $dbh->errstr; # 実行 
     199            my $rows = $sth->fetchall_arrayref(); # 取得 
     200            $c->stash->{rows} = $rows; # テンプレートエンジンにわたす 
     201            $c->render(); # テンプレートエンジンで描画 
    173202        } 
    174  
    175         # リダイレクトする 
    176         my $res = $c-&gt;tx-&gt;res; 
    177         $res-&gt;code(302); 
    178         $res-&gt;headers-&gt;header('Location' =&gt; $c-&gt;tx-&gt;req-&gt;url); # 本当は URL をとりだすコードのかきかたがあるとおもうのだけど、うまくうごかない 
    179     } else { 
    180         # GET のときは掲示板表示 
    181         my $sth = $dbh-&gt;prepare('SELECT id, body FROM entries ORDER BY id DESC LIMIT 30') or die $dbh-&gt;errstr; 
    182         $sth-&gt;execute() or die $dbh-&gt;errstr; # 実行 
    183         my $rows = $sth-&gt;fetchall_arrayref(); # 取得 
    184         $c-&gt;stash-&gt;{rows} = $rows; # テンプレートエンジンにわたす 
    185         $c-&gt;render(); # テンプレートエンジンで描画 
    186203    } 
    187 } 
    188  
    189 1; 
    190 </code></pre> 
     204     
     205    1; 
     206</pre> 
    191207 
    192208<p>httpd を起動します。</p> 
    193209 
    194 <pre><code>% perl bin/bbs daemon 
    195 </code></pre> 
     210<pre> 
     211    % perl bin/bbs daemon 
     212</pre> 
    196213 
    197214<p>下記のような画面がでれば完成です。</p> 
    198215 
    199 <p><img src="http://gyazo.com/98d676ee8a78ba85e474a9e91bfaee1e.png" /></p> 
     216<p><img src="<a href="http://gyazo.com/98d676ee8a78ba85e474a9e91bfaee1e.png">http://gyazo.com/98d676ee8a78ba85e474a9e91bfaee1e.png</a>" /></p> 
     217 
     218</div> 
    200219 
    201220 
  • websites/perl-users.jp/html/index.html

    r30453 r30720  
    134134                                <ul> 
    135135                                    <li><a href="/articles/mobile.html">Perlでモバイルプログラミング</a></li> 
     136                                    <li><a href="/articles/crushing_perl.html">Crushing Perl</a></li> 
    136137                                </ul> 
    137138                            </div> 
  • websites/perl-users.jp/ttroot/index.html

    r30453 r30720  
    106106                                <ul> 
    107107                                    <li><a href="/articles/mobile.html">Perlでモバイルプログラミング</a></li> 
     108                                    <li><a href="/articles/crushing_perl.html">Crushing Perl</a></li> 
    108109                                </ul> 
    109110                            </div>