## 自己紹介
- 仕事: .NETでGUIアプリ
- Twitter: @techno_neko
- 所属: Hokkaido.pm
## 今日お話すること
- ポータブルで!
- ナウい!
- CGI!
## cpanmできない環境とか
つまり、レンタルサーバー
## ローカル環境でやること
```bash
$ cpanm Mojolicious
--> Working on Mojolicious
Fetching http://www.cpan.org/authors/id/S/SR/SRI/Mojolicious-5.69.tar.gz ... OK
Configuring Mojolicious-5.69 ... OK
Building and testing Mojolicious-5.69 ... OK
Successfully installed Mojolicious-5.69 (upgraded from 5.57)
$ cd /Users/path/to/mojo_lite
$ mojo generate lite_app foo.cgi
[exist] /Users/path/to/mojo_lite
[write] /Users/path/to/mojo_lite/foo.cgi
[chmod] /Users/path/to/mojo_lite/foo.cgi 744
```
## 実行してみよう!
```bash
$ perl foo.cgi cgi
[Mon Dec 15 23:51:07 2014] [debug] Your secret passphrase needs to be changed!!!
[Mon Dec 15 23:51:07 2014] [debug] GET "/".
[Mon Dec 15 23:51:07 2014] [debug] Routing to a callback.
[Mon Dec 15 23:51:07 2014] [debug] Rendering template "index.html.ep" from DATA section.
[Mon Dec 15 23:51:07 2014] [debug] Rendering template "layouts/default.html.ep" from DATA section.
[Mon Dec 15 23:51:07 2014] [debug] 200 OK (0.003921s, 255.037/s).
Content-Type: text/html;charset=UTF-8
Date: Mon, 15 Dec 2014 14:51:07 GMT
Status: 200 OK
Content-Length: 137
<!DOCTYPE html>
<html>
<head><title>Welcome</title></head>
<body>Welcome to the Mojolicious real-time web framework!
</body>
</html>
```
## Mojoliciousをローカルフォルダに
さっき、インストールしたばっかりなので、
-n(テストなし)でインストール
```bash
$ cpanm --reinstall -n -l. Mojolicious
--> Working on Mojolicious
Fetching http://www.cpan.org/authors/id/S/SR/SRI/Mojolicious-5.69.tar.gz ... OK
Configuring Mojolicious-5.69 ... OK
Building Mojolicious-5.69 ... OK
Successfully reinstalled Mojolicious-5.69
1 distribution installed
```
## こんな感じ
```bash
$ tree -L 3
.
├── bin # 中身は省略
├── foo.cgi
├── lib
│ └── perl5
│ ├── Mojo
│ ├── Mojo.pm
│ ├── Mojolicious
│ ├── Mojolicious.pm
│ ├── Test
│ ├── darwin-2level
│ └── ojo.pm
└── man
├── man1 # 中身は省略
└── man3 # 中身は省略
```
## ポータブル・・・?
```bash
$ perlbrew off
$ perl -Ilib/perl5 foo.cgi cgi
Can't locate IO/Socket/IP.pm in @INC (@INC contains: lib/perl5 /Library/Perl/5.16/darwin-thread-multi-2level /Library/Perl/5.16 /Network/Library/Perl/5.16/darwin-thread-multi-2level /Network/Library/Perl/5.16 /Library/Perl/Updates/5.16.2 /System/Library/Perl/5.16/darwin-thread-multi-2level /System/Library/Perl/5.16 /System/Library/Perl/Extras/5.16/darwin-thread-multi-2level /System/Library/Perl/Extras/5.16 .) at lib/perl5/Mojo/IOLoop/Client.pm line 5, line 2231.
BEGIN failed--compilation aborted at lib/perl5/Mojo/IOLoop/Client.pm line 5, line 2231.
perlbrew is turned off.
(省略)
```
## Can't locate IO/Socket/IP.pm
## という訳で・・・
```bash
$ perlbrew switch perl-5.20
$ cpanm --reinstall -l. IO::Socket::IP
--> Working on IO::Socket::IP
Fetching http://www.cpan.org/authors/id/P/PE/PEVANS/IO-Socket-IP-0.34.tar.gz ... OK
Configuring IO-Socket-IP-0.34 ... OK
Building and testing IO-Socket-IP-0.34 ... OK
Successfully reinstalled IO-Socket-IP-0.34
1 distribution installed
```
## リベンジだ!
```bash
$ perlbrew off
perlbrew is turned off.
$ perl -Ilib/perl5 foo.cgi cgi
[Tue Dec 16 00:11:33 2014] [debug] Your secret passphrase needs to be changed!!!
[Tue Dec 16 00:11:33 2014] [debug] GET "/".
[Tue Dec 16 00:11:33 2014] [debug] Routing to a callback.
[Tue Dec 16 00:11:33 2014] [debug] Rendering template "index.html.ep" from DATA section.
[Tue Dec 16 00:11:33 2014] [debug] Rendering template "layouts/default.html.ep" from DATA section.
[Tue Dec 16 00:11:33 2014] [debug] 200 OK (0.003315s, 301.659/s).
Content-Type: text/html;charset=UTF-8
Status: 200 OK
Date: Mon, 15 Dec 2014 15:11:33 GMT
Content-Length: 137
<!DOCTYPE html>
<html>
<head><title>Welcome</title></head>
<body>Welcome to the Mojolicious real-time web framework!
</body>
</html>
```
## 実際にサーバーで動かすには?
1. まるっとサーバーにアップロード!
ここから先は、良く分かってないデス・・・
1. 単純なCGIを作って動作確認
- 動かない場合
- パーミッションは問題ない?
## 追加したライブラリの扱いは?
### こんな感じで!
```perl
#!perl
use lib 'lib/perl5';
use Mojolicious::Lite;
# (以下略)
```
## ポータブル!
```bash
$ perl foo.cgi cgi
[Tue Dec 16 00:23:30 2014] [debug] Your secret passphrase needs to be changed!!!
[Tue Dec 16 00:23:30 2014] [debug] GET "/".
[Tue Dec 16 00:23:30 2014] [debug] Routing to a callback.
[Tue Dec 16 00:23:30 2014] [debug] Rendering template "index.html.ep" from DATA section.
[Tue Dec 16 00:23:30 2014] [debug] Rendering template "layouts/default.html.ep" from DATA section.
[Tue Dec 16 00:23:30 2014] [debug] 200 OK (0.003152s, 317.259/s).
Content-Type: text/html;charset=UTF-8
Status: 200 OK
Date: Mon, 15 Dec 2014 15:23:30 GMT
Content-Length: 137
<!DOCTYPE html>
<html>
<head><title>Welcome</title></head>
<body>Welcome to the Mojolicious real-time web framework!
</body>
</html>
```
## とは言うものの・・・
MacOS 10.9.5に入ってるPerlのバージョンは、
v5.16.2だから、そこまで古くない・・・。
## まとめ
- "lib/perl5"フォルダは、"lib"フォルダにしても良いかも
- 自作の"*.pm"とかも、こんな感じで配置
- 見られたくないファイルは、".htaccess"とかでガードする感じで
- 参考: https://github.com/tokuhirom/menta
## おまけ
たとえば、GitHubにしかないモジュール
https://github.com/techno-cat/p5-Cassis
## 取得する
- git clone する
- zip ファイルとしてダウンロード
## フォルダ構成
```bash
$ cd p5-Cassis
$ tree -L 1
.
├── Build.PL
├── Changes
├── LICENSE
├── META.json
├── README.md
├── Samples
├── SandBox
├── cpanfile
├── lib
├── minil.toml
└── t
```
## サンプルコード(aaa.pl)
```perl
use v5.14;
use strict;
use warnings;
use Cassis;
use constant SAMPLING_RATE => 44100;
my $dco = Cassis::DCO::Sin->new( fs => SAMPLING_RATE );
$dco->set_freq( 440 );
my $wav = $dco->exec( num => SAMPLING_RATE ); # 1sec
Cassis::File::write(
file => '440.wav',
fs => SAMPLING_RATE, channels => [ $wav ] );
```
## フォルダ構成
```bash
$ tree -L 1
.
├── Build.PL
├── Changes
├── LICENSE
├── META.json
├── README.md
├── Samples
├── SandBox
├── aaa.pl # <-- 追加したファイル
├── cpanfile
├── lib
├── minil.toml
└── t
```
## 実行してみよう!
"440.wav"を再生してみよう!
```bash
$ perl -Ilib aaa.pl
$ ls
440.wav LICENSE Samples cpanfile t
Build.PL META.json SandBox lib
Changes README.md aaa.pl minil.toml
```
## 他のサンプルコードは?
- 帰ってきたネコブログ
http://www.nekonotechno.com/nekopress/?tag=cassis
- インストールする場合
```bash
$ cd p5-Cassis
$ cpanm .
```