Philosophical answer: You should separate the model from its representation. → MVC
Assuming, your CGI returns HTML, it is not a good idea to scrape the information back.
Let your code call the sub for data and let the sub return the data only. Let your CGI-sub
call the same sub and let it render the result in a specific format that is returned via HTTP.
IF your CGI returns data in a format that is parsable (i.e. JSON or YAML or XML or ...),
then there's the alternative to call the CGI via HTTP (LWP::UserAgent) and deserialise
the result. That's not too performant, though and requires additional error-handling. On the pro-side,
data can be retrieved from a remote server.
Hope that helps. I might completely misunderstood your question, though ;-)
Update: Clarification in form of code fragments:
#-- the sub that computes the data (@result) only from it's parameters
+ (@pars)
# Using @result as an array is just for simplicity. It could be hash
+-ref or an object
# or whatever your expected result fits into...
#
sub get_result { my (@pars) = @_; ..... return @result; }
#-- here's your CGI handler
#
sub cgi_handler {
...
my (@pars) = ... #-- get params from HTTP request
my (@result) = get_result( @pars );
... #-- render HTTP-response from your
+ @result
}
#-- somewhere else in your code... where your original question came f
+rom...
# Instead of a regular function, an object method or a class method
+or an AUTOSUB etc.
# could be called.
#
sub somewhere_else {
#-- no worry to call cgi_handler(), just call
@result = get_result( @pars );
...
}
|