yaml2json

PerlYAMLからJSONへ変換するスクリプトを書いてみた.

!/usr/bin/perl

use Getopt::Long;
use YAML;
use JSON;

my $infile = undef;
my $outfile = undef;

sub main{
    my $in = STDIN;
    my $out = STDOUT;
    if($infile && $infile ne ''){
	open($in, $infile) || die("fail to open file: $infile\n");
    }
    if($outfile && $outfile ne ''){
	open($out, ">$outfile") || die("fail to open file: $outfile\n");
    }
    my $obj = Load(join("", <$in>));
    my $json = JSON->new();
    print($out $json->objToJson($obj)); 
}

GetOptions('in=s' => \$infile,
    'out=s' => \$outfile);
main();