記号の読み込みテスト

概要

  • 記号そのものを値として読み込みたい場合、"" または '' で括る必要がある。

ソース

  • marks.zip

  • marks.pl

    # YAML 記号の読み込みテスト
    # ハイフン, コロン, 角括弧, 波括弧, ハッシュ記号, 
    # バックスラッシュ, シングルクォート, ダブルクォート, 
    # 改行, タブ等の# 記号そのものを値として読み込みたい場合、
    # "" または '' で括る必要がある。
    
    use strict;
    use warnings;
    use utf8;
    use Encode;
    use YAML::Syck;
    use Data::Dump qw(dump);
    
    $YAML::Syck::ImplicitUnicode = 1;
    
    my $charset = 'CP932';
    
    binmode( STDIN,  ":encoding($charset)" );
    binmode( STDOUT, ":encoding($charset)" );
    binmode( STDERR, ":encoding($charset)" );
    
    # バージョン確認
    print "Perl version: " . $] . "\n";
    print "YAML::Syck version: " . $YAML::Syck::VERSION . "\n";
    
    # 括るまたはエスケープ
    my $MarksFile = './marks.yaml';
    my $MarksIn = YAML::Syck::LoadFile( $MarksFile ) or die( "$MarksFile:$!" );
    print dump( $MarksIn ) . "\n";
    
    # 変数をシリアライズ
    my $MarksOut = [ '-', ':', '[', ']', '{', '}', '#', '\\', '\'', '\"', "\n", "\t", ];
    print Dump( $MarksOut ) . "\n";
    
    # EOF
  • marks.yaml

    --- 
    - "-"
    - ":"
    - "["
    - "]"
    - "{"
    - "}"
    - #
    - \#
    - "#"
    - \
    - \\
    - \'
    - "'"
    - \"
    - '"'
    - \n
    - "\n"
    - \t
    - "\t"

出力結果

Perl version: 5.010001
YAML::Syck version: 1.15
[
  "-",
  ":",
  "[",
  "]",
  "{",
  "}",
  undef,
  "\\#",
  "#",
  "\\",
  "\\\\",
  "\\'",
  "'",
  "\\\"",
  "\"",
  "\\n",
  "\n",
  "\\t",
  "\t",
]
--- 
- "-"
- ":"
- "["
- "]"
- "{"
- "}"
- "#"
- \
- "'"
- \"
- "\n"
- "\t"

16進数等の読み込み

概要

  • YAML::Syck で16進数等を解釈させたい場合は「$YAML::Syck::ImplicitTyping = 1;」が必要。

ソース

  • NumberString1.zip

  • NumberString1.pl

    #!/usr/bin/perl
    # YamlString.pl
    # YAML::Syck での数値文字列の読み込みテスト
    # 数値や論理値等として解釈させるには「$YAML::Syck::ImplicitTyping = 1;」が必要。
    
    use strict;
    use warnings;
    use utf8;
    use Encode;
    use YAML::Syck qw( Load Dump );
    
    $YAML::Syck::ImplicitUnicode = 1;
    
    #my $charsetConsole	= 'UTF-8';
    my $charsetConsole	= 'CP932';
    my $charsetFile		= 'UTF-8';
    
    binmode( STDIN,  ":encoding($charsetConsole)" );
    binmode( STDOUT, ":encoding($charsetConsole)" );
    binmode( STDERR, ":encoding($charsetConsole)" );
    
    # バージョン確認
    print "Perl version: $]\n";
    print "YAML::Syck version: $YAML::Syck::VERSION\n";
    
    my $yamlString = qq{
    Decimal:     [ 0, 1, 2, 4, 8, 10, 16 ]
    Binal:       [ 0b00000, 0b00001, 0b00010, 0b00100, 0b01000, 0b01010, 0b10000 ]
    Octal:       [ 000, 001, 002, 004, 010, 012, 020 ]
    Hexadecimal: [ 0x00, 0x01, 0x02, 0x04, 0x08, 0x0a, 0x10 ]
    Boolean:     [ true, false, yes, no, on, off, null, ~ ]
    漢字:        [ 壱, 弐, 参, 四, 五 ]
    HexKey:      { 0x00: "00", 0x01: "01", 0x02: "02", 0x04: "04", 0x08: "08", 0x0a: "0a", 0x10: "10" }
    };
    
    $YAML::Syck::ImplicitTyping = 0;	# Default
    print "\n\$YAML::Syck::ImplicitTyping = 0\n";
    print Dump( Load( $yamlString ) );
    
    $YAML::Syck::ImplicitTyping = 1;
    print "\n\$YAML::Syck::ImplicitTyping = 1\n";
    print Dump( Load( $yamlString ) );
    
    # EOF

出力結果

Perl version: 5.014002
YAML::Syck version: 1.21

$YAML::Syck::ImplicitTyping = 0
--- 
Binal: 
  - 0b00000
  - 0b00001
  - 0b00010
  - 0b00100
  - 0b01000
  - 0b01010
  - 0b10000
Boolean: 
  - 'true'
  - 'false'
  - 'yes'
  - 'no'
  - 'on'
  - 'off'
  - 'null'
  - ~
Decimal: 
  - 0
  - 1
  - 2
  - 4
  - 8
  - 10
  - 16
HexKey: 
  0x00: '00'
  0x01: '01'
  0x02: '02'
  0x04: '04'
  0x08: '08'
  0x0a: 0a
  0x10: 10
Hexadecimal: 
  - 0x00
  - 0x01
  - 0x02
  - 0x04
  - 0x08
  - 0x0a
  - 0x10
Octal: 
  - '000'
  - '001'
  - '002'
  - '004'
  - '010'
  - '012'
  - '020'
漢字: 
  - 壱
  - 弐
  - 参
  - 四
  - 五

$YAML::Syck::ImplicitTyping = 1
--- 
Binal: 
  - 0b00000
  - 0b00001
  - 0b00010
  - 0b00100
  - 0b01000
  - 0b01010
  - 0b10000
Boolean: 
  - 1
  - ''
  - 1
  - ''
  - 1
  - ''
  - ~
  - ~
Decimal: 
  - 0
  - 1
  - 2
  - 4
  - 8
  - 10
  - 16
HexKey: 
  0: '00'
  1: '01'
  10: 0a
  16: 10
  2: '02'
  4: '04'
  8: '08'
Hexadecimal: 
  - 0
  - 1
  - 2
  - 4
  - 8
  - 10
  - 16
Octal: 
  - 0
  - 1
  - 2
  - 4
  - 8
  - 10
  - 16
漢字: 
  - 壱
  - 弐
  - 参
  - 四
  - 五

正規表現

概要

  • YAML 中での正規表現の表記方法。
  • YAML::Syck で Dump すると、非 ASCII 文字列はバイト列となる。
  • バイト列を Load しても utf8 フラグが失われているため、ユニコード文字列にはマッチしない。
  • 正規表現中にユニコード文字を入れたい場合は、\x{xxxx} または \N{U+xxxx} と表記しなければならない。
  • 正規表現の文字列化 (perl v5.14.0 での変更点)
    • perl 5.14 以降: (?^:...)
    • perl 5.14 前: (?-xism:...)

ソース

  • RegExp.zip
    #!/usr/bin/perl
    # 正規表現の読み込み
    
    use strict;
    use warnings;
    use utf8;
    use Encode;
    use YAML::Syck qw( Load Dump );
    use Win32::Unicode::Native;
    
    $YAML::Syck::ImplicitUnicode = 1;
    
    # バージョン確認
    print "Perl version: $]\n";
    print "YAML::Syck version: $YAML::Syck::VERSION\n";
    
    my $obj = {
    	'\[AD\]$' => qr/\[AD\]$/i,
    	'\[広\s?告\]$' => qr/\[広\s?告\]$/,
    	'(吉|\x{20bb7})野' => qr/(吉|\x{20bb7})野/,
    };
    my $yaml = Dump( $obj );
    print $yaml;
    my $yaml2 = q{
    ---
    '\[AD\]$':
      !!perl/regexp (?^i:\[AD\]$)
    '\[広\s?告\]$':
      !!perl/regexp (?^:\[\x{5e83}\s?\x{544a}\]$)
    '(吉|\x{20bb7})野':
      !!perl/regexp (?^:(\x{5409}|\x{20bb7})\x{91ce})
    };
    print $yaml2;
    my $conf = Load($yaml2);
    print Dump($conf);
    
    print "====\n";
    
    my @samples = (
    	"無料で始める恋愛ドラマアプリ[広 告]",
    	"この夏をもっと楽しもう[Ad]",
    	"\x{20BB7}野家",
    );
    
    foreach my $sample (@samples){
    	my $isMatch = '';
    	foreach my $regex (keys(%{$conf})){
    		if ( $sample =~ $conf->{$regex} ){
    			$isMatch = $regex;
    			last;
    		}
    	}
    	if ( $isMatch ){
    		printf("match(%s): '%s'\n", $isMatch, $sample);
    	} else {
    		printf("not match: '%s'\n", $sample);
    	}
    }
    
    # EOF

出力結果

Perl version: 5.014004
YAML::Syck version: 1.22
---
"(吉|\\x{20bb7})野": !!perl/regexp "(?^u:(\xE5\x90\x89|\\x{20bb7})\xE9\x87\x8E)"

\[AD\]$: !!perl/regexp (?^i:\[AD\]$)
"\\[広\\s?告\\]$": !!perl/regexp "(?^u:\\[\xE5\xBA\x83\\s?\xE5\x91\x8A\\]$)"

---
'\[AD\]$':
  !!perl/regexp (?^i:\[AD\]$)
'\[広\s?告\]$':
  !!perl/regexp (?^:\[\x{5e83}\s?\x{544a}\]$)
'(吉|\x{20bb7})野':
  !!perl/regexp (?^:(\x{5409}|\x{20bb7})\x{91ce})
---
"(吉|\\x{20bb7})野": !!perl/regexp (?^u:(?^:(\x{5409}|\x{20bb7})\x{91ce}))
\[AD\]$: !!perl/regexp (?^:(?^i:\[AD\]$))
"\\[広\\s?告\\]$": !!perl/regexp (?^u:(?^:\[\x{5e83}\s?\x{544a}\]$))
====
match(\[広\s?告\]$): '無料で始める恋愛ドラマアプリ[広 告]'
match(\[AD\]$): 'この夏をもっと楽しもう[Ad]'
match((吉|\x{20bb7})野): '𠮷野家'

リンク