Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tsibley
Created February 8, 2018 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsibley/d5b4e8f9d7f4a1207d4974fb69b03745 to your computer and use it in GitHub Desktop.
Save tsibley/d5b4e8f9d7f4a1207d4974fb69b03745 to your computer and use it in GitHub Desktop.
#!/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