NAME

SPRAGL::Cgi_read - Barebones CGI.

VERSION

0.71

SYNOPSIS

use SPRAGL::Cgi_read;

# Reading a header.
my $greet = "Buon Giorno" if header("Accept-Language") =~ m/\b it \b/x;

# Reading a parameter value.
my $id = param->{ID}; # Parameter names are case sensitive.

# Multi value parameters.
for my ($i,$val) ( each param_all("files")->@* ) {
    write_to_log "processing ".meta_all("files")->[$i]->{filename};
    do_something( ${val} );
    };

IDIOMS

param->{p}          # first value of parameter p
param_all('p')->@*  # all values assigned to parameter p
param_all('p')->[2] # the third value assigned to parameter p
meta->{p}           # metadata for first value of parameter p
meta_all('p')->@*   # metadata for every value assigned to parameter p
meta_all('p')->[2]  # the metadata for the third value assigned to parameter p
keys param->%*      # list of all parameter names sent in the request

DESCRIPTION

Barebones module for handling CGI requests. It is applicative and lightweight, and has only a few dependencies.

CGI is simple and quick to code for, even though it is not so performant or fashionable. It nevertheless is handy when making quick and dirty web services that are not going to see a lot of load. HTTP Routing is handled by the file system. Adding or removing functionality is easy and orthogonal, like playing with Lego bricks.

For decades CGI.pm has been the gold standard for doing CGI with Perl. It is a big featureful module, and in many cases that is what is needed. But in other cases you just need a simple basic module.

SPRAGL::Cgi_read.pm exists so you dont have to use CGI.pm.

The SPRAGL::Cgi_read module follows Postels Law (be conservative in what you do, be liberal in what you accept). So in case a request is a bit off, the module will not right out fail, but will try to get fairly intelligible data out of it.

OPTIMIZATIONS

The SPRAGL::Cgi_read module optimizes ressources based on the imports of the CGI script. This works without further ado for normal scripts. But if the script references a method or variable using the SPRAGL::Cgi_read namespace, then it should specify so in its import statement. This is done by prefixing "::" to the import. For example

use SPRAGL::Cgi_read qw(param $uri ::meta ::$method);
use SPRAGL::Cgi_reply;

my $custname = param->{name};
my $custmeta = SPRAGL::Cgi_read::meta(name);
reply "URI was ".$uri." and method was ".$SPRAGL::Cgi_read::method;

If these imports are not specified, calls and lookups might give the wrong values.

COMMAND LINE

With SPRAGL::Cgi_read you can run your CGI scripts from the commandline. This is convenient when debugging or testing. The script will be run as if a GET request with no data started it. But by using options, you can change that.

-c

Emulate that the request was a POST request. Send the content to it on STDIN.

-H <string>

Emulate that the request had the given header field.

-q <string>

Emulate that the request had the given querystring.

Example:

perl index.pl -H "Referer: https://news.ycombinator.com/" -q "?tag=mars"

FUNCTIONS AND VARIABLES

Loaded by default: param, meta, param_all, meta_all, header

Loaded on demand: $method, $uri, %header, $content, $cgi_mode

DEPENDENCIES

Encode

List::Util

Scalar::Util

KNOWN ISSUES

Limited testing. Should work with all major web servers.

TODO

SEE ALSO

SPRAGL::Cgi_reply

CGI

LICENSE & COPYRIGHT

(c) 2022-2023 Bjørn Hee

Licensed under the Apache License, version 2.0

https://www.apache.org/licenses/LICENSE-2.0.txt