nataraj has asked for the wisdom of the Perl Monks concerning the following question:

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)

Replies are listed 'Best First'.
Re: Lazy fetching role for API clients' objects
by kcott (Archbishop) on Sep 10, 2022 at 07:29 UTC
      I'm guessing this is related to your earlier "Framework for making Moose-compatible object from json" post; providing a link to that would've been appropriate.

      Yes this question came from the same project, but I considered it a completely different question

      Take a look at "Moose: has $name|@$names => %options". The "lazy" and "builder" options would seem to be the most appropriate.

      I know how to implement it on bare Moose. At least have an idea. But I want to make sure there is no ready to use tool for it. This is what this post about

Re: Lazy fetching role for API clients' objects
by nataraj (Sexton) on Sep 17, 2022 at 21:46 UTC

    I have really early prototype for MooseX::LazyFetch. You can have a look at it: MooseX::LazyFetch, MooseX::LazyFetch::Role, MooseX::LazyFetch::Meta::Trait::Attribute

    It should be used like that:

    package Test; use Moose; use MooseX::LazyFetch; has id => (is=>'rw'); has x => (is=>'rw', lazy_fetch => 1); has y => (is=>'rw', lazy_fetch => 1); has z => (is=>'rw', lazy_fetch => 1); around 'fetcher' => sub { my $orig = shift; my $self = shift; return {x=>42,y=>3.14,z=>777}; # As if it fetched it }; 1;

    Once somebody try to access x,y or z, it will call fetcher, and set all attributes that are designated for lazy fetching