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

Hi, I'm calling an API internally from a script and need to grep few values from the output of API,when the call is being made to an API,it could take some amount of time to get the output,but my code was trying to grep values from the output file before the output is generated.Is there any way to do further processing only after output is generated?

while ( @accumRow = $getAccums->fetchrow_array() ) { push(@accums,@accumRow); ($currAccumId) = @accumRow; print ("currAccumId = @accumRow \n"); $getSbscrpidFrmAccum->bind_param(1,$currAcct); $getSbscrpidFrmAccum->bind_param(2,$currAccumId); $getSbscrpidFrmAccum->execute()|| logDBIError(" execut +e failed for fetching sbscrpId from Accum of the Account $currAcct + "); while (@sbscrpRow = $getSbscrpidFrmAccum->fetchrow_arr +ay() ) { ( $sbscrp_id ) = @sbscrpRow; } $xmlFile = "GetUsgSummary_" . $currAcct ."_".$currAccu +mId.".xml"; createXmlForUpd( $xmlFile, $currAcct,$sbscrp_id ); $retValue = invokeServer( $currAcct , $xmlFile,$currAc +cumId ); if ( $retValue == ERROR ) { logMessage( "API call failed for acct $currAcct wi +th accumId @accumRow of subscriber $sbscrp_id \n"); } else { open(FILE,"<","$retValue"); my $accumId = 0; my @inclUnits = (); my ($inclUnits); my ($inclUnitsUsed); my ($shared); while(<FILE>) { if($_ =~ m/accumId="$currAccumId"/) { @inclUnits = split(/ /,$_); ($inclUnits) = grep(/inclUnits=/, @inclUni +ts); $inclUnits =~ s/inclUnits="([^"]+)"/$1/g; ($inclUnitsUsed) = grep(/inclUnitsUsed=/, +@inclUnits); $inclUnitsUsed =~ s/inclUnitsUsed="([^"]+) +"/$1/g; ($shared) = grep(/shared=/, @inclUnits); $shared =~ s/shared="([^"]+)"/$1/g; logMessage( "accumId=$currAccumId : $inclU +nits : $inclUnitsUsed : $shared\n"); } } close(FILE); } }

Replies are listed 'Best First'.
Re: Wait for the response of API
by CountZero (Bishop) on Dec 24, 2012 at 11:16 UTC
    Do you mean that the API-call returns before all the data is ready and available?

    Perhaps the API has another call that tells you when the data is "ready"?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Yes...The API call is returning before the data is ready. Is there anything that tells if the data is ready as you mentioned ?
        I have no idea. It will depend on the API, so you should read its docs.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: Wait for the response of API
by NetWallah (Canon) on Dec 24, 2012 at 22:40 UTC
    Are you talking about the "fetchrow_array()" statement of the DBI API ?

    If so - that is a well tested path, and will block (not return) until data is available.

    At what line do you think data was expected and not found ?

    What diagnostic method did you use ?

    What diagnostic or error message supports your claim ? Have you tried to reproduce the issue with a minimal perl script, to test your claim ?

    Oh yes - Seasons Greetings !

                 "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius