http://www.perlmonks.org?node_id=1014222


in reply to is my api wrapper abstraction ok (is it perlish?)

OOP is about hiding low level operations. It's about delegating, not micro-managing. E.g. at a restaurant you give the waiter your order, and they bring your meal. The consumer code above would be like the waiter bringing back the cooking tools and letting you prepare the meal.

Here is a (hopefully) more obvious interface (and a simpler implementation)

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;o +rder=desc http://api.stackexchange.com/answers?sort=votes;order=desc