The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

CGI::Info - Information about the CGI environment

VERSION

Version 0.20

SYNOPSIS

All too often Perl programs have information such as the script's name hard-coded into their source. Generally speaking, hard-coding is bad style since it can make programs difficult to read and it reduces readability and portability. CGI::Info attempts to remove that.

Furthermore, to aid script debugging, CGI::Info attempts to do sensible things when you're not running the program in a CGI environment.

    use CGI::Info;
    my $info = CGI::Info->new();
    # ...

SUBROUTINES/METHODS

new

Creates a CGI::Info object.

script_name

Returns the name of the CGI script. This is useful for POSTing, thus avoiding putting hardcoded paths into forms

        use CGI::Info;

        my $info = CGI::Info->new();
        my $script_name = $info->script_name();
        # ...
        print "<form method=\"POST\" action=$script_name name=\"my_form\">\n";

script_path

Finds the full path name of the script.

        use CGI::Info;

        my $info = CGI::Info->new();
        my $fullname = $info->script_path();
        my @statb = stat($fullname);

        if(@statb) {
                my $mtime = localtime $statb[9];
                print "Last-Modified: $mtime\n";
                # TODO: only for HTTP/1.1 connections
                # $etag = Digest::MD5::md5_hex($html);
                printf "ETag: \"%x\"\n", $statb[9];
        }

script_dir

Returns the file system directory containing the script.

        use CGI::Info;
        use File::Spec;

        my $info = CGI::Info->new();
        my $dir = $info->script_dir();

        print 'HTML files are normally stored in ' .  $info->script_dir() . '/' . File::Spec->updir() . "\n";

host_name

Return the hostname of the current web server, according to CGI. If the name can't be determined from the web server, the system's hostname is used as a fall back. This may not be the same as the machine that the CGI script is running on, some ISPs and other sites run scripts on different machines from those delivering static content. There is a good chance that this will be domain_name() prepended with either 'www' or 'cgi'.

        use CGI::Info;

        my $info = CGI::Info->new();
        my $host_name = $info->host_name();
        my $protcol = $info->protocol();
        # ...
        print "Thank you for visiting our <A HREF=\"$protocol://$host_name\">Website!</A>";

domain_name

Domain_name is the name of the controlling domain for this website. Usually it will be similar to host_name, but will lack the http:// prefix.

cgi_host_url

Return the URL of the machine running the CGI script.

params

Returns a reference to a hash list of the CGI arguments.

If we're not in a CGI environment (e.g. the script is being tested) then the program's command line arguments are used, if there are no command line arguments then they are read from stdin as a list of key=value lines.

Returns undef if the parameters can't be determined.

If an argument is given twice or more, then the values are put in a comma separated list.

The returned hash value can be passed into CGI::Untaint.

Takes one parameter, expect. This is a reference to a list of arguments that we expect to see and pass on. Arguments not in the list are silently ignored. The expect list can also be passed to the constructor.

        use CGI::Info;
        use CGI::Untaint;
        # ...
        my $info = CGI::Info->new();
        my %params;
        if($info->params()) {
                %params = %{$info->params()};
        }
        # ...
        foreach(keys %params) {
                print "$_ => $params{$_}\n";
        }
        my $u = CGI::Untaint->new(%params);

        use CGI::Info;
        # ...
        my $info = CGI::Info->new();
        my @allowed = ('foo', 'bar');
        my $paramsref = $info->params(expect => \@allowed);

is_mobile

Returns a boolean if the website is being viewed on a mobile device such as a smart-phone.

as_string

Returns the parameters as a string, which is useful for debugging or generating keys for a cache.

protocol

Returns the connection protocol, presumably 'http' or 'https', or undef if it can't be determined.

tmpdir

Returns the name of a directory that you can use to create temporary files in.

The routine is preferable to "tmpdir" in File::Spec since CGI programs are often running on shared servers. Having said that, tmpdir will fall back to File::Spec->tmpdir() if it can't find somewhere better.

If the parameter 'default' is given, then use that directory as a fall-back rather than the value in File::Spec->tmpdir().

        use CGI::Info;

        my $info = CGI::Info->new();
        my $dir = $iinfo->tmpdir(default => '/var/tmp');

is_robot

Is the visitor a real person or a robot?

        use CGI::Info;

        my $info = CGI::Info->new();
        unless($info->is_robot()) {
          # update site visitor statistics
        }

AUTHOR

Nigel Horne, <njh at bandsman.co.uk>

BUGS

Please report any bugs or feature requests to bug-cgi-info at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Info. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SEE ALSO

HTTP::BrowserDetect

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc CGI::Info

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2010-2012 Nigel Horne.

This program is released under the following licence: GPL