Perl CGI  «Prev  Next»

Lesson 13

Perl interfaces Conclusion

This module discussed Perl interfaces with CGI and the Web browser. You learned techniques for maintaining the state of the user using hidden fields with the state-machine model and how to use cookies to keep track of users between sessions. In the next module, you will learn how Perl uses streams and pipes to read and write to files and other programs. You will also write a program to send email from a form, instead of using a mailto: URL.
  • Common Gateway Interface (CGI)
    Common Gateway Interface (CGI) is a standard method used to generate dynamic content on web pages and web applications.
    CGI, when implemented on a web server, provides an interface between the web server and programs that generate the web content. These programs are known as CGI scripts or simply CGIs; they are usually written in a scripting language, but can be written in any programming language.
    CGI or Common Gateway Interface is what make the forms work and the counters count. There are many languages that could function as a CGI language like Perl, C, C++, Tcl, or a Unix Shell Script. By this time, you have learned about Perl and are wondering what is the relation between CGI and Perl.
    CGI scripts can be written in a variety of computer languages, but Perl is without dispute the most widely used language for CGI scripting. Now let us see how to use Perl with Forms and other HTML elements. When you know Perl, you can do things like create small programs and read the programs of others without any additional knowledge. If you want to learn how to use Perl as a CGI language, continue reading.

Perl Interfaces with CGI and the Web Browser

  1. Perl Interfaces with CGI and the Web Browser
    • CGI Basics: CGI acts as the interface between a web server and Raku scripts. When a browser sends an HTTP request to a server, the CGI script processes the request and generates an appropriate response (HTML, JSON, etc.) back to the browser.
    • Raku Modules for CGI: Although Perl 6 (Raku) does not have an out-of-the-box CGI.pm equivalent like Perl 5, there are libraries that can be used for CGI-like tasks. Examples include:
      • `HTTP::Server::Simple`: A module to create lightweight HTTP servers in Raku.
      • `Cro`: A robust web framework for creating HTTP services in Raku, which can handle routing, input, and output.
    • Web Interaction: A CGI script in Raku parses input from HTTP requests (GET or POST), processes it, and outputs responses dynamically:
              # Basic CGI-style response in Raku
              use HTTP::Server::Simple;
        
              my $server = HTTP::Server::Simple.new(:port(8080), :host('localhost'));
              $server.handler(sub ($req) {
                my $name = $req.params // 'World';
                return "Content-Type: text/html\n\nHello, $name!";
              });
        
              $server.run;
    • Explanation:
      • The server listens on a given port and host.
      • `$req.params` retrieves query parameters passed from the browser (e.g., `?name=John`).
      • Outputs an HTML response.
  2. Maintaining State with Hidden Fields and State-Machine Model
    • Hidden Fields: Hidden fields are part of HTML forms and are not visible to users but are transmitted with the form submission. They store state information for the server to reconstruct the session.
    • Example in Raku:
              
    • The `state` hidden field carries the current state (`step1`) to the server. When the form is submitted:
      • The server inspects the hidden `state` field.
      • Determines the next step in the state-machine model (e.g., `step2`).
    • Raku Backend Example:
              # State Machine Example
              my $server = HTTP::Server::Simple.new(:port(8080));
              $server.handler(sub ($req) {
                my $state = $req.params // 'start';
                
                if $state eq 'start' {
                  return qq:to/HTML/;
                  
      } elsif $state eq 'step1' { return "Welcome, {$req.params}. You are now at Step 1."; } }); $server.run;
    • State-Machine Process:
      • The server determines the state (e.g., `start`, `step1`).
      • Based on the current state, it returns a specific response.
      • Hidden fields help carry state information across multiple pages.

  3. Summary
    1. Perl 6 (Raku) interacts with CGI and the browser using frameworks like HTTP::Server::Simple or Cro, allowing dynamic content generation.
    2. The state-machine model uses hidden fields to pass state between pages, enabling multi-step workflows.
    3. Cookies allow servers to persist user data across sessions by storing information on the client side and retrieving it during subsequent requests.
    This combination enables robust web applications in Raku while maintaining user-specific data and workflows.

SEMrush Software