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

Very brief query I can't seem to find the answer to - Whenever I print to a local file I use the proper filehandle to make sure the text explicitly ends up where it belongs(using the filehandle assigned to a file's localpath). What is the correct explicit filehandle to use for CGI HTML output please?
  • Comment on What filehandle should be used for HTML prints via CGI??

Replies are listed 'Best First'.
Re: What filehandle should be used for HTML prints via CGI??
by jhourcle (Prior) on Jan 12, 2007 at 17:57 UTC
      Thank you, I thought that might be the case but I wasn't sure. I'll take a look at the various links offered. I have encountered STDOUT but I guess I don't fully understand how it works, since in other occasions STDOUT seems to apply to command line output. I take it that STDOUT is dynamic and if the script is being called via CGI it knows to output to CGI and if being called via command line knows to output to command line?
        This is a reasonable thing to wonder about. Here is a high-level summary of what generally happens: a CGI program will be spawned off by the HTTP server as a separate process which is expected to send its results (valid web content, HTML etc, if all went well) to stdout. The HTTP server collects that, does a bit of post-processing on it and then slings it all back to the source of the original HTTP request.

        Erm, "output to command line" . . . where to start.

        STDOUT is, as the name would imply, the, erm, standard output filehandle. Processes get 3 handles: STDIN, STDOUT, and STDERR. What those actually are hooked up to depends on how whatever forks the process hooks them up to. In the interactive case where you're running something from a shell, they'll all usually be hooked up to a TTY device of some sort. When run as a CGI, the webserver will provide a STDOUT which goes back (after some processing in most cases) to the browser making the request.

        In Perl the default filehandle for print will be STDOUT unless you use select (or reopen STDOUT somewhere else). Any output to that handle will go to whatever that file descriptor's hooked to.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: What filehandle should be used for HTML prints via CGI??
by virtualsue (Vicar) on Jan 12, 2007 at 18:03 UTC
    No explicit file handle is necessary, generally. It's worth having a look at the module documentation for CGI. There is more info here: CGI.pm
Re: What filehandle should be used for HTML prints via CGI??
by ptum (Priest) on Jan 12, 2007 at 18:08 UTC

    A CGI program in Perl generally prints a header and then simply prints the HTML to STDOUT, as has been noted. A simple (untested) "Hello World!" example follows:

    use strict; use CGI; print header(); print start_html(); print h1("Hello, World!"); print end_html();

    As noted, no explicit file handle is needed, since your script is simply printing to STDOUT.

    Please do follow the advice you've been given and read up on CGI.

    Note also that your script needs to be executable by the user under which your webserver runs. This generally means you need to chmod your script to 755 if running under some form of Unix.