Parse::RecDescent始め

まだ、これを使うか決めてないんだけど、
実用Perlプログラミングに載ってたので、どんな感じか使ってみた。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use v5.14;
use strict;
use warnings;
 
use Parse::RecDescent;
 
my $parser = Parse::RecDescent->new( q{
    Source : "(" Text Source(s?) ")" {
        if ( @{$item[3]} ) {
            $return = $item[2] . ' -> ' . $item[3][0];
        }
        else {
            $return = $item[2];
        }
    }
    Text   : /[a-z]+/
} );
 
my $text = do { local $/; <DATA>; };
say $parser->Source( $text );
 
__DATA__
( aaa( bbb( ccc( ddd( eee( fff( ggg( hhh ) ) ) ) ) ) ) )

これを実行すると、こうなる。

$ perl aaa.pl
aaa -> bbb -> ccc -> ddd -> eee -> fff -> ggg -> hhh

こういうのをパースする予定はないんだけど、
こういうことも簡単に出来るって素敵デスね!

おしまい。

Leave a Comment