Let's imagine we have a client for some kind of API, json-based REST API to be more specific

Objects returned by that API are stored as Moose-compatible objects. Sometimes server provides full set of object data (when you ask for object directly). Sometimes not (when object returned as a part of complex request, usually provided just for reference. Like that

{ "article": { "author": { "id": "12345", "display_name": "Swami Dhyan Nataraj" } } }

In this example "author" section has minimal information, that is presumably needed for displaying the article. Though if you request full info for person with "12345" id you will get much more info, including "address" for example

I would expect perl implementation of API to work like this:

my $article = $client->article($article_id); my $addr = $article->author->address;

To do this, "address" method should fetch missing information from the server when it is needed. I would call it Lazy Fetching (BTW, do you know proper name for it?)

Two more questions...

First: I guess this lazy fetching can be implemented as Moose::Role, so you need to write method that knows how to refetch object, provide this method to the object's constructor (LazyFetch Role will know how to deal with it), and mark attributes that are due for lazy fetching. I tried to find such role in CPAN, but did not succeed. Did I miss it? May be there are some implementations that are not Moose one?

Second: Have you seen this concept of Lazy Fetching implemented anywhere else? Not in perl at all. I would hardly believe that I am first to whom this idea came into mind...

Update:P.S.This question is part of bigger project. Previous, may be related questions are Framework for making Moose-compatible object from json, One module to use them all (proxy moudle that exports subs of other modules)


In reply to Lazy fetching role for API clients' objects by nataraj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.