in reply to CGI Action call

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 ); ... }

Replies are listed 'Best First'.
Re^2: CGI Action call
by tultalk (Monk) on Mar 12, 2018 at 13:00 UTC

    Hi Thanks

    This: Let your code call the sub for data and let the sub return the data only. confuses me.

    code called under if (action = xxx) can call a sub which can produce the the desired result but and return(data) it but isn't that returned to the action call and since that is not a sub, won't it fail in the same way as trying to return it directly from the action called code?