2007-04-26 00:11:43

YAMLで困ること

[ Perl ] [ YAML ]

タブ使用禁止でスペースのみは慣れる(未だに嫌だが)

こういう時に困る。
my $ftp = Net::FTP->new( '192.168.0.1', Debug => 1, Passive => 1 );
のパラメータ部分をYAMLにすると

Net::FTP:
  - 192.168.0.1
  - Debug
  - 1
  - Passive
  - 1

となって読み辛いし、説明し辛い。コメント(#)必須。

※ , と => は同義語。
ex) http://search.cpan.org/~nwclark/perl-5.8.8/pod/perlop.pod#Comma_Operator___
Net::FTPはモジュール内で '192.168.0.1' をshiftして、残りの配列をそのままハッシュに代入している。

YAMLを直感的に書き辛い時はYAML::Syckで確認。

use YAML::Syck;
use Data::Dumper;
my $ref = {
        net_ftp_opt => [
                '192.168.0.1',
                Debug => 0,
                Passive => 1
        ],
};
my $yaml = YAML::Syck::Dump($ref);
print $yaml, "\n";
my $perl = YAML::Syck::Load($yaml);
print Dumper($perl), "\n";

こういうのが出てくると泣ける(Net::FTP::Commonとか)
config:
  -
    LocalFile: test.dat
    Pass: password
    User: id
  - Debug
  - 1
  - Timeout
  - 120

2007-04-26 00:08:35

ViewのconfigもYAML化

[ Catalyst ] [ YAML ]

便利かは微妙。

[View](lib/MyApp/View/TT.pm)の以下削除

__PACKAGE__->config({
       CATALYST_VAR => 'Catalyst',
       INCLUDE_PATH => [
               MyApp->path_to( 'root', 'src' ),
               MyApp->path_to( 'root', 'lib' )
       ],
       PRE_PROCESS  => 'config/main',
       WRAPPER      => 'site/wrapper',
       ERROR        => 'error.tt2',
       TIMER        => 0
});

[myapp.yml]に以下追加

View::TT:
  CATALYST_VAR: Catalyst
  INCLUDE_PATH:
    - /home/path/MyApp/root/src
    - /home/path/MyApp/root/lib
  PRE_PROCESS: config/main
  WRAPPER: site/wrapper
  ERROR: error.tt2
  TIMER: 0

2007-04-26 00:07:13

ModelのconfigもYAML化

[ Catalyst ] [ YAML ]

便利。

[Model](lib/MyApp/Model/MyAppDB.pm)の以下削除

__PACKAGE__->config(
       schema_class => 'MyAppDB',
       connect_info => [
               'dbi:mysql:DBNAME:192.168.0.1:3306',
               'ID',
               'PW',
       ],
);

[myapp.yml]に以下追加

Model::MyAppDB:
  schema_class: MyAppDB
  connect_info:
    - dbi:mysql:DBNAME:192.168.0.1:3306
    - ID
    - PW

ex)http://search.cpan.org/~bricas/Catalyst-Plugin-ConfigLoader-0.14/lib/Catalyst/Plugin/ConfigLoader/Manual.pod
読むまで YAML::Syck 使ってたorz