Web APIは普段は使わないけど、FTPでアップロードするための
スクリプトはいくつか書いてます。その中で Passwordをハードコーディング
していたところがあったので, Config::Pitが使えるなって思って使ってみた.
ユーザ名とパスワードの指定は以下のように One-Linerでも可能
perl -MConfig::Pit -e'Config::Pit::set("example.com", data=>{ username => "name", password => "pass" })'
でも面倒。シェル関数としてもいいけど、いちいちユーザ名を聞いたりする
のが面倒だったので, perlの "-s"スイッチを利用して書いてみた
#!/usr/bin/perl -s use strict; use warnings; use 5.010; use Config::Pit; our $username; our $password; our $site; die "Not specified username\n" unless $username; die "Username is empty string\n" if $username =~ m/^\s*$/; die "Not specified password\n" unless $password; die "Password is empty string\n" if $password =~ m/^\s*$/; die "Not specified site\n" unless $site; die "Site is empty string\n" if $site =~ m/^\s*$/; Config::Pit::set($site, data => { username => $username, password => $password, });
シェバンのところにスイッチをつけるのは古いバージョンの Perlでは
できなかった記憶があるので 5.10以降のみということで.
これで以下のような具合にできるようになります.
% config_pit.pl site="hoge.com" username="名前" password="パスワード"
情報の取得は
#!/usr/bin/perl use strict; use warnings; use Config::Pit; my $config = pit_get("hoge.com" , require => { "username" => "ヒント", "password" => "ヒント", }); print "username: $config->{'username'}\n"; print "password: $config->{'password'}\n";
requireには欲しいコンフィグ情報をキー, ヒントを値とする
ハッシュリファレンスを与えればよい.