package main; use strict; use v5.10; my $obj = My::StackExchange->new; my $uri; $uri = $obj->build_uri( type => 'answers', ids => [123,134,145], comments => 1, options => { order => "desc", sort => "votes" } ); say $uri; $uri = $obj->build_uri( type => 'answers', options => { order => "desc", sort => "votes" } ); say $uri; package My::StackExchange; use URI::FromHash qw( uri ); sub new { bless {} } sub build_uri { my ($self, %arg) = @_; #TODO validate input my @paths = ($arg{type}); if ( $arg{ids} ) { push @paths, join(';', @{ $arg{ids} }); } if ( $arg{comments} ) { push @paths, 'comments'; } return uri( scheme => 'http', host => 'api.stackexchange.com', path => \@paths, query => $arg{options}, ); } __DATA__ http://api.stackexchange.com/answers/123;134;145/comments?sort=votes;order=desc http://api.stackexchange.com/answers?sort=votes;order=desc