Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on Language level interfaces to web API's. These libraries are of course built around existing HTTP Request/Reply interfaces. I see myself as having a few options

submit method on the request object e.g

my $req = Request->new( ... );

my $response = $req->submit; # returns response

my $res2     = $req->submit; # sends the same request again

lazy request accessor

my $req = Request->new( ... );

my $response = $req->response; # returns a reference to the response

my $res2     = $req->response; # doesn't send again, just returns the reference

$req->clear_response;          # clears the response value

my $res3     = $req->response; # sends again because no response exists

These are the 2 options I'm thinking of. I have seen more examples of the former but I'm not sure if there's a reason why. Do you have any preferences on which style interface? or are there other ways I'm not thinking of? Overall I'm trying to discern what the best API is to retrieve the response.

share|improve this question
up vote 1 down vote accepted

Most people who want to keep a reference to the same response around will just save $response somewhere. Your second option violates the single responsibility principle for the method and makes it unpredictable. Sometimes response returns an old reference and sometimes it sends a new request. It also adds additional steps if you want to send another request. It's obvious in a small function, but if a lot of code intervenes between requests, you won't know for sure if you need or want to do a clear first in order to generate a new response.

If you are set on caching the response in the Request object, I would at least make it a separate name from the one that submits it. For example, $response = $req->submit for actually submitting it, and $req->response if you just want to retrieve the last cached response.

share|improve this answer
I was thinking more of response is always an accessor to a response object, but it has a builder if it's not set. (which gets one). but ok, makes sense I think. – xenoterracide Mar 30 '12 at 22:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.