# for the following API urls: ##http://api.stackexchange.com/answers/{ids}/comments/?query_str ##http://api.stackexchange.com/answers/?query_str use Net::StackExchange; my $a = Net::StackExchange->new(); print $a->answers->{answers_comments}->(123,134,145,{ order=> "desc", sort=>"votes" }); ######## THE ABOVE PRINTS: #http://api.stackexchange.com/answers/123;134;145/comments?sort=votes&order=desc print $a->answers->{answers}->({ order=> "desc", sort=>"votes" }); ######## THE ABOVE PRINTS: #http://api.stackexchange.com/answers/?sort=votes&order=desc #### ################### # Inside StackExchange.pm ################### sub new { return Net::StackExchange::V2->new(); } ################### # Inside V2.pm ################### sub new { my ($class) = @_; my $self = {}; bless $self, $class; return $self; } sub answers { return Net::StackExchange::V2::Answers->new(); } ################### # Inside Answers.pm ################### use Net::StackExchange::V2::Common qw(query no_params one_param); sub new { my ($class) = @_; my $self = { site => "stackoverflow", answers => no_params("answers"), answers_comments => one_param("answers","comments"), }; bless $self, $class; return $self; } ################################################# ################################################# # THIS IS WHAT NEEDS ATTENTION # Inside Common.pm ################################################# use constant BASE_URL => "http://api.stackexchange.com/"; our @ISA = qw(Exporter); our @EXPORT = qw(query no_params one_param); sub query { #get the hash from the last param and use it for the query str my $queryStrHash = pop @_; #add rest of the params into the url. my $url = join("/",@_); my @params = (); while ( my ($key, $value) = each(%$queryStrHash) ) { push @params, $key."=".$value; } my $query = '?'.join '&',@params; return BASE_URL.$url.$query; } ##function to generate a function that generates url with #a name and no parameters.. sub no_params { #WHY don't I get $self = shift here? my $name = shift; return sub { #WHY don't I get $self = shift here? my $queryStr = pop @_; return query($name, $queryStr); } } ##function to generate a function that generates url #with a name and one parameter.. sub one_param { my $param1 = shift; my $param2 = shift; return sub { my $q = pop @_; my @ids = @_; return query($param1, join(";",@ids), $param2, $q); } }