リバースプロクシの設定方法

変更前。
(この設定ファイルは WindowsApache)

<IfModule mod_proxy.c>
  ProxyRequests Off

    <Proxy *>
      Require all granted
    </Proxy>
</IfModule>

変更後。

<IfModule mod_proxy.c>
  ProxyRequests Off

    <Proxy http://localhost:5000/>
      Require all granted
    </Proxy>

    ProxyPass / http://localhost:5000/
    ProxyPassReverse / http://localhost:5000/
</IfModule>

SQL の IN 句を DBI のプリペアードステートメントで使う場合

my $dbh = DBI->connect(...);

my @users  = ('yamada', 'sato', 'suzuki', 'takahashi');
my @values = map {'?'} @users;
my $place_holders = join ', ', @values;
my $sql = sprintf('SELECT id, timestamp FROM sample WHERE users IN (%s);', $place_holders);

# say $sql
# SELECT id, timestamp FROM sample WHERE users IN (?, ?, ?, ?);

my $sth = $dbh->prepare($sql);

eval {
    $sth->execute(@users);
};

if ($@) {
    $dbh->rollback;
    warn $dbh->errstr;
} else {
    $dbh->commit;
}

Windows 上で SASS を利用したときに、CSS の指定文字コードを UTF-8 にする

WindowsRuby を動かすと規定の外部エンコーディングWindows-31J で指定されている。そのため、日本語が含まれていると、「Invalid Windows-31J character…」というエラーが発生する。これは、SASS で出力した CSS に指定文字コードが「Windows-31J」で指定されてしまうため、「UTF-8」を指定した CSS を書き出す。

set LANG=ja_JP.utf8

上記のコマンドを、コマンドプロンプト上で実行する。

「シンプルなWebアプリを今、自分なりに書いてみる」を Mojolicious 4.0 に対応させてみた。

シンプルなWebアプリを今、自分なりに書いてみる」が 2013年5月にリリースされた 4.0 に対応していないので、自分なりに書き直してみた。
修正したのは、Mojolicious::Controller で廃止された API を修正した。

#!/usr/bin/perl
use Mojolicious::Lite;
use HTML::FillInForm::Lite;
use FormValidator::Lite;
use utf8;

get '/' => sub {
    my $self = shift;
    $self->stash->{error_messages} = undef;
    $self->render('index');
};

post '/' => sub {
    my $self = shift;
    my $validator = FormValidator::Lite->new($self->req);

    $validator->set_message(
        'zip.not_null' => '郵便番号が空です。',
        'zip.length'   => '郵便番号が正しくありません。',
        'zip.uint'     => '郵便番号が正しくありません',
    );

    my $res = $validator->check(
        zip => ['NOT_NULL', 'UINT', ['LENGTH' => 7]],
    );

    my @error_messages = ();

    if ($validator->has_error) {
        for my $message ($validator->get_error_messages) {
            push @error_messages, $message;
        }

        $self->stash->{error_messages} = \@error_messages;
        # 下記を修正
        my $html = $self->render('index', partial => 1);

        # 下記を修正
        $self->render(
            text => HTML::FillInForm::Lite->fill(\$html, $self->req->params),
            format => 'html'
        );
    } else {
        $self->stash->{zip} = $self->req->param('zip');
        $self->render('thankyou');
    }
};

app->start;

__DATA__

@@ index.html.ep
% layout 'default';
<p>郵便番号を7桁の数字で入力してください。</p>
% if ($error_messages) {
% for my $message (@$error_messages) {
<p><b style="color:red;"><%= $message %></b></p>
% }
% }
<form action="/" method="post">
  <input type="text" name="zip" maxlength="7" size="7"/>
  <input type="submit" value="送信" />
</form>

@@ thankyou.html.ep
<p>正常に入力されました。郵便番号は「<%= $zip %>」です。<br />ありがとうございます。</p>
<p><a href="/">戻る</a></p>

@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
  <head><title><%= title %></title></head>
  <body><%= content %></body>
</html>

わずか1年半前のサンプルコードが動かなくなるなんて、変化の速さに改めて驚く。

ファイル属性の変更を git 上で管理する

Windows で作成したファイルを git push し、LinuxFreeBSD で git pull したファイルの属性は、0644 で記録されている。
これらのファイルに実行権限 (ファイル属性の変更) を付与して、git commit して git push した後に、Windows で git pull すると、付与した実行権限が反映されていない。
その場合は、Windows 上ならば、Git Bash で、LinuxFreeBSD などではコマンドライン上で下記の作業を実施する。

% ls -l
-rw-r--r--  1 littlebuddha  wheel   329 Jul 30 17:49 sample.pl
% git ls-tree HEAD sample.pl
100644 blob e269d073bc1b03ad0a1281a7f005010d416670ec sample.pl
% chmod u+x sample.pl
% ls -l
-rwxr--r--  1 littlebuddha  wheel   329 Jul 30 17:49 sample.pl
% git ls-tree HEAD sample.pl
100644 blob e269d073bc1b03ad0a1281a7f005010d416670ec sample.pl
% git update-index --chmod=+x sample.pl
% git ls-tree HEAD sample.pl
100644 blob e269d073bc1b03ad0a1281a7f005010d416670ec sample.pl
% git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   sample.pl
#
% git commit -m "add executable attribute."
[master 61e6875] add executable attribute.
 1 file changed, 0 insertions(+), 0 deletions(-)
 mode change 100644 => 100755 sample/sample.pl
% git ls-tree HEAD sample.pl
100755 blob e269d073bc1b03ad0a1281a7f005010d416670ec sample.pl

で完了。ファイル属性の変更を記録しておくことで、Windows で git pull し、ファイルの変更を加えた後に commit と push をしても、上記で記録した実行権限 (ファイル属性の変更) は反映されたままになっている。

cpanm をローカルディレクトリ内にインストールする際の設定

% mkdir -p ~/perl5/extlib/perl5
% echo 'export PERL_CPANM_OPT="--local-lib=~$HOME/perl5/extlib/perl5"' >> ~/.zshrc
% echo 'export PERL5LIB="$HOME/perl5/lib:$HOME/perl5/extlib/perl5/lib/perl5:$PERL5LIB"' >> ~/.zshrc