# Parameter: CGI object # Return: hash (a map) of the CGI variables sub getHash($) { my($q) = @_; return $q->Vars; } sub exampleUse() { my($q, %h, $thing); $q = new CGI; %h = getHash($q); $thing = $h{thing}; } #--------------------------------------------- # Parameter: CGI object # Return: names of all the CGI parameters sub getNames($) { my($q) = @_; return $q->param; } sub exampleUse() { my($q, $i); $q = new CGI; for $i (getNames($q)) { print "$i\n"; } } #--------------------------------------------- # Parameter: CGI object # Return: boolean: is an SSL connection sub isSSL($) { my($q) = @_; my($url); return $q->url() =~ m/^https:/i; } sub exampleUse() { my($q); $q = new CGI; if (!isSSL($q)) { print "Not secure\n"; return; } } #--------------------------------------------- # Parameter: CGI object # Action: redirect the user to https if they aren't already sub ensureSSL($) { my($q) = @_; if (!isSSL($q)) { my($url); $url = $q->url(); $url =~ s/http:/https:/; print qq(<meta http-equiv="refresh" content="0; url=$url">); return 0; } return 1; } sub exampleUse() { my($q); $q = new CGI; return if !ensureSSL($q); } #--------------------------------------------- # Parameter: CGI object # Return: Does the same as CGI::query_string() but with &'s sub getQueryString($) { my($q) = @_; my($i, $qs); for $i (getNames($q)) { if ($qs) { $qs .= '&'; } $qs .= $i . '=' . $q->param($i); } return $qs; }
Programming Tips - perl: get a hash of the perl CGI variables and other small useful CGI functions
Date: 2010feb11
Update: 2025jul2
Language: perl
Platform: web
Keywords: Common Gateway Interface
Q. perl: get a hash of the perl CGI variables and other small useful CGI functions
A. Here's how you do that and some other handy perl CGI functions.