#!/usr/bin/perl -- use strict; use warnings; our @PAIRS; our %JSON; Main( @ARGV ); exit( 0 ); sub Main { %JSON = fromPairs(); my $startUrl = $PAIRS[0][0]; Paginate( $startUrl, sub { my( $json, $url ) = @_; ## real work here print "## callback @_ \n"; print $json->{result}[0]{name}, "\n\n"; }, ); return; } sub getAll { my @jsons; Paginate( $startUrl, sub { my( $json, $url ) = @_; push @jsons, $json; }, ); return \@jsons; } sub Paginate { my( $url, $callback )= @_; my @gets = $url; while( @gets ){ my $url = shift @gets; my $json = GET( $url ); if( my $id = $json->{next_page_id} ){ push @gets, urlNext( $url, $id, $json ); } $callback->( $json, $url ); } } sub urlNext { my( $url, $id, $json ) = @_; ## logic here return "https://127.0.0.1/wapi/v2.7.3/record:a?_page_id=$id"; } sub GET { ## real network request, REST::Client or whatever return $JSON{ $_[0] }; } sub fromPairs { @PAIRS = ( [ "https://127.0.0.1/wapi/v2.7.3/record:a?_max_results=2&_paging=1&_return_as_object=1&_return_fields=name&name~=test1.com", { next_page_id => "789c5590...4efc1732", result => [ { _ref => "record:a/ZG5zLmJpbmRfYSQuXY29tLn3DEwLjEuMC4x:a1.test1.com/default", name => "a1.test1.com", }, { _ref => "record:a/ZG5zLmJpbmRLnRlc3QxLGE1LDEwLjEuMC41:a5.test1.com/default", name => "a5.test1.com", }, ], }, ], [ "https://127.0.0.1/wapi/v2.7.3/record:a?_page_id=789c5590...4efc1732", { next_page_id => "789c5590...3e113c3d4d", result => [ { _ref => "record:a/ZG5zLmJpbmRfYSQc3QxLGE0LDEwLjEuMC40:a4.test1.com/default", name => "a4.test1.com", }, { _ref => "record:a/ZG5zLmuY29tLnRlc3QxLGEzLDEwLjEuMC4z:a3.test1.com/default", name => "a3.test1.com", }, ], }, ], [ "https://127.0.0.1/wapi/v2.7.3/record:a?_page_id=789c5590...3e113c3d4d", { result => [ { _ref => "record:a/ZG5zLmJpbmRfYSQuX2RLGEyLDEwLjEuMC4y:a2.test1.com/default", name => "a2.test1.com", }, ], }, ], ); return map { @$_ } @PAIRS; }