=encoding euc-jp
=head1 NAME
=begin original
CGI - Handle Common Gateway Interface requests and responses
=end original
CGI - Common Gateway Interface のリクエストとレスポンスを扱う
=head1 SYNOPSIS
use CGI;
my $q = CGI->new;
# Process an HTTP request
@values = $q->param('form_field');
$fh = $q->upload('file_field');
$riddle = $query->cookie('riddle_name');
%answers = $query->cookie('answers');
# Prepare various HTTP responses
print $q->header();
print $q->header('application/json');
$cookie1 = $q->cookie(-name=>'riddle_name', -value=>"The Sphynx's Question");
$cookie2 = $q->cookie(-name=>'answers', -value=>\%answers);
print $q->header(
-type => 'image/gif',
-expires => '+3d',
-cookie => [$cookie1,$cookie2]
);
print $q->redirect('http://somewhere.else/in/movie/land');
=head1 DESCRIPTION
=begin original
CGI.pm is a stable, complete and mature solution for processing and preparing
HTTP requests and responses. Major features including processing form
submissions, file uploads, reading and writing cookies, query string generation
and manipulation, and processing and preparing HTTP headers. Some HTML
generation utilities are included as well.
=end original
CGI.pm は、HTML リクエストとレスポンスを処理したり準備したりするための、
安定しており、完全で、枯れているソリューションです。
主な機能としては、フォームの処理、ファイルアップロード、クッキーの読み書き、
クエリ文字列の生成と操作、HTML ヘッダの処理と準備などがあります。
いくつかの HTML 生成ユーリティティも含んでいます。
=begin original
CGI.pm performs very well in in a vanilla CGI.pm environment and also comes
with built-in support for mod_perl and mod_perl2 as well as FastCGI.
=end original
CGI.pm は CGI.pm 単体の環境でとてもよく機能し、
mod_perl や mod_perl2 および FastCGI 対応も内蔵しています。
=begin original
It has the benefit of having developed and refined over 10 years with input
from dozens of contributors and being deployed on thousands of websites.
CGI.pm has been included in the Perl distribution since Perl 5.4, and has
become a de-facto standard.
=end original
数十人の貢献者によって 10 年以上開発と精製されてきたという利点があり、
何千ものウェブサイトで使われています。
CGI.pm は Perl 5.4 から Perl 配布に含まれていて、デファクトスタンダードに
なっています。
=head2 PROGRAMMING STYLE
(プログラミングスタイル)
=begin original
There are two styles of programming with CGI.pm, an object-oriented
style and a function-oriented style. In the object-oriented style you
create one or more CGI objects and then use object methods to create
the various elements of the page. Each CGI object starts out with the
list of named parameters that were passed to your CGI script by the
server. You can modify the objects, save them to a file or database
and recreate them. Because each object corresponds to the "state" of
the CGI script, and because each object's parameter list is
independent of the others, this allows you to save the state of the
script and restore it later.
=end original
CGI.pm ではオブジェクト指向スタイルと関数指向スタイルの
二つのプログラミングスタイルがあります。
オブジェクト指向スタイルでは、一つまたは
複数の CGI オブジェクトを作成し、ページのさまなざな要素を作成するために
オブジェクトメソッドを使います。
各オブジェクトはサーバーによって
スクリプトに渡された名前付きパラメータのリストが出発点となります。
オブジェクトを変更したり、ファイルやデータベースに格納し、それを元に
戻すことが出来ます。
というのも各オブジェクトは CGI スクリプトの
"状態"(state)に対応しており、各オブジェクトのパラメータリストは、
その他のものとは独立しているため、スクリプトの状態を保存し後から
取り出すこともできるのです。
=begin original
For example, using the object oriented style, here is how you create
a simple "Hello World" HTML page:
=end original
以下にオブジェクト指向スタイルを使って、簡単な "Hello World" HTML ページを
どのように作成するかの例を示します:
#!/usr/local/bin/perl -w
use CGI; # load CGI routines
$q = CGI->new; # create new CGI object
print $q->header, # create the HTTP header
$q->start_html('hello world'), # start the HTML
$q->h1('hello world'), # level 1 header
$q->end_html; # end the HTML
=begin original
In the function-oriented style, there is one default CGI object that
you rarely deal with directly. Instead you just call functions to
retrieve CGI parameters, create HTML tags, manage cookies, and so
on. This provides you with a cleaner programming interface, but
limits you to using one CGI object at a time. The following example
prints the same page, but uses the function-oriented interface.
The main differences are that we now need to import a set of functions
into our name space (usually the "standard" functions), and we don't
need to create the CGI object.
=end original
関数指向スタイルでは、直接扱うことがまずない、一つのデフォルトの
CGI オブジェクトがあります。
CGI パラメータを取り出し、HTML タグを作成し、
クッキーを管理する等々のために、代りに関数を単に呼び出します。
これは、よりすっきりしたプログラミングインタフェースを提供しますが、
一度に一つの CGI オブジェクトしか使えないよう制限します。
以下の例は同じページで関数指向インターフェースを使っています。
主な違いは、今度は名前空間に関数のセット (通常は "standard" の関数群) を
インポートする必要があること、そして CGI オブジェクトを作成する必要が
ないことです。
#!/usr/local/bin/perl
use CGI qw/:standard/; # load standard CGI routines
print header, # create the HTTP header
start_html('hello world'), # start the HTML
h1('hello world'), # level 1 header
end_html; # end the HTML
=begin original
The examples in this document mainly use the object-oriented style.
See HOW TO IMPORT FUNCTIONS for important information on
function-oriented programming in CGI.pm
=end original
このドキュメントの例では主にオブジェクト指向スタイルを使います。
CGI.pm での関数指向プログラミングについての重要な情報は
「関数のインポート方法」をご覧下さい。
=head2 CALLING CGI.PM ROUTINES
(CGI.pm ルーチンの呼び出し)
=begin original
Most CGI.pm routines accept several arguments, sometimes as many as 20
optional ones! To simplify this interface, all routines use a named
argument calling style that looks like this:
=end original
ほとんどの CGI.pm ルーチンはさまざまな引数を受け取ります。
中には 20 ものオプションの引数を受取るものもあります!
このインターフェースを簡単にするため、すべてのルーチンは以下のような
名前付き引数呼び出しスタイルを使います:
print $q->header(-type=>'image/gif',-expires=>'+3d');
=begin original
Each argument name is preceded by a dash. Neither case nor order
matters in the argument list. -type, -Type, and -TYPE are all
acceptable. In fact, only the first argument needs to begin with a
dash. If a dash is present in the first argument, CGI.pm assumes
dashes for the subsequent ones.
=end original
各引数の名前の前にはダッシュがつきます。
引数リストでは大文字/小文字や順番は問題になりません。
-type、-Type、-TYPE のすべてが受取られます。
実際には、最初の引数だけがダッシュから始まる必要があります。
最初の引数にダッシュがあれば、CGI.pm は後のものにもダッシュが
あるものとします。
=begin original
Several routines are commonly called with just one argument. In the
case of these routines you can provide the single argument without an
argument name. header() happens to be one of these routines. In this
case, the single argument is the document type.
=end original
さまざまなルーチンは一般に一つの引数だけで呼ばれます。
それらのルーチンの場合、引数名なしに一つの引数を与えることが出来ます。
header() は、そうしたルーチンの一つです。
この場合、一つの引数はドキュメントタイプです。
print $q->header('text/html');
=begin original
Other such routines are documented below.
=end original
他のそのようなルーチンは下記で記述しています。
=begin original
Sometimes named arguments expect a scalar, sometimes a reference to an
array, and sometimes a reference to a hash. Often, you can pass any
type of argument and the routine will do whatever is most appropriate.
For example, the param() routine is used to set a CGI parameter to a
single or a multi-valued value. The two cases are shown below:
=end original
名前付き引数はあるときはスカラを期待し、あるときは配列へのリファレンス、
あるいはハッシュへのリファレンスを期待します。
多くの場合、どんな種類の引数も渡すことができ、ルーチンはそれに対して
最も適切なことを行います。
例えば param() ルーチンは CGI パラメータに一つあるいは複数の値を設定する
ために使われます。
二つのケースを以下に示します:
$q->param(-name=>'veggie',-value=>'tomato');
$q->param(-name=>'veggie',-value=>['tomato','tomahto','potato','potahto']);
=begin original
A large number of routines in CGI.pm actually aren't specifically
defined in the module, but are generated automatically as needed.
These are the "HTML shortcuts," routines that generate HTML tags for
use in dynamically-generated pages. HTML tags have both attributes
(the attribute="value" pairs within the tag itself) and contents (the
part between the opening and closing pairs.) To distinguish between
attributes and contents, CGI.pm uses the convention of passing HTML
attributes as a hash reference as the first argument, and the
contents, if any, as any subsequent arguments. It works out like
this:
=end original
CGI.pm のルーチンの多くがモジュール内で特に定義されておらず、必要に応じて
自動的に生成されます。
これらは動的に生成されるページで使われ、HTML を
生成する "HTML ショートカット" ルーチンです。
HTML タグは属性(タグ自身に入っている属性="値"の組) と内容
(開始と終了の組の間の部分) の両方を持ちます。
属性と内容とを区別するため、CGI.pm は HTML 属性をハッシュリファレンスで
最初の引数として、そして内容があればその後の引数として、
渡すような約束を使っています。
それは以下のように機能します:
Code Generated HTML
---- --------------
h1()
h1('some','contents');
some contents
h1({-align=>left});
h1({-align=>left},'contents');
contents
=begin original
HTML tags are described in more detail later.
=end original
HTML タグについては後で詳しく記述します。
=begin original
Many newcomers to CGI.pm are puzzled by the difference between the
calling conventions for the HTML shortcuts, which require curly braces
around the HTML tag attributes, and the calling conventions for other
routines, which manage to generate attributes without the curly
brackets. Don't be confused. As a convenience the curly braces are
optional in all but the HTML shortcuts. If you like, you can use
curly braces when calling any routine that takes named arguments. For
example:
=end original
CGI を使い始めたばかりの人の多くが、HTML タグ属性を囲む中かっこを必要とする
HTML ショートカットの呼び出し方と、中かっこ無しに属性の生成を管理する他の
ルーチンの呼び出し方との違いに惑わされます。
混乱しないで下さい。
便宜上、中かっこは HTML を除くすべてでオプションです。
もし好むなら、名前付き引数を取るどのようなルーチンでも呼び出すときに
中かっこを使えます。
例えば:
print $q->header( {-type=>'image/gif',-expires=>'+3d'} );
=begin original
If you use the B<-w> switch, you will be warned that some CGI.pm argument
names conflict with built-in Perl functions. The most frequent of
these is the -values argument, used to create multi-valued menus,
radio button clusters and the like. To get around this warning, you
have several choices:
=end original
B<-w> スイッチを使うと、いくつかの CGI.pm 引数は Perl 組込関数と名前が
ぶつかっていることを警告されるでしょう。
これらのほとんどは、
複数の値を持つメニュー(multi-valued menu)、ラジオボタン(radio button)、
クラスター(cluster)などを作成するために使われる -values 引数です。
この警告を回避するためには、いくつかの選択肢があります:
=over 4
=item 1.
=begin original
Use another name for the argument, if one is available.
For example, -value is an alias for -values.
=end original
もし他の名前が使えれば、引数に他の名前を使う。
例えば -value は -values のための別名です。
=item 2.
=begin original
Change the capitalization, e.g. -Values
=end original
先頭を大文字化する。
例. -Values
=item 3.
=begin original
Put quotes around the argument name, e.g. '-values'
=end original
引数名の周りをクォートで囲む。
例. '-values'
=back
=begin original
Many routines will do something useful with a named argument that it
doesn't recognize. For example, you can produce non-standard HTTP
header fields by providing them as named arguments:
=end original
多くのルーチンが理解できない名前付き引数についても、なんらかの有効なことを
行います。
例えば、名前付きの引数として与えることにより標準ではない
HTTP ヘッダフィールドを作成することが出来ます:
print $q->header(-type => 'text/html',
-cost => 'Three smackers',
-annoyance_level => 'high',
-complaints_to => 'bit bucket');
=begin original
This will produce the following nonstandard HTTP header:
=end original
これは以下の標準ではない HTTP ヘッダを作成します:
HTTP/1.0 200 OK
Cost: Three smackers
Annoyance-level: high
Complaints-to: bit bucket
Content-type: text/html
=begin original
Notice the way that underscores are translated automatically into
hyphens. HTML-generating routines perform a different type of
translation.
=end original
アンダースコアが自動的にハイフンに変換される方法について注意してください。
HTML 作成ルーチンは異なる変換をします。
=begin original
This feature allows you to keep up with the rapidly changing HTTP and
HTML "standards".
=end original
この機能は HTTP と HTML の"標準"に迅速に追いかけることを可能にします。
=head2 CREATING A NEW QUERY OBJECT (OBJECT-ORIENTED STYLE):
(新しい問い合わせオブジェクトの作成(オブジェクト指向スタイル) )
$query = CGI->new;
=begin original
This will parse the input (from both POST and GET methods) and store
it into a perl5 object called $query.
=end original
これは (POST と GET メソッドの両方からの) 入力を解析し、
$query と呼ばれる perl5 オブジェクトに格納します。
=begin original
Any filehandles from file uploads will have their position reset to
the beginning of the file.
=end original
ファイルアップロードからのすべてのファイルハンドルは、
その位置をファイルの先頭にリセットします。
=head2 CREATING A NEW QUERY OBJECT FROM AN INPUT FILE
(入力ファイルからの新しい問い合わせオブジェクトの作成)
$query = CGI->new(INPUTFILE);
=begin original
If you provide a file handle to the new() method, it will read
parameters from the file (or STDIN, or whatever). The file can be in
any of the forms describing below under debugging (i.e. a series of
newline delimited TAG=VALUE pairs will work). Conveniently, this type
of file is created by the save() method (see below). Multiple records
can be saved and restored.
=end original
ファイルハンドルを new() メソッドに与えると、ファイル (または STDIN でも
なんでも) からパラメータを読み込みます。
デバッグ中、ファイルには以下に説明する形式ならば、何にでも
することができます (つまり改行で区切られたタグ=値の組が機能します) 。
便利なことに、このファイルのタイプは save() メソッドにより作成されます。
複数のレコードを保存し、元に戻すことが出来ます。
=begin original
Perl purists will be pleased to know that this syntax accepts
references to file handles, or even references to filehandle globs,
which is the "official" way to pass a filehandle:
=end original
Perl 純粋主義者はこの文法がファイルハンドルを、ファイルハンドルグロブさえも
受取ることを知って喜ぶでしょう; これはファイルハンドルを渡す「公式の」
方法です:
$query = CGI->new(\*STDIN);
=begin original
You can also initialize the CGI object with a FileHandle or IO::File
object.
=end original
CGI オブジェクトを FileHandle または IO::File オブジェクトで初期化することも
出来ます。
=begin original
If you are using the function-oriented interface and want to
initialize CGI state from a file handle, the way to do this is with
B. This will (re)initialize the
default CGI object from the indicated file handle.
=end original
関数指向インターフェースを使っていて、CGI 状態をファイルハンドルで
初期化したければ、Bでおこないます。
これはデフォルトの CGI オブジェクトを指定されたファイルハンドルで
(再) 初期化します。
open (IN,"test.in") || die;
restore_parameters(IN);
close IN;
=begin original
You can also initialize the query object from a hash
reference:
=end original
ハッシュリファレンスから問い合わせオブジェクトを初期化することも出来ます:
$query = CGI->new( {'dinosaur'=>'barney',
'song'=>'I love you',
'friends'=>[qw/Jessica George Nancy/]}
);
=begin original
or from a properly formatted, URL-escaped query string:
=end original
あるいは適切にフォーマットされた、URL エスケープされた問い合わせ文字列から:
$query = CGI->new('dinosaur=barney&color=purple');
=begin original
or from a previously existing CGI object (currently this clones the
parameter list, but none of the other object-specific fields, such as
autoescaping):
=end original
あるいは既に存在している CGI オブジェクトから (現在、これはパラメータリストの
複製を作りますが、autoescaping のようなオブジェクト特有のフィールドは
複写しません):
$old_query = CGI->new;
$new_query = CGI->new($old_query);
=begin original
To create an empty query, initialize it from an empty string or hash:
=end original
空の問い合わせを作成するためには、空文字列または空のハッシュで初期化します:
$empty_query = CGI->new("");
-or-
$empty_query = CGI->new({});
=head2 FETCHING A LIST OF KEYWORDS FROM THE QUERY:
(問い合わせからのキーワードのリストの取り出し)
@keywords = $query->keywords
=begin original
If the script was invoked as the result of an search, the
parsed keywords can be obtained as an array using the keywords() method.
=end original
検索の結果としてスクリプトが呼び出されれば、解析されたキーワードは
keywords() メソッドを使って配列として取得出来ます。
=head2 FETCHING THE NAMES OF ALL THE PARAMETERS PASSED TO YOUR SCRIPT:
(スクリプトの渡された全てのパラメータの名前の取り出し:)
@names = $query->param
=begin original
If the script was invoked with a parameter list
(e.g. "name1=value1&name2=value2&name3=value3"), the param() method
will return the parameter names as a list. If the script was invoked
as an script and contains a string without ampersands
(e.g. "value1+value2+value3") , there will be a single parameter named
"keywords" containing the "+"-delimited keywords.
=end original
パラメータ付きでスクリプトが呼び出されると (例えば
"name1=value1&name2=value2&name3=value3") 、
param()メソッドはパラメータ名をリストで返します。
もしスクリプトがスクリプトとして呼び出され、アンパサンドのない
文字列が入っていれば (例えば、"value1+value2+value3") 、
"+" で区切られたキーワードが入った "keywords" という名前の一つの
パラメータになります。
=begin original
NOTE: As of version 1.5, the array of parameter names returned will
be in the same order as they were submitted by the browser.
Usually this order is the same as the order in which the
parameters are defined in the form (however, this isn't part
of the spec, and so isn't guaranteed).
=end original
注意:バージョン1.5では、パラメータ名の配列はブラウザにより実行されたのと
同じ順番でした。
通常、この順序はパラメータがフォームで定義された順と同じです
(しかしながら仕様には入っていないため保証はされません。)
=head2 FETCHING THE VALUE OR VALUES OF A SINGLE NAMED PARAMETER:
(一つの名前つきパラメータの値を取り出す:)
@values = $query->param('foo');
-or-
$value = $query->param('foo');
=begin original
Pass the param() method a single argument to fetch the value of the
named parameter. If the parameter is multivalued (e.g. from multiple
selections in a scrolling list), you can ask to receive an array. Otherwise
the method will return a single value.
=end original
名前付きパラメータの値を取り出すために param() メソッドに一つの引数を
渡してください。
もしそのパラメータが複数の値を持っていれば(例えば
スクローリングリスト(scrolling list)での複数の選択から)、配列で
受け取るようにすることが出来ます。
そうでなければ、このメソッドは一つの値を返します。
=begin original
If a value is not given in the query string, as in the queries
"name1=&name2=", it will be returned as an empty string.
=end original
もし値が問い合わせ文字列で与えられなければ、つまり問い合わせで
"name1=&name2=" であれば、空文字列を返します。
=begin original
If the parameter does not exist at all, then param() will return undef
in a scalar context, and the empty list in a list context.
=end original
もしパラメータが全くなければ、param() はスカラコンテキストでは undef を
返し、リストコンテキストでは空リストを返します。
=head2 SETTING THE VALUE(S) OF A NAMED PARAMETER:
(名前つきパラメータへの値の設定:)
$query->param('foo','an','array','of','values');
=begin original
This sets the value for the named parameter 'foo' to an array of
values. This is one way to change the value of a field AFTER
the script has been invoked once before. (Another way is with
the -override parameter accepted by all methods that generate
form elements.)
=end original
これは名前付きパラメータ 'foo' の値として値の配列を設定します。
これは、スクリプトが前に一度呼び出された後にフィールドの値を変更するための
一つの方法です。
(もう一つの方法はフォーム要素を作成するすべてのメソッドで受け取られる
-override パラメータを使うことです。)
=begin original
param() also recognizes a named parameter style of calling described
in more detail later:
=end original
param() は下記でさらに詳しく記述する呼び出しの名前付きパラメータ形式も
理解します:
$query->param(-name=>'foo',-values=>['an','array','of','values']);
-or-
$query->param(-name=>'foo',-value=>'the value');
=head2 APPENDING ADDITIONAL VALUES TO A NAMED PARAMETER:
(名前つきパラメータに値を追加する:)
$query->append(-name=>'foo',-values=>['yet','more','values']);
=begin original
This adds a value or list of values to the named parameter. The
values are appended to the end of the parameter if it already exists.
Otherwise the parameter is created. Note that this method only
recognizes the named argument calling syntax.
=end original
これは値または値のリストを名前付きパラメータに追加します。
既にあれば、その値はパラメータの最後に追加されます。
そうでなければパラメータが作成されます。
このメソッドは名前付き引数呼び出し書式しか理解しないことに注意してください。
=head2 IMPORTING ALL PARAMETERS INTO A NAMESPACE:
(すべてのパラメータの名前空間へのインポート:)
$query->import_names('R');
=begin original
This creates a series of variables in the 'R' namespace. For example,
$R::foo, @R:foo. For keyword lists, a variable @R::keywords will appear.
If no namespace is given, this method will assume 'Q'.
WARNING: don't import anything into 'main'; this is a major security
risk!!!!
=end original
これは一連の変数を 'R' 名前空間に作成します。
例えば $R::foo、@R:foo のように。
キーワードリストでは、変数 @R:keyword があります。
名前空間が指定されなければ、この引数は 'Q' を想定します。
警告: 'main' には何もインポートしないこと; それはセキュリティ上、大きな
危険性があります!!!
=begin original
NOTE 1: Variable names are transformed as necessary into legal Perl
variable names. All non-legal characters are transformed into
underscores. If you need to keep the original names, you should use
the param() method instead to access CGI variables by name.
=end original
注意 1: 変数名は必要に応じて有効な Perl 変数名に変換されます。
全ての無効な文字は下線に変換されます。
もし元の名前を保存しておく必要が亜あるなら、CGI 変数に名前で
アクセスするのではなく、param() メソッドを使うべきです。
=begin original
NOTE 2: In older versions, this method was called B. As of version 2.20,
this name has been removed completely to avoid conflict with the built-in
Perl module B operator.
=end original
注意 2: 古いバージョンでは、このメソッドは B と呼ばれていました。
バージョン 2.20 から、組み込みの Perl モジュール B 演算子と
ぶつかることを避けるため、この名前は完全に削除されました。
=head2 DELETING A PARAMETER COMPLETELY:
(パラメータを完全に削除する:)
$query->delete('foo','bar','baz');
=begin original
This completely clears a list of parameters. It sometimes useful for
resetting parameters that you don't want passed down between script
invocations.
=end original
これはパラメータを完全にクリアします。
それはスクリプト呼び出しの間で、渡したくないパラメータをリセットするのに
時々便利です。
=begin original
If you are using the function call interface, use "Delete()" instead
to avoid conflicts with Perl's built-in delete operator.
=end original
関数呼び出しインターフェースを使っているのであれば、Perl の組み込み演算子
delete との衝突を避けるため、代わりに "Delete()" を使ってください。
=head2 DELETING ALL PARAMETERS:
(すべてのパラメータを削除する:)
$query->delete_all();
=begin original
This clears the CGI object completely. It might be useful to ensure
that all the defaults are taken when you create a fill-out form.
=end original
これは CGI オブジェクトを完全にクリアします。
これはフォームを作成するときに、すべてのデフォルトが取られることを
保証するために便利です。
=begin original
Use Delete_all() instead if you are using the function call interface.
=end original
関数呼び出しインターフェースを使っているならば、代りに Delete_all() を使って
ください。
=head2 HANDLING NON-URLENCODED ARGUMENTS
(URL エンコードされていない引数を扱う)
=begin original
If POSTed data is not of type application/x-www-form-urlencoded or
multipart/form-data, then the POSTed data will not be processed, but
instead be returned as-is in a parameter named POSTDATA. To retrieve
it, use code like this:
=end original
もし POST されたデータのタイプが application/x-www-form-urlencoded か
multipart/form-data ではない場合、POST されたデータは処理されず、
代わりに POSTDATA という名前のパラメータにそのままの形で返されます。
これを取得するには、以下のようなコードを使います:
my $data = $query->param('POSTDATA');
=begin original
Likewise if PUTed data can be retrieved with code like this:
=end original
同様に、PUT されたデータは以下のようなコードで取得できます:
my $data = $query->param('PUTDATA');
=begin original
(If you don't know what the preceding means, don't worry about it. It
only affects people trying to use CGI for XML processing and other
specialized tasks.)
=end original
(もしここに書いたことの意味が分からなくても、気にする必要はありません。
これは CGI を、XML 処理やその他の特殊な処理に使おうとする人々にのみ
影響します。
=head2 DIRECT ACCESS TO THE PARAMETER LIST:
(パラメータリストへの直接アクセス:)
$q->param_fetch('address')->[1] = '1313 Mockingbird Lane';
unshift @{$q->param_fetch(-name=>'address')},'George Munster';
=begin original
If you need access to the parameter list in a way that isn't covered
by the methods above, you can obtain a direct reference to it by
calling the B method with the name of the . This
will return an array reference to the named parameters, which you then
can manipulate in any way you like.
=end original
上述のメソッドでカバーされていない方法でパラメータリストへアクセスする
必要があるなら、その名前で B を呼び出すことにより、それへの
直接のリファレンスを取得出来ます。
これは名前付きパラメータへの配列リファレンスを返し、これは好きなように
操作できます。
=begin original
You can also use a named argument style using the B<-name> argument.
=end original
B<-name> を使って、名前付き引数スタイルを使うことも出来ます。
=head2 FETCHING THE PARAMETER LIST AS A HASH:
(パラメータリストのハッシュでの取り出し:)
$params = $q->Vars;
print $params->{'address'};
@foo = split("\0",$params->{'foo'});
%params = $q->Vars;
use CGI ':cgi-lib';
$params = Vars;
=begin original
Many people want to fetch the entire parameter list as a hash in which
the keys are the names of the CGI parameters, and the values are the
parameters' values. The Vars() method does this. Called in a scalar
context, it returns the parameter list as a tied hash reference.
Changing a key changes the value of the parameter in the underlying
CGI parameter list. Called in a list context, it returns the
parameter list as an ordinary hash. This allows you to read the
contents of the parameter list, but not to change it.
=end original
多くの人がすべてのパラメータリストを、CGI パラメータの名前をキーとし、
そのパラメータの値を値とするハッシュとして取り出しがります。
これを Vars() メソッドが行います。
スカラコンテキストで呼ばれると、tie されたハッシュリファレンスとして
パラメータリストを返します。
キーを変更すると、元になっている CGI パラメータリストでのパラメータの
値を変更します。
ハッシュコンテキストで呼ばれると、それは通常のハッシュとして
パラメータリストを返します。
これによりパラメータリストの内容を読むことが出来ますが、変更することは
できません。
=begin original
When using this, the thing you must watch out for are multivalued CGI
parameters. Because a hash cannot distinguish between scalar and
list context, multivalued parameters will be returned as a packed
string, separated by the "\0" (null) character. You must split this
packed string in order to get at the individual values. This is the
convention introduced long ago by Steve Brenner in his cgi-lib.pl
module for Perl version 4.
=end original
これを使うとき、複数の値を持つ CGI パラメータについて気をつけなければ
いけません。
ハッシュはスカラコンテキストとリストコンテキストを区別しないので、複数の値を
もつパラメータは "\0"(null) 文字で区切られた、パックされた文字列で
返されます。
それぞれの値を取り出すためにはパックされた文字列を分割しなければなりません。
このやり方は Perl バージョン 4 のための cgi-lib.pl モジュールで、
Steve Brrenner によってずっと昔に導入されました。
=begin original
If you wish to use Vars() as a function, import the I<:cgi-lib> set of
function calls (also see the section on CGI-LIB compatibility).
=end original
Vars() を関数として使いたければ、関数呼び出しセット I<:cgi-lib> を
インポートしてください。
(CGI-LIB との互換性についてのセクションもご覧下さい)。
=head2 SAVING THE STATE OF THE SCRIPT TO A FILE:
(スクリプトの状態をファイルに保存する:)
$query->save(\*FILEHANDLE)
=begin original
This will write the current state of the form to the provided
filehandle. You can read it back in by providing a filehandle
to the new() method. Note that the filehandle can be a file, a pipe,
or whatever!
=end original
これはフォームの現在の状態を指定されたファイルハンドルに書き込みます。
new() メソッドにファイルハンドルを与えることにより読み戻すことが出来ます。
ファイルハンドルは、ファイル、パイプ、その他何にでもにすることが
出来ることに注意してください!
=begin original
The format of the saved file is:
=end original
保存されるファイルの形式は以下の通りです:
NAME1=VALUE1
NAME1=VALUE1'
NAME2=VALUE2
NAME3=VALUE3
=
=begin original
Both name and value are URL escaped. Multi-valued CGI parameters are
represented as repeated names. A session record is delimited by a
single = symbol. You can write out multiple records and read them
back in with several calls to B. You can do this across several
sessions by opening the file in append mode, allowing you to create
primitive guest books, or to keep a history of users' queries. Here's
a short example of creating multiple session records:
=end original
名前と値の両方が URL エスケープされます。
複数の値を持つ CGI パラメータは名前を繰り返すことにより表すことができます。
セッションレコードはsingle=symbol によって範囲を決められます。
何回も B を呼ぶことにより、複数のレコードを書き出し、読み戻すことが
出来ます。
追記 (append) モードでファイルを開くことにより、複数のセッションに
またがって、これを行うことが出来、これにより原始的なゲストブックや
ユーザの質問の履歴を作成することが出来ます。
以下は複数のセッションレコードを作成する短い例です:
use CGI;
open (OUT,'>>','test.out') || die;
$records = 5;
for (0..$records) {
my $q = CGI->new;
$q->param(-name=>'counter',-value=>$_);
$q->save(\*OUT);
}
close OUT;
# reopen for reading
open (IN,'<','test.out') || die;
while (!eof(IN)) {
my $q = CGI->new(\*IN);
print $q->param('counter'),"\n";
}
=begin original
The file format used for save/restore is identical to that used by the
Whitehead Genome Center's data exchange format "Boulderio", and can be
manipulated and even databased using Boulderio utilities. See
=end original
保存/復帰に使われるファイルフォーマットは Whitehead Genome Center の
データ交換フォーマット"Boulderio" に使われているものと同じで、
Boulderio ユーティリティを使って扱ったり、さらにはデータベース化することが
できます。
さらなる詳細は
http://stein.cshl.org/boulder/
=begin original
for further details.
=end original
をご覧下さい。
=begin original
If you wish to use this method from the function-oriented (non-OO)
interface, the exported name for this method is B.
=end original
関数指向 (非OO) からこの関数を使いたいのであれば、エクスポートされる
このメソッドの名前は B です。
=head2 RETRIEVING CGI ERRORS
(CGIエラーの取り出し)
=begin original
Errors can occur while processing user input, particularly when
processing uploaded files. When these errors occur, CGI will stop
processing and return an empty parameter list. You can test for
the existence and nature of errors using the I function.
The error messages are formatted as HTTP status codes. You can either
incorporate the error text into an HTML page, or use it as the value
of the HTTP status:
=end original
ユーザ入力を処理して切る間、特にアップロードされたファイルを処理している
間にエラーが発生することがあります。
これらのエラーが発生したとき、CGI は処理を止め、空のパラメータリストを
返します。
エラーの存在とその性質を I 関数を使って調べることが出来ます。
エラーメッセージは HTTP ステータスコードとしてフォーマットされます。
HTML ページにそのエラーテキストを入れたり、HTTP ステータスの値として
使うことができます:
my $error = $q->cgi_error;
if ($error) {
print $q->header(-status=>$error),
$q->start_html('Problems'),
$q->h2('Request not processed'),
$q->strong($error);
exit 0;
}
=begin original
When using the function-oriented interface (see the next section),
errors may only occur the first time you call I. Be ready
for this!
=end original
関数指向インターフェース (次のセクションをご覧下さい) を使うとき、エラーは
最初に I を呼んだときにだけ発生します。
これに備えてください!
=head2 USING THE FUNCTION-ORIENTED INTERFACE
(関数指向インターフェースの使い方)
=begin original
To use the function-oriented interface, you must specify which CGI.pm
routines or sets of routines to import into your script's namespace.
There is a small overhead associated with this importation, but it
isn't much.
=end original
関数指向インタフェースを使うためには、どの CGI.pm ルーチンまたは関数群を
スクリプトの名前空間にインポートするかを指定しなければいけません。
このインポートに関連して少しオーバーヘッドがありますが、大したことは
ありません。
use CGI ;
=begin original
The listed methods will be imported into the current package; you can
call them directly without creating a CGI object first. This example
shows how to import the B and B
methods, and then use them directly:
=end original
リストに入れられたメソッドは現在のパッケージにインポートされます;
CGI オブジェクトを最初に作成することなく直接呼び出すことが出来ます。
この例ではどのように B と B メソッドをインポートし、
それらを直接使うかを示しています:
use CGI 'param','header';
print header('text/plain');
$zipcode = param('zipcode');
=begin original
More frequently, you'll import common sets of functions by referring
to the groups by name. All function sets are preceded with a ":"
character as in ":html3" (for tags defined in the HTML 3 standard).
=end original
さらに多くの場合、名前でグループを参照することにより一般的な関数の組を
インポートします。
すべての関数の組の前には ":html3" (HTML3標準で定義されたタグ用) のように、
前に":" がつきます。
=begin original
Here is a list of the function sets you can import:
=end original
以下にインポートできる関数の組のリストを示します:
=over 4
=item B<:cgi>
=begin original
Import all CGI-handling methods, such as B, B
and the like.
=end original
B, B のような、CGI を扱うすべてのメソッドを
インポートします。
=item B<:form>
=begin original
Import all fill-out form generating methods, such as B.
=end original
B のような、フォームを作成するメソッドをインポートします。
=item B<:html2>
=begin original
Import all methods that generate HTML 2.0 standard elements.
=end original
HTML 2.0 標準要素を作成するすべてのメソッドをインポートします。
=item B<:html3>
=begin original
Import all methods that generate HTML 3.0 elements (such as
, and ).
=end original
HTML 3.0 標準要素を作成するすべてのメソッドをインポートします。
(
, , のような)
=item B<:html4>
=begin original
Import all methods that generate HTML 4 elements (such as
, and ).
=end original
HTML 4 標準要素を作成するすべてのメソッドをインポートします。
(, , のような)
=item B<:netscape>
=begin original
Import the