Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

CGI Action call

by tultalk (Monk)
on Mar 11, 2018 at 17:03 UTC ( [id://1210668]=perlquestion: print w/replies, xml ) Need Help??

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

Hi: Philosophical question. I want to return data from call using cgi action ..... Not a "sub" so get error when trying to return data. Can't find any info directly addressing this issue. Don't want to have to create another pm and export functions. Can't export from cgi module. Thanks

Replies are listed 'Best First'.
Re: CGI Action call
by Perlbotics (Archbishop) on Mar 11, 2018 at 18:52 UTC

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

      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?

Re: CGI Action call
by Marshall (Canon) on Mar 11, 2018 at 21:26 UTC
    I am also flummoxed here. I just don't understand the question.

    If you call a CGI function, it will produce a result to the calling code.
    Can you show some code to give an example of the problem?

      Hi: Thanks

      I am not calling a "function" (sub something() . I and calling ?action=four&in=6 and that labeled block of code cannot return anything (error) as it is not a sub. Seems a shortcoming to me since these action= in this case are really subs just called in a different manner.

      Further, how do you call a sub directly using the MyCGI.cgi call?

        ?action=four&in=6 is not a labeled block of code in your CGI application. Those are parameters passed to the CGI. Your application could "handle" them by running an inline block of code, or by calling a function, or by completely ignoring them. You haven't provided an SSCCE, so we cannot know how you chose to handle them.

        Speaking on the philosophical level, to "return" anything in a CGI environment, you print the headers and the "anything" to STDOUT: If your CGI is supposed to output HTML, then you print headers followed by HTML to STDOUT; if your CGI is supposed to output JSON, then print the headers followed by the JSON text; if your CGI is supposed to output an image, your script needs to print the headers then the (possibly-encoded) binary for the image; if your CGI is supposed to output an error message, your script needs to print headers then the error message. For the error condition, if you want the message to just be part of the normal response, then included it as part of the already-existing message: for example, give some normal HTML, but include a <p class=error>error message goes here</p> or similar at an appropriate location in the HTML output; or, in the case of an image, include your normal pixels for the image, but then overwrite some of the pixels with your error indicator, before printing the image bytes to STDOUT.

        If this is not helpful, you are going to have to give an SSCCE and a better description of what your current code outputs, and how you'd like to modify that output under the conditions you will need to specify.

        Are you talking about returning data from one cgi script 'called' from another cgi script ?

        poj
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1210668]
Approved by Marshall
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-19 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found