1
0

Make json-pretty support URLs too

This commit is contained in:
Noah 2014-06-02 15:37:33 -07:00
parent 4f028a5680
commit e9e5328a0d

View File

@ -2,22 +2,28 @@
# json-pretty: Take a JSON file and pretty-print it. # json-pretty: Take a JSON file and pretty-print it.
# #
# Usage: json-pretty <file.json> # Usage: json-pretty <file.json or url>
# #
# --Kirsle # --Kirsle
use strict; use strict;
use warnings; use warnings;
use LWP::Simple;
use JSON; use JSON;
my $file = shift(@ARGV) or die "Usage: $0 <file.json>\n"; my $file = shift(@ARGV) or die "Usage: $0 <file.json or url>\n";
my $json = JSON->new->utf8->pretty(); my $json = JSON->new->utf8->pretty();
local $/ = undef; my $text = "";
open(my $fh, "<", $file); if ($file =~ /^https?:/i) {
my $text = <$fh>; $text = get $file;
close($fh); } else {
local $/ = undef;
open(my $fh, "<", $file);
$text = <$fh>;
close($fh);
}
my $data = $json->decode($text); my $data = $json->decode($text);
print $json->encode($data); print $json->encode($data);