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

When the user first comes to the page the first "IF" with x1a.html page loads, it makes the call to the "got_send()" no problem.
Once the user is on that first page they see buttons where they can click on it to get the other results, that's how the other "IFs" get called/executed.
As you can see this sample program make calls and gets back some kind of XML, it is not XML it just looks like XML.
In this XML its where I go through to get the values I need and to populate a html page for the user.
My issue is that I was asked to combined all the 3 calls at once and load as the user arrives on the first page.
How could I do that since the XML from each call comes in using the same XML and depending on a call the values in any of the tag numbers in the XML will be different, lets say if I am expecting the value from tag <7> on the first call to be TOM, how could I call the second "IF", since it will be empty or not and on the third one it could be "MARY" or any other value.
Is this a case where dimensional arrays come to play and than it will not matter if I am making 2, 3 or more calls to got_send() sub?
I'll probably have to hard code some of the parameters to make the calls since they are necessary to the got_send() sub.
I am trapped, any suggestions please!!


Here is the sample code I have to relate to my question.

if($page eq "x1a.html") { $re_type = "a2"; $Q = ($prod) ? 'a0' : 'a0'; $send=$sec.$account.$year.$view; } if($page eq "x22.html") { $re_type = "x2"; $Q = ($prod) ? 'p0' : 'w0'; $send=$sec.$account.$year.$view; } if ($page eq "y11.html") { $req_type = "y1"; $Q = ($prod) ? 'p0' : 'w0'; $send=$sec.$account.$year.$view.$action; } if ($send) { got_send(); } sub got_send { my $socket; my $x = ''; my @read = (); my $host = ($prod) ? 'my.prod.com' : 'mytest.box.com'; my $port; # Alternate depending on the time (in seconds) ($now[5] % 2) ? ($port = 6000) : ($port = 6100); $AoH{300} = sprintf "%2s(%d) %s", $QPro, ($port/100), $send; $socket = IO::Socket::INET->new( "$host:$port" ) or &error; print $socket "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n <!DOCTYPE ACCOUNTS REF NAMES>\n <ACCOUNTS>\n <Type>$re_type<Type>\n <Id>$send</Id>\n <Profile>$Q</Profile> </ACCOUNTS>\n \n"; $x = <$socket>; close(Server); $x=~s/</\n</g; @read = split(/\n/, $x); foreach my $y (@read) { if (!$y) { $y = '<0>' } # Prevent uninitialzed message on $1 $y=~/<(.*?)>(.*)/; if($1 eq "7"){$name = $2;} #more code here.... last if ($x =~ /^[\s\x00]*$/); } } #End of sub


Returned result from first call
<1>66543 <2>2009 <3>TY <4>- <6> <7>TOM <8>2 - DUNDAS <8>100 - MAIN <9>ONT 00001 <10> <11>000-000-0000 <12>


I need some way to return multiple results and be able to differentiate the tag values coming in in the same tag number to display:
<1>66543 <2>2009 <3>TY <4>- <6> <7>TOM <8>2 - DUNDAS <8>100 - MAIN <9>ONT 00001 <10> <11>000-000-0000 <12> <1>12345 <2>1999 <3>PP <4>XLX - TEST <6>Comm <7> <8>1 BLVD <9>MA 00001 <10>CITI, INC. <11>000-000-0000 <12> <1>8887 <2>2000 <3> <4>TEST <6> <7>JOE <7>MARY <8>100 MAIN <9>CT 02221 <10>BH, INC. <11>871-000-0000 <12>YEAR


Thanks for the help!!!

Replies are listed 'Best First'.
Re: Combining calls to a subroutine issue.
by graff (Chancellor) on Apr 14, 2010 at 16:24 UTC
    My first impulse would be to call that subroutine 3 times in one run:
    if ( $page eq 'x1a.html' ) { $re_type = "a2"; $Q = ($prod) ? 'a0' : 'a0'; $send=$sec.$account.$year.$view; got_send(); # first "page" $re_type = "x2"; $Q = ($prod) ? 'p0' : 'w0'; $send=$sec.$account.$year.$view; got_send(); # second "page" $req_type = "y1"; $Q = ($prod) ? 'p0' : 'w0'; $send=$sec.$account.$year.$view.$action; got_send(); # third "page" }
    (That's the "quickest" thing, though some rearrangement -- possibly using a loop over a data structure of call parameters -- would be worthwhile.)

    But I suppose there might be something you haven't shown us in your "got_send()" sub that makes this impractical?

    The way out would be to refactor the "got_send" sub to make it more modular: pass parameters to it on each call, instead of having it refer to global variables; have it return a string (or something), which the caller will know what to do with, based on the caller's knowledge of the context for each call, rather than trying to do everything inside of "got_send()".

    Perhaps I'm misunderstanding the question? Or maybe you've left out some useful details...

      The trick part on this is that in the html file there are tags like, <!-- 2 --> and so forth that gets substituted by the value of the same tag number on this XML, and if the first call comes in with tag <2> equal something, how to get the second call having a different value on the same tag <2> and display on the same html page?
        Your description of the problem is too confusing (confused). What/where is this "html file" that has these comment tags like "<!-- 2 -- > and so forth"? Why are you using a format that is not really XML or HTML (or XHTML)? What does a client/user supply? What does the client/user want in response?

        Get down to first principles. Break things up into individual, modular components that do specific, simple parts of the task. Plug these components into subroutine calls in loops as needed, and make the code look like a natural-language description of the task. (I gather that English is not your first language, whereas it tends to be the first natural language for perl; this might be a problem, but sticking to simple "command-style" statements ("do this, then do that, then...") will serve you well enough.

        Separate the creation/management of data structure from the presentation of data, and set up a main-level controller that uses those two sets of functions ("model vs. view vs. control"). Based on what you've shown so far, I don't think there's anything else I can offer.