| 1 |
NAME |
| 2 |
CGI::SSI - Use SSI from CGI scripts |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
# autotie STDOUT or any other open filehandle |
| 6 |
|
| 7 |
use CGI::SSI (autotie => 'STDOUT'); |
| 8 |
|
| 9 |
print $shtml; # browser sees resulting HTML |
| 10 |
|
| 11 |
# or tie it yourself to any open filehandle |
| 12 |
|
| 13 |
use CGI::SSI; |
| 14 |
|
| 15 |
open(FILE,'+>'.$html_file) or die $!; |
| 16 |
$ssi = tie(*FILE, 'CGI::SSI', filehandle => 'FILE'); |
| 17 |
print FILE $shtml; # HTML arrives in the file |
| 18 |
|
| 19 |
# or use the object-oriented interface |
| 20 |
|
| 21 |
use CGI::SSI; |
| 22 |
|
| 23 |
$ssi = CGI::SSI->new(); |
| 24 |
|
| 25 |
$ssi->if('"$varname" =~ /^foo/'); |
| 26 |
$html .= $ssi->process($shtml); |
| 27 |
$ssi->else(); |
| 28 |
$html .= $ssi->include(file => $filename); |
| 29 |
$ssi->endif(); |
| 30 |
|
| 31 |
print $ssi->exec(cgi => $url); |
| 32 |
print $ssi->flastmod(file => $filename); |
| 33 |
|
| 34 |
# |
| 35 |
# or roll your own favorite flavor of SSI |
| 36 |
# |
| 37 |
|
| 38 |
package CGI::SSI::MySSI; |
| 39 |
use CGI::SSI; |
| 40 |
@CGI::SSI::MySSI::ISA = qw(CGI::SSI); |
| 41 |
|
| 42 |
sub include { |
| 43 |
my($self,$type,$file_or_url) = @_; |
| 44 |
# my idea of include goes something like this... |
| 45 |
return $html; |
| 46 |
} |
| 47 |
1; |
| 48 |
__END__ |
| 49 |
|
| 50 |
# |
| 51 |
# or use .htaccess to include all files in a dir |
| 52 |
# |
| 53 |
|
| 54 |
# in .htaccess |
| 55 |
Action cgi-ssi /cgi-bin/ssi/process.cgi |
| 56 |
<FilesMatch "\.shtml"> |
| 57 |
SetHandler cgi-ssi |
| 58 |
</FilesMatch> |
| 59 |
|
| 60 |
# in /cgi-bin/ssi/process.cgi |
| 61 |
#!/usr/local/bin/perl |
| 62 |
use CGI::SSI; |
| 63 |
CGI::SSI->handler(); |
| 64 |
__END__ |
| 65 |
|
| 66 |
DESCRIPTION |
| 67 |
CGI::SSI is meant to be used as an easy way to filter shtml through CGI |
| 68 |
scripts in a loose imitation of Apache's mod_include. If you're using |
| 69 |
Apache, you may want to use either mod_include or the Apache::SSI module |
| 70 |
instead of CGI::SSI. Limitations in a CGI script's knowledge of how the |
| 71 |
server behaves make some SSI directives impossible to imitate from a CGI |
| 72 |
script. |
| 73 |
|
| 74 |
Most of the time, you'll simply want to filter shtml through STDOUT or |
| 75 |
some other open filehandle. "autotie" is available for STDOUT, but in |
| 76 |
general, you'll want to tie other filehandles yourself: |
| 77 |
|
| 78 |
$ssi = tie(*FH, 'CGI::SSI', filehandle => 'FH'); |
| 79 |
print FH $shtml; |
| 80 |
|
| 81 |
Note that you'll need to pass the name of the filehandle to "tie()" as a |
| 82 |
named parameter. Other named parameters are possible, as detailed below. |
| 83 |
These parameters are the same as those passed to the "new()" method. |
| 84 |
However, "new()" will not tie a filehandle for you. |
| 85 |
|
| 86 |
CGI::SSI has it's own flavor of SSI. Test expressions are Perlish. You |
| 87 |
may create and use multiple CGI::SSI objects; they will not step on each |
| 88 |
others' variables. |
| 89 |
|
| 90 |
Object-Oriented methods use the same general format so as to imitate SSI |
| 91 |
directives: |
| 92 |
|
| 93 |
<!--#include virtual="/foo/bar.footer" --> |
| 94 |
|
| 95 |
would be |
| 96 |
|
| 97 |
$ssi->include(virtual => '/foo/bar.footer'); |
| 98 |
|
| 99 |
likewise, |
| 100 |
|
| 101 |
<!--#exec cgi="/cgi-bin/foo.cgi" --> |
| 102 |
|
| 103 |
would be |
| 104 |
|
| 105 |
$ssi->exec(cgi => '/cgi-bin/foo.cgi'); |
| 106 |
|
| 107 |
Usually, if there's no chance for ambiguity, the first argument may be |
| 108 |
left out: |
| 109 |
|
| 110 |
<!--#echo var="var_name" --> |
| 111 |
|
| 112 |
could be either |
| 113 |
|
| 114 |
$ssi->echo(var => 'var_name'); |
| 115 |
|
| 116 |
or |
| 117 |
|
| 118 |
$ssi->echo('var_name'); |
| 119 |
|
| 120 |
Likewise, |
| 121 |
|
| 122 |
$ssi->set(var => $varname, value => $value) |
| 123 |
|
| 124 |
is the same as |
| 125 |
|
| 126 |
$ssi->set($varname => $value) |
| 127 |
|
| 128 |
$ssi->new([%args]) |
| 129 |
Creates a new CGI::SSI object. The following are valid (optional) |
| 130 |
arguments: |
| 131 |
|
| 132 |
DOCUMENT_URI => $doc_uri, |
| 133 |
DOCUMENT_NAME => $doc_name, |
| 134 |
DOCUMENT_ROOT => $doc_root, |
| 135 |
errmsg => $oops, |
| 136 |
sizefmt => ('bytes' || 'abbrev'), |
| 137 |
timefmt => $time_fmt, |
| 138 |
MAX_RECURSIONS => $default_100, # when to stop infinite loops w/ error msg |
| 139 |
COOKIE_JAR => HTTP::Cookies->new, |
| 140 |
|
| 141 |
$ssi->config($type, $arg) |
| 142 |
$type is either 'sizefmt', 'timefmt', or 'errmsg'. $arg is similar |
| 143 |
to those of the SSI "spec", referenced below. |
| 144 |
|
| 145 |
$ssi->set($varname => $value) |
| 146 |
Sets variables internal to the CGI::SSI object. (Not to be confused |
| 147 |
with the normal variables your script uses!) These variables may be |
| 148 |
used in test expressions, and retreived using $ssi->echo($varname). |
| 149 |
These variables also will not be available in external, included |
| 150 |
resources. |
| 151 |
|
| 152 |
$ssi->echo($varname) |
| 153 |
Returns the value of the variable named $varname. Such variables may |
| 154 |
be set manually using the "set()" method. There are also several |
| 155 |
built-in variables: |
| 156 |
|
| 157 |
DOCUMENT_URI - the URI of this document |
| 158 |
DOCUMENT_NAME - the name of the current document |
| 159 |
DATE_GMT - the same as 'gmtime' |
| 160 |
DATE_LOCAL - the same as 'localtime' |
| 161 |
LAST_MODIFIED - the last time this script was modified |
| 162 |
|
| 163 |
$ssi->exec($type, $arg) |
| 164 |
$type is either 'cmd' or 'cgi'. $arg is similar to the SSI "spec" |
| 165 |
(see below). |
| 166 |
|
| 167 |
$ssi->include($type, $arg) |
| 168 |
Similar to "exec", but "virtual" and "file" are the two valid types. |
| 169 |
SSI variables will not be available outside of your CGI::SSI object, |
| 170 |
regardless of whether the virtual resource is on the local system or |
| 171 |
a remote system. |
| 172 |
|
| 173 |
$ssi->flastmod($type, $filename) |
| 174 |
Similar to "include". |
| 175 |
|
| 176 |
$ssi->fsize($type, $filename) |
| 177 |
Same as "flastmod". |
| 178 |
|
| 179 |
$ssi->printenv |
| 180 |
Returns the environment similar to Apache's mod_include. |
| 181 |
|
| 182 |
$ssi->cookie_jar([$jar]) |
| 183 |
Returns the currently-used HTTP::Cookies object. You may optionally |
| 184 |
pass in a new HTTP::Cookies object. The jar is used for web requests |
| 185 |
in exec cgi and include virtual directives. |
| 186 |
|
| 187 |
FLOW-CONTROL METHODS |
| 188 |
The following methods may be used to test expressions. During a "block" |
| 189 |
where the test $expr is false, nothing will be returned (or printed, if |
| 190 |
tied). |
| 191 |
|
| 192 |
$ssi->if($expr) |
| 193 |
The expr can be anything Perl, but care should be taken. This causes |
| 194 |
problems: |
| 195 |
|
| 196 |
$ssi->set(varname => "foo"); |
| 197 |
<!--#if expr="'\$varname' =~ /^foo$/" -->ok<!--#endif --> |
| 198 |
|
| 199 |
The $varname is expanded as you would expect. (We escape it so as to |
| 200 |
use the $varname within the CGI::SSI object, instead of that within |
| 201 |
our progam.) But the $/ inside the regex is also expanded. This is |
| 202 |
fixed by escaping the "$": |
| 203 |
|
| 204 |
<!--#if expr="'\$varname' =~ /^value\$/" -->ok<!--#endif --> |
| 205 |
|
| 206 |
The expressions used in if and elif tags/calls are tricky due to the |
| 207 |
number of escapes required. In some cases, you'll need to write |
| 208 |
"\\\\" to mean "\". |
| 209 |
|
| 210 |
$ssi->elif($expr) |
| 211 |
$ssi->else |
| 212 |
$ssi->endif |
| 213 |
SEE ALSO |
| 214 |
"Apache::SSI" and the SSI "spec" at |
| 215 |
http://www.apache.org/docs/mod/mod_include.html |
| 216 |
|
| 217 |
AUTHOR |
| 218 |
(c) 2000-2007 James Tolley <[email protected]> All Rights Reserved. |
| 219 |
|
| 220 |
This is free software. You may copy and/or modify it under the same |
| 221 |
terms as perl itself. |
| 222 |
|
| 223 |
CREDITS |
| 224 |
Many Thanks to Corey Wilson for a bug report and fix. |
| 225 |
|