Created
February 8, 2018 22:08
-
-
Save tsibley/d5b4e8f9d7f4a1207d4974fb69b03745 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use open qw< :std :encoding(UTF-8) >; | |
use URI; | |
use JSON::PP; | |
my $JSON = JSON::PP->new->pretty->allow_nonref; | |
my @uris = | |
map { uri_to_hash($_) } | |
map { URI->new($_) } | |
@ARGV; | |
sub uri_to_hash { | |
my $uri = shift; | |
my $call_if_can = sub { | |
my $object = shift; | |
my $method = $object->can(shift); | |
return $method ? $object->$method(@_) : undef; | |
}; | |
return { | |
scheme => $uri->scheme, | |
opaque => $uri->opaque, | |
userinfo => $uri->$call_if_can("userinfo"), | |
authority => $uri->authority, | |
host => $uri->$call_if_can("host"), | |
port => $uri->$call_if_can("port"), | |
path => $uri->path, | |
query_pairs => { $uri->$call_if_can("query_form") }, | |
fragment => $uri->fragment, | |
}; | |
} | |
print $JSON->encode( @uris > 1 ? \@uris : $uris[0] ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment