-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml2messagepack
executable file
·67 lines (58 loc) · 1.12 KB
/
yaml2messagepack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env perl
#
# $Id: yaml2messagepack,v 1.3 2012/01/13 06:57:15 ryo Exp $
#
# Copyright(c) 2011 Ryo SHIMIZU <[email protected]>
#
use strict;
use warnings;
use Data::MessagePack;
use Encode;
use Getopt::Std;
use LWP::Simple;
use YAML::Syck;
use YAML::XS;
sub usage {
die <<__USAGE__;
usage: yaml2jmessagepack [options] [file ...]
-x use YAML::XS instead of YAML::Syck
-q quiet
__USAGE__
}
my %opts;
getopts('qx', \%opts) or usage();
if ($#ARGV >= 0) {
for my $file (@ARGV) {
print "== $file ==\n" if (($#ARGV > 0) && !exists($opts{q}));
yaml2messagepack(scalar(fileread($file)));
}
} else {
my $file = join("", <>);
yaml2messagepack($file);
}
exit;
sub yaml2messagepack {
my $yaml = shift;
my $data;
if (exists $opts{x}) {
$data = YAML::XS::Load($yaml);
} else {
$data = YAML::Syck::Load($yaml);
}
print Data::MessagePack->pack($data);
}
sub fileread {
my $file = shift;
if ($file =~ m#^(http|https|ftp)://#i) {
my $yaml = get($file);
unless (defined $yaml) {
warn "$file: error\n";
$yaml = '';
}
$yaml;
} else {
open my $fh, "< $file" or die "open: $file: $!\n";
local $/;
<$fh>;
}
}