perlfaq9 > 5.6.1 との差分

perlfaq9 5.6.1 と 5.14.1 の差分

11
22=encoding euc-jp
33
44=head1 NAME
55
66=begin original
77
8perlfaq9 - Networking ($Revision: 1.26 $, $Date: 1999/05/23 16:08:30 $)
8perlfaq9 - Networking
99
1010=end original
1111
12perlfaq9 - ネットワーク ($Revision: 1.26 $, $Date: 1999/05/23 16:08:30 $)
12perlfaq9 - ネットワーク
1313
1414=head1 DESCRIPTION
1515
16=begin original
17
18This section deals with questions related to networking, the internet,
19and a few on the web.
20
21=end original
22
1623このセクションでは、ネットワーク、インターネット、web に関する
1724質問を扱っています。
1825
19=head2 My CGI script runs from the command line but not the browser. (500 Server Error)
26=head2 What is the correct form of response from a CGI script?
2027
21(私のCGIスクリプトはコマンドラインでは動くだけど、ブラウザー上で動きません
28(CGI スクリプトから返答の正しい形?)
22(500 Server Errorになります))
2329
2430=begin original
2531
26If you can demonstrate that you've read the following FAQs and that
32(Alan Flavell <flavell+www@a5.ph.gla.ac.uk> answers...)
27your problem isn't something simple that can be easily answered, you'll
28probably receive a courteous and useful reply to your question if you
29post it on comp.infosystems.www.authoring.cgi (if it's something to do
30with HTTP, HTML, or the CGI protocols). Questions that appear to be Perl
31questions but are really CGI ones that are posted to comp.lang.perl.misc
32may not be so well received.
3333
3434=end original
3535
36FAQの以下の部分を読んで、あなたの問題簡単にられるものではないと
36(Alan Flavell <flavell+www@a5.ph.gla.ac.uk> が答えます...)
37わかったのなら、(HTTP、HTMLやCGIプロトコルに関するものなら)
38comp.infosystems.www.authoring.cgiにポストすれば
39有用なリプライが得られるでしょう。
40Perlに関する質問のように見えていても、実はCGIに関するものだというものが
41comp.lang.perl.miscに投稿されることがありますが、回答はついていません。
4237
43便利なFAQや関連するドキュメントを以下に挙げます:
38=begin original
4439
45 CGI FAQ
40The Common Gateway Interface (CGI) specifies a software interface between
46 http://www.webthing.com/tutorials/cgifaq.html
41a program ("CGI script") and a web server (HTTPD). It is not specific
42to Perl, and has its own FAQs and tutorials, and usenet group,
43comp.infosystems.www.authoring.cgi
4744
48 Web FAQ
45=end original
49 http://www.boutell.com/faq/
5046
51 WWW Security FAQ
47The Common Gateway Interface (CGI) はプログラム("CGI スクリプト")と
52 http://www.w3.org/Security/Faq/
48web サーバ (HTTPD) の間のソフトウェアインターフェースを定めています。
49これは Perl 固有のものではないので、独自の FAQと独自のチュートリアル、
50そして独自の usenet group である comp.infosystems.www.authoring.cgi が
51あります。
5352
54 HTTP Spec
53=begin original
55 http://www.w3.org/pub/WWW/Protocols/HTTP/
5654
57 HTML Spec
55The CGI specification is outlined in an informational RFC:
58 http://www.w3.org/TR/REC-html40/
56http://www.ietf.org/rfc/rfc3875
59 http://www.w3.org/pub/WWW/MarkUp/
6057
61 CGI Spec
58=end original
62 http://www.w3.org/CGI/
6359
64 CGI Security FAQ
60CGI 仕様は RFC に概要が述べられています:
65 http://www.go2net.com/people/paulp/cgi-security/safe-cgi.txt
61http://www.ietf.org/rfc/rfc3875
6662
63=begin original
64
65These Perl FAQs very selectively cover some CGI issues. However, Perl
66programmers are strongly advised to use the C<CGI.pm> module, to take care
67of the details for them.
68
69=end original
70
71これらの Perl FAQ はいくつかの CGI の問題についてとても抜粋して
72カバーしています。
73しかし、これらの詳細に用心するために、Perl プログラマは C<CGI> モジュールを
74使うことを強く忠告されます。
75
76=begin original
77
78The similarity between CGI response headers (defined in the CGI
79specification) and HTTP response headers (defined in the HTTP
80specification, RFC2616) is intentional, but can sometimes be confusing.
81
82=end original
83
84(CGI 仕様に定義されている) CGI レスポンスヘッダと、(RFC2616 の HTTP 仕様で
85定義されている) HTTP レスポンスヘッダの類似性は意図的なものですが、
86時々混乱を引き起こします。
87
88=begin original
89
90The CGI specification defines two kinds of script: the "Parsed Header"
91script, and the "Non Parsed Header" (NPH) script. Check your server
92documentation to see what it supports. "Parsed Header" scripts are
93simpler in various respects. The CGI specification allows any of the
94usual newline representations in the CGI response (it's the server's
95job to create an accurate HTTP response based on it). So "\n" written in
96text mode is technically correct, and recommended. NPH scripts are more
97tricky: they must put out a complete and accurate set of HTTP
98transaction response headers; the HTTP specification calls for records
99to be terminated with carriage-return and line-feed; i.e., ASCII \015\012
100written in binary mode.
101
102=end original
103
104CGI 仕様は 2 種類のスクリプトを定義しています:
105"Parsed Header" スクリプトと、"Non Parsed Header" (NPH) スクリプト。
106何をサポートしているかについてはサーバーのドキュメントをチェックして
107ください。
108"Parsed Header" スクリプトは色々な側面においてより単純です。
109CGI 仕様は CGI レスポンスとして一般的な改行表現のどれを使ってもよいことに
110なっています(そこから正確な HTTP レスポンスを作成するのはサーバの仕事です)。
111従って "\n" をテキストモードで書くのは技術的に正しく、推奨されています。
112NPH スクリプトではより微妙です: ここでは完全に正確な HTTP トランザクション
113レスポンスヘッダを出力しなければなりません; HTTP 仕様はレコードが復帰と
114改行(つまりバイナリモードで ASCII コードの \015\012 が書かれる)で
115終端されていることを要求します。
116
117=begin original
118
119Using C<CGI.pm> gives excellent platform independence, including EBCDIC
120systems. C<CGI.pm> selects an appropriate newline representation
121(C<$CGI::CRLF>) and sets binmode as appropriate.
122
123=end original
124
125C<CGI.pm> を使うことで、EBCDIC システムを含むすばらしいプラットフォーム
126独立性が得られます。
127C<CGI.pm> は適切な改行表現を選択し(C<$CGI::CRLF>)、binmode を適切にセットします。
128
129=head2 My CGI script runs from the command line but not the browser. (500 Server Error)
130
131(私の CGI スクリプトはコマンドラインでは動くのだけど、ブラウザ上では動きません (500 Server Error になります))
132
133=begin original
134
135(contributed by brian d foy)
136
137=end original
138
139(brian d foy によって寄贈されました)
140
141=begin original
142
143There are many things that might be wrong with your CGI program, and only
144some of them might be related to Perl. Try going through the troubleshooting
145guide on Perlmonks:
146
147=end original
148
149CGI プログラムが悪いかもしれないいくつかのことがあり、その一部だけが
150Perl に関連します。
151Perlmonks によるトラブルシューティングを確認してみてください:
152
153 http://www.perlmonks.org/?node_id=380424
154
67155=head2 How can I get better error messages from a CGI program?
68156
69(CGIプログラムから、もっとまともなエラーメッセージを得るには?)
157(CGI プログラムから、もっとまともなエラーメッセージを得るには?)
70158
71159=begin original
72160
73Use the CGI::Carp module. It replaces C<warn> and C<die>, plus the
161Use the C<CGI::Carp> module. It replaces C<warn> and C<die>, plus the
74normal Carp modules C<carp>, C<croak>, and C<confess> functions with
162normal C<Carp> module's C<carp>, C<croak>, and C<confess> functions with
75163more verbose and safer versions. It still sends them to the normal
76164server error log.
77165
78166=end original
79167
80CGI::Carpモジュールを使いましょう。このモジュールはC<warn>とC<die>の
168C<CGI::Carp> モジュールを使いましょう。
81置き換えを行い、さらに通常CarpモジュールC<carp>C<croak>、
169のモジュールC<warn>C<die> の置き換えを行いさらに通常の
82C<confess>といった関数をより饒舌で安全なものに置き換えます。
170C<Carp> モジュールの C<carp>、C<croak>、C<confess> といった関数をより饒舌で
171安全なものに置き換えます。
83172その出力は、サーバーの通常のエラーログに送られます。
84173
85 use CGI::Carp;
174 use CGI::Carp;
86 warn "This is a complaint";
175 warn "This is a complaint";
87 die "But this one is serious";
176 die "But this one is serious";
88177
89以下のCGI::Carpの使用例では、エラーをあなたの選択したファイルへリダイレクトし、
178=begin original
90コンパイル時の警告も同様に補足するためにBEGINブロックに
180The following use of C<CGI::Carp> also redirects errors to a file of your choice,
181placed in a C<BEGIN> block to catch compile-time warnings as well:
182
183=end original
184
185以下の C<CGI::Carp> の使用例では、エラーをあなたの選択したファイルへ
186リダイレクトし、コンパイル時の警告も同様に補足するため C<BEGIN> ブロックに
91187置いています:
92188
93 BEGIN {
189 BEGIN {
94 use CGI::Carp qw(carpout);
190 use CGI::Carp qw(carpout);
95 open(LOG, ">>/var/local/cgi-logs/mycgi-log")
191 open(LOG, ">>/var/local/cgi-logs/mycgi-log")
96 or die "Unable to append to mycgi-log: $!\n";
192 or die "Unable to append to mycgi-log: $!\n";
97 carpout(*LOG);
193 carpout(*LOG);
98 }
194 }
99195
100深刻なエラーをクライアントのブラウザーに戻すように変更することもできます。
196=begin original
197
198You can even arrange for fatal errors to go back to the client browser,
199which is nice for your own debugging, but might confuse the end user.
200
201=end original
202
203深刻なエラーをクライアントのブラウザに戻すように変更することもできます。
101204これはあなたがデバッグするには良いでしょうが、エンドユーザーを
102205混乱させてしまうかもしれません。
103206
104 use CGI::Carp qw(fatalsToBrowser);
207 use CGI::Carp qw(fatalsToBrowser);
105 die "Bad error here";
208 die "Bad error here";
106209
107210=begin original
108211
109212Even if the error happens before you get the HTTP header out, the module
110213will try to take care of this to avoid the dreaded server 500 errors.
111214Normal warnings still go out to the server error log (or wherever
112215you've sent them with C<carpout>) with the application name and date
113216stamp prepended.
114217
115218=end original
116219
117あなたがHTTPヘッダーを受け取るよりも前にエラーが起こったとしても、
220あなたが HTTP ヘッダーを受け取るよりも前にエラーが起こったとしても、
118モジュールはサーバーの500エラーを避けるためにそのエラーを取り扱おうと
221モジュールはサーバーの 500 エラーを避けるためにそのエラーを取り扱おうと
119222するでしょう。
120通常の警告はサーバーのエラーログ(もしくはあなたがC<carpout>で指定した場所)に
223通常の警告はサーバーのエラーログ(もしくはあなたが C<carpout> で指定した場所)に
121224アプリケーションの名前と日付を伴って送られます。
122225
123226=head2 How do I remove HTML from a string?
124227
125(ある文字列からHTMLを取り除くには?)
228(ある文字列から HTML を取り除くには?)
126229
127230=begin original
128231
129The most correct way (albeit not the fastest) is to use HTML::Parser
232The most correct way (albeit not the fastest) is to use C<HTML::Parser>
130233from CPAN. Another mostly correct
131way is to use HTML::FormatText which not only removes HTML but also
234way is to use C<HTML::FormatText> which not only removes HTML but also
132235attempts to do a little simple formatting of the resulting plain text.
133236
134237=end original
135238
136(最速ではありませんが)最も正しい方法は、CPANにあるHTML::Parserを
239(最速ではありませんが)最も正しい方法は、CPAN にある C<HTML::Parser>
137240使うというものです。
138もう一つのまず正しい方法は、HTML::FormatText を使って HTML を
241もう一つのまず正しい方法は、C<HTML::FormatText> を使って HTML を
139242取り除くだけでなく、結果のプレーンテキストを簡単に整形することです。
140243
141多くの人が、C<< s/<.*?>//g >>のような単純な(simple-minded)正規表現を
244=begin original
245
246Many folks attempt a simple-minded regular expression approach, like
247C<< s/<.*?>//g >>, but that fails in many cases because the tags
248may continue over line breaks, they may contain quoted angle-brackets,
249or HTML comments may be present. Plus, folks forget to convert
250entities--like C<&lt;> for example.
251
252=end original
253
254多くの人が、C<< s/<.*?>//g >> のような単純な(simple-minded)正規表現を
142255使ったアプローチを行おうとするのですが、これは多くの場合
143失敗していまいます。なぜなら、タグは行をまたがって継続する可能性があり、
256失敗していまいます。
257なぜなら、タグは行をまたがって継続する可能性があり、
144258クォートされたアングルブラケットを含む可能性があり、
145259HTML のコメントがあるかもしれないからです。
146さらに、人々はC<&lt;>のようなエンティティを変換することを忘れてしまうのです。
260さらに、人々は C<&lt;> のようなエンティティを変換することを忘れてしまうのです。
147261
148以下の例は “単純な”アプローチで、ほとんどのファイルに対しては
262=begin original
263
264Here's one "simple-minded" approach, that works for most files:
265
266=end original
267
268以下の例は「単純な」アプローチで、ほとんどのファイルに対しては
149269うまくいきます:
150270
151 #!/usr/bin/perl -p0777
271 #!/usr/bin/perl -p0777
152 s/<(?:[^>'"]*|(['"]).*?\1)*>//gs
272 s/<(?:[^>'"]*|(['"]).*?\g1)*>//gs
153273
274=begin original
275
276If you want a more complete solution, see the 3-stage striphtml
277program in
278http://www.cpan.org/authors/Tom_Christiansen/scripts/striphtml.gz
279.
280
281=end original
282
154283もし、より完璧な解決策を求めているのなら、
155http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/striphtml.gz
284http://www.cpan.org/authors/Tom_Christiansen/scripts/striphtml.gz
156285にある 3-stage striphtml プログラムを参照してみてください。
157286
158287=begin original
159288
160289Here are some tricky cases that you should think about when picking
161290a solution:
162291
163292=end original
164293
165294以下に挙げたのは、あなたが自分でやろうとしたときに
166295考慮すべきであろうトリッキーな例です:
167296
168 <IMG SRC = "foo.gif" ALT = "A > B">
297 <IMG SRC = "foo.gif" ALT = "A > B">
169298
170 <IMG SRC = "foo.gif"
299 <IMG SRC = "foo.gif"
171300 ALT = "A > B">
172301
173 <!-- <A comment> -->
302 <!-- <A comment> -->
174303
175 <script>if (a<b && a>c)</script>
304 <script>if (a<b && a>c)</script>
176305
177 <# Just data #>
306 <# Just data #>
178307
179 <![INCLUDE CDATA [ >>>>>>>>>>>> ]]>
308 <![INCLUDE CDATA [ >>>>>>>>>>>> ]]>
180309
181310=begin original
182311
183312If HTML comments include other tags, those solutions would also break
184313on text like this:
185314
186315=end original
187316
188以下のテキストのようにHTMLのコメントが他のタグを含んでいた場合には、
317以下のテキストのように HTML のコメントが他のタグを含んでいた場合には、
189318せっかくの対応策もダメにしてしまうかもしれません:
190319
191 <!-- This section commented out.
320 <!-- This section commented out.
192 <B>You can't see me!</B>
321 <B>You can't see me!</B>
193 -->
322 -->
194323
195324=head2 How do I extract URLs?
196325
197(URLの展開を行うには?)
326(URL の展開を行うには?)
198327
199素早いけど完璧ではないやり方はこうです
328=begin original
200329
201 #!/usr/bin/perl -n00
330You can easily extract all sorts of URLs from HTML with
202 # qxurl - tchrist@perl.com
331C<HTML::SimpleLinkExtor> which handles anchors, images, objects,
203 print "$2\n" while m{
332frames, and many other tags that can contain a URL. If you need
204 < \s*
333anything more complex, you can create your own subclass of
205 A \s+ HREF \s* = \s* (["']) (.*?) \1
334C<HTML::LinkExtor> or C<HTML::Parser>. You might even use
206 \s* >
335C<HTML::SimpleLinkExtor> as an example for something specifically
207 }gsix;
336suited to your needs.
208337
338=end original
339
340アンカー、イメージ、オブジェクト、フレーム、およびその他の URL を含む
341多くのタグを扱える C<HTML::SimpleLinkExtor> を使って、HTML からあらゆる
342種類の URL を簡単に抽出できます。
343もしもっと複雑なものが必要なら、自分自身で C<HTML::LinkExtor> や
344C<HTML::Parser> のサブクラスを作れます。
345例えば、あなたの用途に特に適用するなら、C<HTML::SimpleLinkExtor> を
346使うこともできます。
347
209348=begin original
210349
211This version does not adjust relative URLs, understand alternate
350You can use C<URI::Find> to extract URLs from an arbitrary text document.
212bases, deal with HTML comments, deal with HREF and NAME attributes
213in the same tag, understand extra qualifiers like TARGET, or accept
214URLs themselves as arguments. It also runs about 100x faster than a
215more "complete" solution using the LWP suite of modules, such as the
216http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/xurl.gz program.
217351
218352=end original
219353
220これは相対URLを扱うことができずalternate baseを理解できず、HTML のコメント、
354任意のテキスト文書から URL 抽出するためにはC<URI::Find> が使えます。
221同じタグにあるHREFやNAMEといったアトリビュートを扱うことができませんし、
222TARGET のような追加の修飾子を理解せず、
223URL自身を引数として受け取ることができません。
224これは
225http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/xurl.gz
226のような、LWPスイートにあるモジュールを使っている、より“完全な”
227解決策よりも百倍の早さで実行されます。
228355
356=begin original
357
358Less complete solutions involving regular expressions can save
359you a lot of processing time if you know that the input is simple. One
360solution from Tom Christiansen runs 100 times faster than most
361module-based approaches but only extracts URLs from anchors where the first
362attribute is HREF and there are no other attributes.
363
364=end original
365
366もし入力が単純であることが分かっているなら、正規表現を使ったより不完全な
367解法によって多くの処理時間を節約できます。
368Tom Christiansen による一つの解法は、モジュールを使った手法よりも 100 倍
369速いですが、最初の属性が HREF で、その他の属性がないアンカーの URL のみを
370抽出します。
371
372 #!/usr/bin/perl -n00
373 # qxurl - tchrist@perl.com
374 print "$2\n" while m{
375 < \s*
376 A \s+ HREF \s* = \s* (["']) (.*?) \g1
377 \s* >
378 }gsix;
379
229380=head2 How do I download a file from the user's machine? How do I open a file on another machine?
230381
231(ユーザーのマシンからファイルをダウンロードするには?
382(ユーザーのマシンからファイルをダウンロードするには? 別のマシンにあるファイルをオープンするには?)
232別のマシンにあるファイルをオープンするには?)
233383
234HTMLフォームのコンテキストにあるのならば、B<multipart/form-data>
384=begin original
385
386In this case, download means to use the file upload feature of HTML
387forms. You allow the web surfer to specify a file to send to your web
388server. To you it looks like a download, and to the user it looks
389like an upload. No matter what you call it, you do it with what's
390known as B<multipart/form-data> encoding. The C<CGI.pm> module (which
391comes with Perl as part of the Standard Library) supports this in the
392C<start_multipart_form()> method, which isn't the same as the C<startform()>
393method.
394
395=end original
396
397この場合、ダウンロードというのは HTML フォームのファイルアップロード機能を
398使うということを意味します。
399Web サーファーに、Web サーバーに送るファイルを指定できるようにします。
400あなたにとってダウンロードに見えるものは、ユーザーにとってはアップロードに
401見えます。
402何と呼ぶかには関わらず、B<multipart/form-data>
235403エンコーディングとして知られているものを使うことができるでしょう。
236CGI.pmモジュール(CPANで入手可能です)はこれを start_multipart_form()
404C<CGI.pm> モジュール(標準ライブラリになっています)はこれを
237というstarform()メソッドは異なるメソッドでサポートしてます。
405C<start_multipart_form()> とい
406C<starform()> メソッドとは異なるメソッドでサポートしています。
238407
239=head2 How do I make a pop-up menu in HTML?
408=begin original
240409
241(HTMLでポップアップメニューを作るには?)
410See the section in the C<CGI.pm> documentation on file uploads for code
411examples and details.
242412
413=end original
414
415コードのサンプルと詳細については、C<CGI.pm> の文書のファイルアップロードの
416章を参照してください。
417
418=head2 How do I make an HTML pop-up menu with Perl?
419
420(Perl で HTML のポップアップメニューを作るには?)
421
243422=begin original
244423
245Use the B<< <SELECT> >> and B<< <OPTION> >> tags. The CGI.pm
424(contributed by brian d foy)
246module (available from CPAN) supports this widget, as well as many
247others, including some that it cleverly synthesizes on its own.
248425
249426=end original
250427
251B<< <SELECT> >> タグと B<< <OPTION> >> タグを使いす。
428(brian d foy によって寄贈されした)
252CGI.pmモジュール(CPANから入手可能です)はこのウィジェットを他の多くのものと同様、
253独自のメニューを器用に統合しているものを含めてサポートしています。
254429
430=begin original
255431
432The C<CGI.pm> module (which comes with Perl) has functions to create
433the HTML form widgets. See the C<CGI.pm> documentation for more
434examples.
435
436=end original
437
438C<CGI.pm> モジュール(標準配布です)には HTML フォームウィジェットを作るための
439関数があります。
440更なる例については C<CGI.pm> の文書を参照してください。
441
442 use CGI qw/:standard/;
443 print header,
444 start_html('Favorite Animals'),
445
446 start_form,
447 "What's your favorite animal? ",
448 popup_menu(
449 -name => 'animal',
450 -values => [ qw( Llama Alpaca Camel Ram ) ]
451 ),
452 submit,
453
454 end_form,
455 end_html;
456
256457=head2 How do I fetch an HTML file?
257458
258(HTMLファイルをフェッチするには?)
459(HTML ファイルをフェッチするには?)
259460
260一つのやり方は、あなたがlynxというテキストベースのHTMLブラウザーを
461=begin original
261インストールしているとすれば、次のようなものです:
262462
263 $html_code = `lynx -source $url`;
463(contributed by brian d foy)
264 $text_data = `lynx -dump $url`;
265464
465=end original
466
467(brian d foy によって寄贈されました)
468
266469=begin original
267470
268The libwww-perl (LWP) modules from CPAN provide a more powerful way
471Use the libwww-perl distribution. The C<LWP::Simple> module can fetch web
269to do this. They don't require lynx, but like lynx, can still work
472resources and give their content back to you as a string:
270through proxies:
271473
272474=end original
273475
274CPANで入手できる libwww-perl (LWP)モジュールはこれ行う、
476libwww-perl ディストリビューション使ってください。
275よりパワルな方法提供ます。
477C<LWP::Simple> はウェブ上のリソースをェッチして、その内容文字列と
276これは lynx を必要とせず、プロキシ越でも使えます:
478します:
277479
278 # 最も単純なバージョン
480 use LWP::Simple qw(get);
279 use LWP::Simple;
280 $content = get($URL);
281481
282 # もしくはURLからHTMLを出力する
482 my $html = get( "http://www.example.com/index.html" );
283 use LWP::Simple;
284 getprint "http://www.linpro.no/lwp/";
285483
286 # もしくはあるURLのHTMLからASCIIを印字する
484=begin original
287 # HTML-Treeパッケージも必要です。
288 use LWP::Simple;
289 use HTML::Parser;
290 use HTML::FormatText;
291 my ($html, $ascii);
292 $html = get("http://www.perl.com/");
293 defined $html
294 or die "Can't fetch HTML from http://www.perl.com/";
295 $ascii = HTML::FormatText->new->format(parse_html($html));
296 print $ascii;
297485
486It can also store the resource directly in a file:
487
488=end original
489
490リソースを直接ファイルに保存することもできます:
491
492 use LWP::Simple qw(getstore);
493
494 getstore( "http://www.example.com/index.html", "foo.html" );
495
496=begin original
497
498If you need to do something more complicated, you can use
499C<LWP::UserAgent> module to create your own user-agent (e.g. browser)
500to get the job done. If you want to simulate an interactive web
501browser, you can use the C<WWW::Mechanize> module.
502
503=end original
504
505もっと複雑なことをする必要がある場合は、仕事を行うための独自の
506ユーザーエージェント(例えばブラウザ)を作るために
507C<LWP::UserAgent> モジュールを使えます。
508対話的なウェブブラウザをシミュレートしたい場合は、
509C<WWW::Mechanize> モジュールが使えます。
510
298511=head2 How do I automate an HTML form submission?
299512
300(HTMLフォームの処理を自動化するには?)
513(HTML フォームの処理を自動化するには?)
301514
302GETメソッドを使って値を処理しているのであれば、URLを作って、
515=begin original
303さらにC<query_form>メソッドを使ってフォームをエンコードします:
304516
305 use LWP::Simple;
517If you are doing something complex, such as moving through many pages
306 use URI::URL;
518and forms or a web site, you can use C<WWW::Mechanize>. See its
519documentation for all the details.
307520
308 my $url = url('http://www.perl.com/cgi-bin/cpan_mod');
521=end original
309 $url->query_form(module => 'DB_File', readme => 1);
310 $content = get($url);
311522
312POSTメソッドを使っているのであれば自分用ージェントを作成して
523もし複数ージとフォームや web サイトを移動するような、複雑なことを
524しようとしているなら、C<WWW::Mechanize> が使えます。
525全ての詳細についてはこれのドキュメントを参照してください。
526
527=begin original
528
529If you're submitting values using the GET method, create a URL and encode
530the form using the C<query_form> method:
531
532=end original
533
534GET メソッドを使って値を処理しているのであれば、URL を作って、
535さらに C<query_form> メソッドを使ってフォームをエンコードします:
536
537 use LWP::Simple;
538 use URI::URL;
539
540 my $url = url('http://www.perl.com/cgi-bin/cpan_mod');
541 $url->query_form(module => 'DB_File', readme => 1);
542 $content = get($url);
543
544=begin original
545
546If you're using the POST method, create your own user agent and encode
547the content appropriately.
548
549=end original
550
551POST メソッドを使っているのであれば、自分用のエージェントを作成して
313552コンテンツを適切にエンコードしてやります。
314553
315 use HTTP::Request::Common qw(POST);
554 use HTTP::Request::Common qw(POST);
316 use LWP::UserAgent;
555 use LWP::UserAgent;
317556
318 $ua = LWP::UserAgent->new();
557 $ua = LWP::UserAgent->new();
319 my $req = POST 'http://www.perl.com/cgi-bin/cpan_mod',
558 my $req = POST 'http://www.perl.com/cgi-bin/cpan_mod',
320 [ module => 'DB_File', readme => 1 ];
559 [ module => 'DB_File', readme => 1 ];
321 $content = $ua->request($req)->as_string;
560 $content = $ua->request($req)->as_string;
322561
323=head2 how do I decode or create those %-encodings on the web?
562=head2 How do I decode or create those %-encodings on the web?
563X<URI> X<CGI.pm> X<CGI> X<URI::Escape> X<RFC 2396>
324564
325565(web上で %-encodings をデコードしたり生成したりするには?)
326566
327567=begin original
328568
329If you are writing a CGI script, you should be using the CGI.pm module
569(contributed by brian d foy)
330that comes with perl, or some other equivalent module. The CGI module
331automatically decodes queries for you, and provides an escape()
332function to handle encoding.
333570
334571=end original
335572
336CGI スクリプトを書いているのなら、perl に付属している CGI.pm モジュール
573(brian d foy によって寄贈されました)
337または等価なモジュールを使うべきです。
338CGI モジュールは自動的にクエリをデコードし、escape() 関数で
339エンコードもできます。
340574
341575=begin original
342576
343The best source of detailed information on URI encoding is RFC 2396.
577Those C<%> encodings handle reserved characters in URIs, as described
344Basically, the following substitutions do it:
578in RFC 2396, Section 2. This encoding replaces the reserved character
579with the hexadecimal representation of the character's number from
580the US-ASCII table. For instance, a colon, C<:>, becomes C<%3A>.
345581
346582=end original
347583
348URI エンコーディング詳細情報に関する最良のソースは RFC 2396 です。
584これら C<%> 符号化は RFC 2396 の 2 章に記述されている、
349基本的には、以下の置換が実行されます:
585URI として予約されている文字を扱います
586この符号化は予約文字を、その文字の番号の 16 進表現に
587US-ASCII テーブルを使って置き換えます。
588例えば、コロン (C<:>) は C<%3A> になります。
350589
351 s/([^\w()'*~!.-])/sprintf '%%%02x', $1/eg; # encode
590=begin original
352591
353 s/%([A-Fa-f\d]{2})/chr hex $1/eg; # decode
592In CGI scripts, you don't have to worry about decoding URIs if you are
593using C<CGI.pm>. You shouldn't have to process the URI yourself,
594either on the way in or the way out.
354595
596=end original
597
598CGI スクリプトでは、C<CGI.pm> を使っているなら URL のデコードについて
599気にする必要はありません。
600エンコードにしてもデコードにしても、URI を自分で処理する必要はないはずです。
601
355602=begin original
356603
357However, you should only apply them to individual URI components, not
604If you have to encode a string yourself, remember that you should
358the entire URI, otherwise you'll lose information and generally mess
605never try to encode an already-composed URI. You need to escape the
359things up. If that didn't explain it, don't worry. Just go read
606components separately then put them together. To encode a string, you
360section 2 of the RFC, it's probably the best explanation there is.
607can use the C<URI::Escape> module. The C<uri_escape> function
608returns the escaped string:
361609
362610=end original
363611
364かし、これは URI 全体はなく、個々の URI コネントに
612文字列を自分ドする必要があるなら、
365して適用するべきです。さもければ、情報が失われ、
613してすでに構成された URI をエンコードするべきでいことを
366ぐちゃぐちゃにります
614忘れいでください
367これが説明なっいなくても心配はいりま
615要素を別々エスケープして、それから全てを合わます
368RFC の第 2 章を読んでください
616文字列をエンコードするには、C<URI::Escape> モジュールが使えます
369おそらくこの問題に関する最良の説明です。
617C<uri_escape> 数はエスケープされた文字列を返しま:
370618
619 my $original = "Colon : Hash # Percent %";
620
621 my $escaped = uri_escape( $original );
622
623 print "$escaped\n"; # 'Colon%20%3A%20Hash%20%23%20Percent%20%25'
624
371625=begin original
372626
373RFC 2396 also contains a lot of other useful information, including a
627To decode the string, use the C<uri_unescape> function:
374regexp for breaking any arbitrary URI into components (Appendix B).
375628
376629=end original
377630
378RFC 2396 にはその他の有用な情報が多く含まれています。その中には
631文字列をデコードするには、C<uri_unescape> 関数を使います:
379任意の URI をコンポーネントに分割するための正規表現(Appendix B)を含みます。
380632
381=head2 How do I redirect to another page?
633 my $unescaped = uri_unescape( $escaped );
382634
383(別のページにリダイレクトするには?)
635 print $unescaped; # back to original
384636
385637=begin original
386638
387According to RFC 2616, "Hypertext Transfer Protocol -- HTTP/1.1", the
639If you wanted to do it yourself, you simply need to replace the
388preferred method is to send a C<Location:> header instead of a
640reserved characters with their encodings. A global substitution
389C<Content-Type:> header:
641is one way to do it:
390642
391643=end original
392644
393RFC 2616 "Hypertext Transfer Protocol -- HTTP/1.1" によ
645もしこを自分でするなら単に予約文字をエンコードされた物に置き換えます。
394好ましい手法は C<Content-Type:> ヘッダ代わり C<Location:> ヘッダを
646全体を一度に変換するなら、以下ようします:
395送ることです:
396647
397 Location: http://www.domain.com/newpage
648 # encode
649 $string =~ s/([^^A-Za-z0-9\-_.!~*'()])/ sprintf "%%%0x", ord $1 /eg;
398650
651 #decode
652 $string =~ s/%([A-Fa-f\d]{2})/chr hex $1/eg;
653
654=head2 How do I redirect to another page?
655
656(別のページにリダイレクトするには?)
657
399658=begin original
400659
401Note that relative URLs in these headers can cause strange effects
660Specify the complete URL of the destination (even if it is on the same
402because of "optimizations" that servers do.
661server). This is one of the two different kinds of CGI "Location:"
662responses which are defined in the CGI specification for a Parsed Headers
663script. The other kind (an absolute URLpath) is resolved internally to
664the server without any HTTP redirection. The CGI specifications do not
665allow relative URLs in either case.
403666
404667=end original
405668
406これらのヘッダに相対 URL を指定すると、サーバーが行う“最適化”のために
669(たとえ同じサバでも)宛て先の完全な URL を指定してください。
407奇妙な効果を引き起す可能性があるこに注意してくだい。
670れは Parsed Headers スクリプトとして CGI 仕様に定義れた二つの異なった
408
671CGI "Location:" レスポンスのうちの一つです。
409 $url = "http://www.perl.com/CPAN/";
672その他の種類 (絶対 URL パス) HTTP リダイレクトなしにサーバによって
410 print "Location: $url\n\n";
673内部的に解決されます。
411 exit;
674CGI 仕様ではどちらの場合でも相対 URL は認められていません。
412675
413676=begin original
414677
415To target a particular frame in a frameset, include the "Window-target:"
678Use of C<CGI.pm> is strongly recommended. This example shows redirection
416in the header.
679with a complete URL. This redirection is handled by the web browser.
417680
418681=end original
419682
420フレームセットの特定のフレーム指定るには、
683C<CGI.pm> 使うことを強くお勧めしま
421ヘッダーに "Window-target:"含めます。
684この例では完全な URL へのリダイレクト行います。
685このリダイレクトは web ブラウザによって扱われます。
422686
423 print <<EOF;
687 use CGI qw/:standard/;
424 Location: http://www.domain.com/newpage
425 Window-target: <FrameName>
426688
427 EOF
689 my $url = 'http://www.cpan.org/';
690 print redirect($url);
428691
429692=begin original
430693
431To be correct to the spec, each of those virtual newlines should
694This example shows a redirection with an absolute URLpath. This
432really be physical C<"\015\012"> sequences by the time your message is
695redirection is handled by the local web server.
433received by the client browser. Except for NPH scripts, though, that
434local newline should get translated by your server into standard form,
435so you shouldn't have a problem here, even if you are stuck on MacOS.
436Everybody else probably won't even notice.
437696
438697=end original
439698
440仕様を正確に達成するに、クラアンブラウザからメッセージ受け取った時点で
699この例で絶対 URL パスへのリダレクトを行います。
441全て仮想的な改行ついC<"\015\012">に置き換えるべきです。
700リダイレクトはローカルの web サーバよっ行われます。
442しかし、NPH スクリプトを除いて、このようなローカルな改行は
443サーバーによって標準的な形式に変換されることが多いので、
444MacOS ではまっていた場合でも問題にはならないことが多いでしょう。
445それ以外ではおそらく誰も気付きすらしないでしょう。
446701
702 my $url = '/CPAN/index.html';
703 print redirect($url);
704
705=begin original
706
707But if coded directly, it could be as follows (the final "\n" is
708shown separately, for clarity), using either a complete URL or
709an absolute URLpath.
710
711=end original
712
713しかし、直接コーディングするなら、完全な URL か絶対 URLpath を使って、
714以下のようになります(最後の "\n" は明確化するために分けて表示しています)。
715
716 print "Location: $url\n"; # CGI response header
717 print "\n"; # end of headers
718
447719=head2 How do I put a password on my web pages?
448720
449(私のwebぺージでパスワードを入力するには?)
721(私の web ぺージでパスワードを入力するには?)
450722
451状況によります。あなたの使っているwebサーバーのドキュメントを読む
723=begin original
452必要があるでしょうし、先に例示した他のFAQの幾つかをチェックする
453必要もあるでしょう。
454724
725To enable authentication for your web server, you need to configure
726your web server. The configuration is different for different sorts
727of web servers--apache does it differently from iPlanet which does
728it differently from IIS. Check your web server documentation for
729the details for your particular server.
730
731=end original
732
733利用する Web サーバーで認証を有効にするには、Web サーバーを設定することが
734必要です。
735web サーバの種類によって設定は異なります -- apache は iPlanet とは異なり、
736また IIS とも異なります。
737特定のサーバーに関する詳細については、そのサーバーのドキュメントを
738チェックしてください。
739
455740=head2 How do I edit my .htpasswd and .htgroup files with Perl?
456741
457(Perlを使って.htpasswdや.htgroupといったファイルを編集するには?)
742(Perl を使って .htpasswd .htgroup といったファイルを編集するには?)
458743
459HTTPD::UserAdmin モジュールと HTTPD::GroupAdmin モジュールは、
744=begin original
745
746The C<HTTPD::UserAdmin> and C<HTTPD::GroupAdmin> modules provide a
747consistent OO interface to these files, regardless of how they're
748stored. Databases may be text, dbm, Berkeley DB or any database with
749a DBI compatible driver. C<HTTPD::UserAdmin> supports files used by the
750"Basic" and "Digest" authentication schemes. Here's an example:
751
752=end original
753
754C<HTTPD::UserAdmin> モジュールと C<HTTPD::GroupAdmin> モジュールは、
460755ファイルがどのように格納されているかに関係なくこれらのファイルに対する
461756首尾一貫したオブジェクト指向インターフェースを提供します。
462HTTPD::UserAdmin は`Basic' および `Digest' 認証スキームで
757データベースはテキスト、dbm、Berkeley DB、あるいは DBI 互換ドライバのある
463使われるファイルをサポトしす。以下に例を挙げす:
758どんなデタベースでもかせん。
759C<HTTPD::UserAdmin> は "Basic" および "Digest" 認証スキームで
760使われるファイルをサポートします。
761以下に例を挙げます:
464762
465 use HTTPD::UserAdmin ();
763 use HTTPD::UserAdmin ();
466 HTTPD::UserAdmin
764 HTTPD::UserAdmin
467765 ->new(DB => "/foo/.htpasswd")
468766 ->add($username => $password);
469767
470768=head2 How do I make sure users can't enter values into a form that cause my CGI script to do bad things?
471769
472(私のCGIスクリプトに悪影響をもたらすようなものを、ユーザーが
770(私の CGI スクリプトに悪影響をもたらすようなものを、ユーザーがフォームに入力できないようにするには?)
473フォームに入力できないようにするにはどうするのでしょうか?)
474771
475http://www-genome.wi.mit.edu/WWW/faqs/www-security-faq.html,
772=begin original
476にある CGI セキュリティ FAQを読んでください。そして
477http://www.perl.com/CPAN/doc/FAQs/cgi/perl-cgi-faq.html.
478にあるPerl/CGI FAQを読んでください。
479773
480まとめ: スクリプトの外側(たとえばCGIパラメーター)からやってくる
774(contributed by brian d foy)
481データを使ってC<eval>やC<system>を使わないようにさせる汚染検査
482(tainting、L<perlsec>を参照してください)を使います。汚染検査に加え、
483一引数形式の system() や exec() を決して使わないようにします。
484その代わりに、シェルのグロビングが行われないように、
485リストとしてコマンドと引数とを与えるようにします。
486775
487=head2 How do I parse an mail header?
776=end original
488777
778(brian d foy によって寄贈されました)
779
780=begin original
781
782You can't prevent people from sending your script bad data. Even if
783you add some client-side checks, people may disable them or bypass
784them completely. For instance, someone might use a module such as
785C<LWP> to access your CGI program. If you want to prevent data that
786try to use SQL injection or other sorts of attacks (and you should
787want to), you have to not trust any data that enter your program.
788
789=end original
790
791人々がスクリプトに悪いデータを送るのを防ぐことは出来ません。
792例えクライアント側でチェックをしたとしても、それを無効にしたり
793完全に回避したりできます。
794例えば、CGI プログラムに C<LWP> のようなモジュールを使うかもしれません。
795SQL インジェクションや同様の種類の攻撃を行うようなデータを防ぎたいのなら
796(そしておそらくそうしたいでしょう)、プログラムに入力される
797どんなデータも信用しないようにする必要があります。
798
799=begin original
800
801The L<perlsec> documentation has general advice about data security.
802If you are using the C<DBI> module, use placeholder to fill in data.
803If you are running external programs with C<system> or C<exec>, use
804the list forms. There are many other precautions that you should take,
805too many to list here, and most of them fall under the category of not
806using any data that you don't intend to use. Trust no one.
807
808=end original
809
810L<perlsec> 文書にはデータセキュリティに関する一般的な助言があります。
811C<DBI> モジュールを使っているなら、データを埋めるのにプレースホルダを
812使ってください。
813C<system> や C<exec> で外部プログラムを実行しているなら、リスト形式を
814使ってください。
815その他、ここに書ききれないほどの注意するべき多くの事前注意があり、
816そのほとんどは、使おうとしていないデータは使わないという分野に当てはまります。
817誰も信用しないでください。
818
819=head2 How do I parse a mail header?
820
489821(メールのヘッダーを解析するには?)
490822
823=begin original
824
825For a quick-and-dirty solution, try this solution derived
826from L<perlfunc/split>:
827
828=end original
829
491830拙速な解決策なら、L<perlfunc/split> から派生した
492以下のやり方を試してみてください
831以下のやり方を試してみてください:
493832
494 $/ = '';
833 $/ = '';
495 $header = <MSG>;
834 $header = <MSG>;
496 $header =~ s/\n\s+/ /g; # 継続行を連結する
835 $header =~ s/\n\s+/ /g; # merge continuation lines
497 %head = ( UNIX_FROM_LINE, split /^([-\w]+):\s*/m, $header );
836 %head = ( UNIX_FROM_LINE, split /^([-\w]+):\s*/m, $header );
498837
838=begin original
839
840That solution doesn't do well if, for example, you're trying to
841maintain all the Received lines. A more complete approach is to use
842the C<Mail::Header> module from CPAN (part of the C<MailTools> package).
843
844=end original
845
499846このやり方は、たとえば受信した行すべてを保守しようとするときには
500うまくありません。より完璧なアプローチはCPANにあるMail::Header
847うまくありません。
501モジュルを使うというものです(このモジュールはMailToolsパッケー
848より完璧なアプロ CPAN にある C<Mail::Header> モュールを
502一部です)。
849使うというものです(このモジュールは C<MailTools> パッケージの一部です)。
503850
504851=head2 How do I decode a CGI form?
505852
506(CGIフォームをデコードするには?)
853(CGI フォームをデコードするには?)
507854
508855=begin original
509856
510You use a standard module, probably CGI.pm. Under no circumstances
857(contributed by brian d foy)
511should you attempt to do so by hand!
512858
513859=end original
514860
515標準のモジュール、多分CGI.pmを使うといでょう。
861(brian d foy にって寄贈されまた)
516あなたが手作業で行うことは多分ありません!
517862
518863=begin original
519864
520You'll see a lot of CGI programs that blindly read from STDIN the number
865Use the C<CGI.pm> module that comes with Perl. It's quick,
521of bytes equal to CONTENT_LENGTH for POSTs, or grab QUERY_STRING for
866it's easy, and it actually does quite a bit of work to
522decoding GETs. These programs are very poorly written. They only work
867ensure things happen correctly. It handles GET, POST, and
523sometimes. They typically forget to check the return value of the read()
868HEAD requests, multipart forms, multivalued fields, query
524system call, which is a cardinal sin. They don't handle HEAD requests.
869string and message body combinations, and many other things
525They don't handle multipart forms used for file uploads. They don't deal
870you probably don't want to think about.
526with GET/POST combinations where query fields are in more than one place.
527They don't deal with keywords in the query string.
528871
529872=end original
530873
531あなたはPOST対するCONTENT_LENGTHと同じだけのバイト標準入力から
874Perl 同梱されてい C<CGI.pm> モジュール使いましょう。
532闇雲に読み込んだりGETデコードするためにQUERY_STRINGを取り込んでいるような
875これは早く簡単で、物事が正しく行われること確実にするため
533CGIプログラムを見るこになるでょう
876ちょっとした作業を行います
534これらのプログラは非常にまずい書き方をされていて
877GET, POST, HEAD リクエスト、マルチパートフォー、複数値フィールド
535部分的にしか動作しまん。これらプログラムで典型的なも
878クエリ文字列とメッセージボディの組み合わ、およびその、
536read()システムコール戻り値チェックするのを忘れていたりします。
879あなたが考えようとも思わないような多く事柄扱えます。
537それは大きな罪なのです。また、HEADリクエストを
538取り扱っていなかったりする場合もありますし、
539ファイルのアップロードに使われる
540マルチパート形式を扱ってないこともあります。
541あるいは二ヶ所以上で
542問い合わせフィールドがある場合のGET/POSTの組み合わせに対応していません。
543さらに問い合わせ文字列中にあるキーワードの扱いもしていません。
544880
545881=begin original
546882
547In short, they're bad hacks. Resist them at all costs. Please do not be
883It doesn't get much easier: the C<CGI.pm> module automatically
548tempted to reinvent the wheel. Instead, use the CGI.pm or CGI_Lite.pm
884parses the input and makes each value available through the
549(available from CPAN), or if you're trapped in the module-free land
885C<param()> function.
550of perl1 .. perl4, you might look into cgi-lib.pl (available from
551http://www.bio.cam.ac.uk/web/form.html).
552886
553887=end original
554888
555端的にいって、そ良く hack です。行わないようにしましょう。
889以上簡単にはなりません: C<CGI.pm> モジュールは入力を自動的パースて、
556どうか車輪再発明するようなことはないでください
890それぞれの値 C<param()> 関数を通て利用可能にします
557代わりに、CGI.pm か CGI_Lite.pm (CPAN で入手できます)を使ってください。
558あなたが(モジュールを使うことのできない)perl1 〜 perl4 を使っているのなら、
559cgi-lib.pl を調べてみてください
560(http://cgi-lib.stanford.edu/cgi-lib/で入手可能です)
561891
892 use CGI qw(:standard);
893
894 my $total = param( 'price' ) + param( 'shipping' );
895
896 my @items = param( 'item' ); # multiple values, same field name
897
562898=begin original
563899
564Make sure you know whether to use a GET or a POST in your form.
900If you want an object-oriented approach, C<CGI.pm> can do that too.
565GETs should only be used for something that doesn't update the server.
566Otherwise you can get mangled databases and repeated feedback mail
567messages. The fancy word for this is ``idempotency''. This simply
568means that there should be no difference between making a GET request
569for a particular URL once or multiple times. This is because the
570HTTP protocol definition says that a GET request may be cached by the
571browser, or server, or an intervening proxy. POST requests cannot be
572cached, because each request is independent and matters. Typically,
573POST requests change or depend on state on the server (query or update
574a database, send mail, or purchase a computer).
575901
576902=end original
577903
578なたのフォームで GET や POST をどのように使うかを良く知っておいてください
904オブジェクト指向手法が使いいなら、C<CGI.pm> はそのようにもできます
579GET はサーバーを更新しないようなものに対してのみ使うべきです。
580あるいは、データベースをいじったりメールメッセージによるフィードバックを
581繰り返し行うようなこともできます。このことを表わすのにぴったりな
582単語は“idempotency”です。その意味は、GET リクエストを作り出したものと
583特定の URL とに違いがないということです。
584これはつまり、HTTP プロトコルは GET リクエストがブラウザーや
585サーバー、あるいは途中にあるプロクシーによってキャッシングしている
586可能性があることを定義しています。POST リクエストは
587キャッシュすることはできません。なぜなら呼び出しのそれぞれは
588独立したものであるからなのです。
589典型的な例では、POST リクエストはサーバーの状態を変更したり、
590状態に依存しています(データベースに対する問い合わせや更新、メールの送信、
591コンピューターの購入など)。
592905
906 use CGI;
907
908 my $cgi = CGI->new();
909
910 my $total = $cgi->param( 'price' ) + $cgi->param( 'shipping' );
911
912 my @items = $cgi->param( 'item' );
913
914=begin original
915
916You might also try C<CGI::Minimal> which is a lightweight version
917of the same thing. Other CGI::* modules on CPAN might work better
918for you, too.
919
920=end original
921
922同じことをする軽量版の C<CGI::Minimal> も試したいかもしれません。
923CPAN にあるその他の CGI::* モジュールもあなたのためによく働くでしょう。
924
925=begin original
926
927Many people try to write their own decoder (or copy one from
928another program) and then run into one of the many "gotchas"
929of the task. It's much easier and less hassle to use C<CGI.pm>.
930
931=end original
932
933多くの人々が自分用のデコーダを書こうとします (あるいは他のプログラムから
934コピーしようとします); そしてこの作業の多くの「コツ」の一つに出くわすことに
935なります。
936C<CGI.pm> を使うことはより簡単で、面倒事も少なくなります。
937
593938=head2 How do I check a valid mail address?
594939
595940(メールアドレスが正しいかチェックするには?)
596941
597942=begin original
598943
599You can't, at least, not in real time. Bummer, eh?
944(partly contributed by Aaron Sherman)
600945
601946=end original
602947
603できません。少なくともリアルタイムで。がかりしたでしょう?
948(一部 Aaron Sherman によて寄贈されました)
604949
605950=begin original
606951
607Without sending mail to the address and seeing whether it bounces (and
952This isn't as simple a question as it sounds. There are two parts:
608even then you face the halting problem), you cannot determine whether
609an email address is valid. Even if you apply the email header
610standard, you can have problems, because there are deliverable
611addresses that aren't RFC-822 (the mail header standard) compliant,
612and addresses that aren't deliverable which are compliant.
613953
614954=end original
615955
616そのアドレスにメールを送ってそが届いうかを確認しければ
956は見目ほ単純質問ではありません。
617(そしてあなたがそhalting problemに直面したあとでさえも)、
957これは二つ部分からなります:
618そのアドレスが正しいものかどうかを決めることはできません。
619あなたが電子メールヘッダーの標準を適用したとしても、
620問題が起こる可能性があります。
621なぜなら、配達可能なアドレスには RFC-822(電子メールヘッダーの標準)に
622適合していないものがあり、適合していても配達できないアドレスも
623あるからです。
624958
625959=begin original
626960
627Many are tempted to try to eliminate many frequently-invalid
961a) How do I verify that an email address is correctly formatted?
628mail addresses with a simple regex, such as
629C</^[\w.-]+\@(?:[\w-]+\.)+\w+$/>. It's a very bad idea. However,
630this also throws out many valid ones, and says nothing about
631potential deliverability, so it is not suggested. Instead, see
632http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/ckaddr.gz,
633which actually checks against the full RFC spec (except for nested
634comments), looks for addresses you may not wish to accept mail to
635(say, Bill Clinton or your postmaster), and then makes sure that the
636hostname given can be looked up in the DNS MX records. It's not fast,
637but it works for what it tries to do.
638962
639963=end original
640964
641多くの人が 不正メールアドレスを C</^[\w.-]+\@(?:[\w-]+\.)+\w+$/>
965a) メールアドレスが正しい形式か検証するには?
642のように単純な正規表現で削除しようとしています。しかしながら、
643これは正当なアドレスも一緒に取り除いてしまい、
644潜在的な到達性についてなにも言いません。ですからこれはお薦めしません。
645代わりに、完全な RFC 仕様に突き合わせてチェックを行い
646(ネストしたコメントを除く)、
647あなたが受け取りたくないメールアドレスを探し出し、
648さらにホスト名が DNS で引けるかどうかを確かめる
649http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/ckaddr.gz
650を参照してください。これは速くはありませんが、しかしきちんと動きます。
651966
652967=begin original
653968
654Our best advice for verifying a person's mail address is to have them
969b) How do I verify that an email address targets a valid recipient?
655enter their address twice, just as you normally do to change a password.
656This usually weeds out typos. If both versions match, send
657mail to that address with a personal message that looks somewhat like:
658970
659971=end original
660972
661私たちができる最善のアドバイスは、個人のメールアドレスをチェックする
973b) メールアドレスが正当な受信者対象としているかを検証するには?
662パスワードを変更するときと同じように
663ユーザーにアドレスを二回入力させるというものです。
664これによって通常は打ち間違いを防ぐことができます。
665二回の入力がマッチしたなら、以下のような内容のメッセージをメールとして
666そのアドレスへ送ります:
667974
668 Dear someuser@host.com,
975=begin original
669976
977Without sending mail to the address and seeing whether there's a human
978on the other end to answer you, you cannot fully answer part I<b>, but
979either the C<Email::Valid> or the C<RFC::RFC822::Address> module will do
980both part I<a> and part I<b> as far as you can in real-time.
981
982=end original
983
984そのアドレスにメールを送ってそれが届いたかどうかを確認しなければ
985完全にパート I<b> に答えられませんが、C<Email::Valid> か
986C<RFC::RFC822::Address> のモジュールは、リアルタイムでできる限りの
987ことに対してパート I<a> とパート I<b> の両方を行います。
988
670989=begin original
671990
672 Please confirm the mail address you gave us Wed May 6 09:38:41
991If you want to just check part I<a> to see that the address is valid
673 MDT 1998 by replying to this message. Include the string
992according to the mail header standard with a simple regular expression,
674 "Rumpelstiltskin" in that reply, but spelled in reverse; that is,
993you can have problems, because there are deliverable addresses that
675 start with "Nik...". Once this is done, your confirmed address will
994aren't RFC-2822 (the latest mail header standard) compliant, and
676 be entered into our records.
995addresses that aren't deliverable which, are compliant. However, the
996following will match valid RFC-2822 addresses that do not have comments,
997folding whitespace, or any other obsolete or non-essential elements.
998This I<just> matches the address itself:
677999
6781000=end original
6791001
680 [あなたがMDTの1998年5月6日(水)9時38分41秒私たちに送たメ
1002もしあなたが単純な正規表現でアドレスがメールヘッダ標準ているかを
681 イルアドレスの確認をし、このメッセージリプライしてくださ
1003見ることでパート I<a>チェックたいなら問題を抱えるなります;
682 い。リプライは"Rumpelstiltskin" とう文字列を、"Nik..."の
1004なぜなら、RFC-2822 (最新のメールヘッダ標準)準拠してなけれども
683 よう逆順にして含めてくださ。そが済めば、あたのアドレ
1005配達可能なアドレスが存在し、標準準拠しているけども配達不能なアドレスも
684 スの確認は終了し、私たちのレコードに入力されます。]
1006存在るからです
1007しかし以下のコードは、コメント、折り畳みの空白、あるいはその他の時代遅れに
1008なっていたり本質的でない要素を含んでいない、有効な RFC-2822 アドレスに
1009マッチングします。
1010これは I<単に> アドレス自身にマッチングします:
6851011
686あなたがメッセージの返事を受け取り、そのメールの発信者があなたの
1012 my $atom = qr{[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+};
687指示に従っていれば、そのアドレスが本物であると信じることができるでしょう。
1013 my $dot_atom = qr{$atom(?:\.$atom)*};
1014 my $quoted = qr{"(?:\\[^\r\n]|[^\\"])*"};
1015 my $local = qr{(?:$dot_atom|$quoted)};
1016 my $quotedpair = qr{\\[\x00-\x09\x0B-\x0c\x0e-\x7e]};
1017 my $domain_lit = qr{\[(?:$quotedpair|[\x21-\x5a\x5e-\x7e])*\]};
1018 my $domain = qr{(?:$dot_atom|$domain_lit)};
1019 my $addr_spec = qr{$local\@$domain};
6881020
6891021=begin original
6901022
1023Just match an address against C</^${addr_spec}$/> to see if it follows
1024the RFC2822 specification. However, because it is impossible to be
1025sure that such a correctly formed address is actually the correct way
1026to reach a particular person or even has a mailbox associated with it,
1027you must be very careful about how you use this.
1028
1029=end original
1030
1031もしアドレスが RFC 2822 仕様に準拠しているかどうかを見たいなら、単に
1032C</^${addr_spec}$/> とマッチングさせてください。
1033しかし、このような正しい形式のアドレスが実際に特定の個人に届く
1034正しい方法なのか、あるいはその個人に関連付けられたメールボックスに
1035届くのかさえも明確にすることは不可能なので、これをどう使うかについては
1036とても慎重になる必要があります。
1037
1038=begin original
1039
1040Our best advice for verifying a person's mail address is to have them
1041enter their address twice, just as you normally do to change a
1042password. This usually weeds out typos. If both versions match, send
1043mail to that address with a personal message. If you get the message
1044back and they've followed your directions, you can be reasonably
1045assured that it's real.
1046
1047=end original
1048
1049私たちができる最善のアドバイスは、個人のメールアドレスをチェックするのに
1050パスワードを変更するときと同じようにユーザーにアドレスを
1051二回入力させるというものです。
1052これによって通常は打ち間違いを防ぐことができます。
1053二回の入力がマッチしたなら、個人的な内容のメッセージをメールとして
1054そのアドレスへ送ります。
1055もしメッセージが返ってきて、それがあなたの指示に従っているなら、
1056それが実際のものであると十分に仮定できます。
1057
1058=begin original
1059
6911060A related strategy that's less open to forgery is to give them a PIN
6921061(personal ID number). Record the address and PIN (best that it be a
693random one) for later processing. In the mail you send, ask them to
1062random one) for later processing. In the mail you send, ask them to
6941063include the PIN in their reply. But if it bounces, or the message is
695included via a ``vacation'' script, it'll be there anyway. So it's
1064included via a "vacation" script, it'll be there anyway. So it's
6961065best to ask them to mail back a slight alteration of the PIN, such as
6971066with the characters reversed, one added or subtracted to each digit, etc.
6981067
6991068=end original
7001069
701より偽造のやりにくい別のやり方に、チェックに対象者に対してPIN
1070より偽造のやりにくい別のやり方に、チェックに対象者に対して PIN
702(Personal ID Number)を与えるというものがあります。
1071(Personal ID Number) を与えるというものがあります。
703後の処理のためにアドレスとPIN(ランダムであることが望ましい)を記録しておくのです。
1072後の処理のためにアドレスと PIN (ランダムであることが望ましい)を
1073記録しておくのです。
7041074あなたがメールを送るときに、宛て先人に対して彼らの出すリプライに
705PINを含めるように依頼するのです。
1075PIN を含めるように依頼するのです。
706しかしそれがそのまま返ってきたり、
1076しかしそれがそのまま返ってきたり、あるいは返ってきたメッセージが
707あるいは返ってきたメッセージが``vacation'' スクリプトを通じての
1077"vacation" スクリプトを通じてのものであっても、そのまま PIN が
708ものであっても、そのまま PIN が含まれてしまいます。
1078含まれてしまいます。
709ですから、最善なやり方はメールを送るときに返事には
1079ですから、最善なやり方はメールを送るときに返事には文字を逆順にするとか、
710文字を逆順にするとか、各桁に対して足し算や引き算を行うなどして
1080各桁に対して足し算や引き算を行うなどして
711PINを変形したものを含めて返すように依頼するという方法です。
1081PIN を変形したものを含めて返すように依頼するという方法です。
7121082
7131083=head2 How do I decode a MIME/BASE64 string?
7141084
7151085(MIME/BASE64 文字列のデコードを行うには?)
7161086
7171087=begin original
7181088
719The MIME-Base64 package (available from CPAN) handles this as well as
1089The C<MIME-Base64> package (available from CPAN) handles this as well as
7201090the MIME/QP encoding. Decoding BASE64 becomes as simple as:
7211091
7221092=end original
7231093
724MIME-Base64 パッケージ(CPANで入手可能です)はこの問題と、
1094C<MIME-Base64> パッケージ(CPAN で入手可能です)はこの問題と、
7251095MIME/QP エンコーディングを取り扱います。
726BASE64のデコードは以下のように単純です:
1096BASE64 のデコードは以下のように単純です:
7271097
728 use MIME::Base64;
1098 use MIME::Base64;
729 $decoded = decode_base64($encoded);
1099 $decoded = decode_base64($encoded);
7301100
7311101=begin original
7321102
733The MIME-Tools package (available from CPAN) supports extraction with
1103The C<MIME-Tools> package (available from CPAN) supports extraction with
7341104decoding of BASE64 encoded attachments and content directly from email
7351105messages.
7361106
7371107=end original
7381108
739MIME-Tools パッケージ (CPAN にあります) は BASE64 エンコードされた
1109C<MIME-Tools> パッケージ (CPAN にあります) は BASE64 エンコードされた
7401110添付ファイルと本文をメールのメッセージから直接抽出できます。
7411111
742もしデコードしたい文字列が短い(84 文字以下)の場合、
1112=begin original
743より直接的なやり方は、ちょっとした変換をした後でunpack()関数の
744“u”フォーマットを使うというものです:
7451113
746 tr#A-Za-z0-9+/##cd; # base64でないキャラクターを取り除く
1114If the string to decode is short (less than 84 bytes long)
747 tr#A-Za-z0-9+/# -_#; # uuencodeフォーマットに変換
1115a more direct approach is to use the C<unpack()> function's "u"
748 $len = pack("c", 32 + 0.75*length); # 長さを計算する
1116format after minor transliterations:
749 print unpack("u", $len . $_); # uudecodeして出力
7501117
751=head2 How do I return the user's email address?
1118=end original
7521119
1120もしデコードしたい文字列が短い(84 文字以下)の場合、より直接的なやり方は、
1121ちょっとした変換をした後で C<unpack()> 関数の "u" フォーマットを
1122使うというものです:
1123
1124 tr#A-Za-z0-9+/##cd; # remove non-base64 chars
1125 tr#A-Za-z0-9+/# -_#; # convert to uuencoded format
1126 $len = pack("c", 32 + 0.75*length); # compute length byte
1127 print unpack("u", $len . $_); # uudecode and print
1128
1129=head2 How do I return the user's mail address?
1130
7531131(ユーザーのメールアドレスを返すには?)
7541132
755getpwuidをサポートしているシステムであれば、$< という変数と
1133=begin original
756Sys::Hostname モジュール(標準のperl配布キットの一部です)を使って
1135On systems that support getpwuid, the C<< $< >> variable, and the
1136C<Sys::Hostname> module (which is part of the standard perl distribution),
1137you can probably try using something like this:
1138
1139=end original
1140
1141getpwuid をサポートしているシステムであれば、C<< $< >> という変数と
1142C<Sys::Hostname> モジュール(標準の perl 配布キットの一部です)を使って
7571143以下のようなことが試せるでしょう。
7581144
759 use Sys::Hostname;
1145 use Sys::Hostname;
760 $address = sprintf('%s@%s', getpwuid($<), hostname);
1146 $address = sprintf('%s@%s', scalar getpwuid($<), hostname);
7611147
7621148=begin original
7631149
764Company policies on email address can mean that this generates addresses
1150Company policies on mail address can mean that this generates addresses
765that the company's email system will not accept, so you should ask for
1151that the company's mail system will not accept, so you should ask for
766users' email addresses when this matters. Furthermore, not all systems
1152users' mail addresses when this matters. Furthermore, not all systems
7671153on which Perl runs are so forthcoming with this information as is Unix.
7681154
7691155=end original
7701156
7711157会社のメールアドレスに関するポリシーが、これが生成するアドレスは
7721158その会社のメールシステムが受け付けないものである可能性があります。
7731159ですから、ユーザーに、そのユーザーのメールアドレスを尋ねるべきでしょう。
774それに加え、Perlが動作する全てのシステムで
1160それに加え、Perl が動作する全てのシステムで
775この情報が(UNIXと同じように)得られるわけではありません。
1161この情報が(UNIX と同じように)得られるわけではありません。
7761162
777CPANにある Mail::Utilモジュール (MailToolsパッケージの一部です)は
1163=begin original
1164
1165The C<Mail::Util> module from CPAN (part of the C<MailTools> package) provides a
1166C<mailaddress()> function that tries to guess the mail address of the user.
1167It makes a more intelligent guess than the code above, using information
1168given when the module was installed, but it could still be incorrect.
1169Again, the best way is often just to ask the user.
1170
1171=end original
1172
1173CPAN にある C<Mail::Util> モジュール (C<MailTools> パッケージの一部です)は
7781174メールアドレスがそのユーザーのものであるかどうかを確かめようとする
779mailaddress()という関数を提供しています。
1175C<mailaddress()> という関数を提供しています。
780これは上で例示したやり方よりも賢く、
1176これは上で例示したやり方よりも賢く、モジュールがインストールされたときの
781モジュールがインストールされたときの情報を使いますが、
1177情報を使いますが、それでも正しくない可能性があります。
782それでも正しくない可能性があります。
7831178くり返しますが、最善の方法はユーザーに尋ねること、というのがほとんどです。
7841179
7851180=head2 How do I send mail?
7861181
7871182(メールを送るには?)
7881183
789C<sendmail>プログラムを直接使います:
1184=begin original
7901185
791 open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq")
1186Use the C<sendmail> program directly:
792 or die "Can't fork for sendmail: $!\n";
793 print SENDMAIL <<"EOF";
794 From: メールの発信者 <me\@host>
795 To: 宛て先 <you\@otherhost>
796 Subject: 適切なサブジェクト
7971187
798 空行に続けてメッセージの本体を書きます。好きな行数書けます。
1188=end original
799 EOF
800 close(SENDMAIL) or warn "sendmail didn't close nicely";
8011189
1190C<sendmail> プログラムを直接使います:
1191
1192 open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq")
1193 or die "Can't fork for sendmail: $!\n";
1194 print SENDMAIL <<"EOF";
1195 From: User Originating Mail <me\@host>
1196 To: Final Destination <you\@otherhost>
1197 Subject: A relevant subject line
1198
1199 Body of the message goes here after the blank line
1200 in as many lines as you like.
1201 EOF
1202 close(SENDMAIL) or warn "sendmail didn't close nicely";
1203
8021204=begin original
8031205
804The B<-oi> option prevents sendmail from interpreting a line consisting
1206The B<-oi> option prevents C<sendmail> from interpreting a line consisting
8051207of a single dot as "end of message". The B<-t> option says to use the
8061208headers to decide who to send the message to, and B<-odq> says to put
8071209the message into the queue. This last option means your message won't
8081210be immediately delivered, so leave it out if you want immediate
8091211delivery.
8101212
8111213=end original
8121214
813B<-oi>オプションはsendmailがドットだけの行を“メッセージの終わり”と
1215B<-oi> オプションは C<sendmail> がドットだけの行を“メッセージの終わり”と
814みなさないようにするためのオプションです。B<-t>オプションは
1216みなさないようにするためのオプションです。
815メッセージを誰にるかを決めるかのためにヘッダーを使うことを指示し、
1217B<-t>オプションはメッセージを誰にるかを決めるかのために
816B<-odq>オプションメッセージをキューに入れることを指示します。
1218ヘッダーを使うことを指示し、B<-odq> オプションメッセージを
1219キューに入れることを指示します。
8171220最後のオプションの意味は、あなたのメッセージがすぐには配送されないことを
818意味します。ですから、すぐに配送させたいのであれば
1221意味します。
819このオプションを取り除いてください。
1222ですから、すぐに配送させたいのであればこのオプションを取り除いてください。
8201223
8211224=begin original
8221225
823Alternate, less convenient approaches include calling mail (sometimes
1226Alternate, less convenient approaches include calling C<mail> (sometimes
824called mailx) directly or simply opening up port 25 have having an
1227called C<mailx>) directly or simply opening up port 25 have having an
8251228intimate conversation between just you and the remote SMTP daemon,
826probably sendmail.
1229probably C<sendmail>.
8271230
8281231=end original
8291232
830あるいは、直接 mail (mailx と呼ばれることもあります)を
1233あるいは、直接 C<mail> (C<mailx> と呼ばれることもあります)を呼びだしたり、
831呼びだしたり、単純に 25 番ポートを使ってリモートの
1234単純に 25 番ポートを使ってリモートの SMTP デーモン(多分 C<sendmail> でしょう)
832SMTP デーモン(多分 sendmail でしょう) との間で
1235との間で詳細な通信を行うといったあまり便利でない方法もあります。
833詳細な通信を行うといったあまり便利でない方法もあります。
8341236
835あるいはCPANにあるモジュールMail::Mailerが使えるかもしれません:
1237=begin original
8361238
837 use Mail::Mailer;
1239Or you might be able use the CPAN module C<Mail::Mailer>:
8381240
839 $mailer = Mail::Mailer->new();
1241=end original
840 $mailer->open({ From => $from_address,
841 To => $to_address,
842 Subject => $subject,
843 })
844 or die "Can't open: $!\n";
845 print $mailer $body;
846 $mailer->close();
8471242
848Mail::Internet モジュール Mail::Mailer より UNIX 的ではない
1243あるいは CPAN にあるモジュール C<Mail::Mailer> が使えるかもしれません:
849Net::SMTP を使っていますが、信頼性も低いです。
1245 use Mail::Mailer;
1246
1247 $mailer = Mail::Mailer->new();
1248 $mailer->open({ From => $from_address,
1249 To => $to_address,
1250 Subject => $subject,
1251 })
1252 or die "Can't open: $!\n";
1253 print $mailer $body;
1254 $mailer->close();
1255
1256=begin original
1257
1258The C<Mail::Internet> module uses C<Net::SMTP> which is less Unix-centric than
1259C<Mail::Mailer>, but less reliable. Avoid raw SMTP commands. There
1260are many reasons to use a mail transport agent like C<sendmail>. These
1261include queuing, MX records, and security.
1262
1263=end original
1264
1265C<Mail::Internet> モジュールは C<Mail::Mailer> より UNIX 的ではない
1266C<Net::SMTP> を使っていますが、信頼性も低いです。
8501267生の SMTP コマンドを無視します。
851sendmail のような mail transport agent を使う理由はたくさんあります。
1268C<sendmail> のような mail transport agent を使う理由はたくさんあります。
8521269その中にはキューイングも含まれますし、MX レコードやセキュリティと
8531270いったものが含まれます。
8541271
8551272=head2 How do I use MIME to make an attachment to a mail message?
8561273
8571274(メールメッセージに添付するためにどうやって MIME を使えばいいですか?)
8581275
8591276=begin original
8601277
861This answer is extracted directly from the MIME::Lite documentation.
1278This answer is extracted directly from the C<MIME::Lite> documentation.
8621279Create a multipart message (i.e., one with attachments).
8631280
8641281=end original
8651282
866この回答は MIME::Lite のドキュメントから直接持ってきたものです。
1283この回答は C<MIME::Lite> のドキュメントから直接持ってきたものです。
8671284マルチパートメッセージ(つまり 添付つきのメッセージ) を作ります。
8681285
869 use MIME::Lite;
1286 use MIME::Lite;
8701287
871 ### Create a new multipart message:
1288 ### Create a new multipart message:
872 $msg = MIME::Lite->new(
1289 $msg = MIME::Lite->new(
873 From =>'me@myhost.com',
1290 From =>'me@myhost.com',
874 To =>'you@yourhost.com',
1291 To =>'you@yourhost.com',
875 Cc =>'some@other.com, some@more.com',
1292 Cc =>'some@other.com, some@more.com',
876 Subject =>'A message with 2 parts...',
1293 Subject =>'A message with 2 parts...',
877 Type =>'multipart/mixed'
1294 Type =>'multipart/mixed'
878 );
1295 );
8791296
880 ### Add parts (each "attach" has same arguments as "new"):
1297 ### Add parts (each "attach" has same arguments as "new"):
881 $msg->attach(Type =>'TEXT',
1298 $msg->attach(Type =>'TEXT',
882 Data =>"Here's the GIF file you wanted"
1299 Data =>"Here's the GIF file you wanted"
883 );
1300 );
884 $msg->attach(Type =>'image/gif',
1301 $msg->attach(Type =>'image/gif',
885 Path =>'aaa000123.gif',
1302 Path =>'aaa000123.gif',
886 Filename =>'logo.gif'
1303 Filename =>'logo.gif'
887 );
1304 );
8881305
889 $text = $msg->as_string;
1306 $text = $msg->as_string;
8901307
8911308=begin original
8921309
893MIME::Lite also includes a method for sending these things.
1310C<MIME::Lite> also includes a method for sending these things.
8941311
8951312=end original
8961313
897MIME::Lite はまたこれらのものを送るためのメソッドを含みます。
1314C<MIME::Lite> はまたこれらのものを送るためのメソッドを含みます。
8981315
899 $msg->send;
1316 $msg->send;
9001317
9011318=begin original
9021319
903This defaults to using L<sendmail(1)> but can be customized to use
1320This defaults to using L<sendmail> but can be customized to use
9041321SMTP via L<Net::SMTP>.
9051322
9061323=end original
9071324
908これはデフォルトでは L<sendmail(1)> を使いますが、
1325これはデフォルトでは L<sendmail> を使いますが、
9091326L<Net::SMTP> 経由で SMTP を使うようにカスタマイズできます。
9101327
9111328=head2 How do I read mail?
9121329
9131330(メールを読み出すには?)
9141331
915CPAN にある Mail::Folder モジュール(MailFolder パッケージの一部です)や
1332=begin original
916Mail::Internet モジュール(これも MailTools パッケージの一部です)が
1334While you could use the C<Mail::Folder> module from CPAN (part of the
1335C<MailFolder> package) or the C<Mail::Internet> module from CPAN (part
1336of the C<MailTools> package), often a module is overkill. Here's a
1337mail sorter.
1338
1339=end original
1340
1341CPAN にある C<Mail::Folder> モジュール(C<MailFolder> パッケージの一部です)や
1342C<Mail::Internet> モジュール(これも C<MailTools> パッケージの一部です)が
9171343使えますが、モジュールを使うのはやりすぎかもしれません。
9181344以下にメールをソートする方法を示します。
9191345
920 #!/usr/bin/perl
1346 #!/usr/bin/perl
921 # bysub1 - サブジェクトによる単純なソート
922 my(@msgs, @sub);
923 my $msgno = -1;
924 $/ = ''; # パラグラフ読み出し
925 while (<>) {
926 if (/^From/m) {
927 /^Subject:\s*(?:Re:\s*)*(.*)/mi;
928 $sub[++$msgno] = lc($1) || '';
929 }
930 $msgs[$msgno] .= $_;
931 }
932 for my $i (sort { $sub[$a] cmp $sub[$b] || $a <=> $b } (0 .. $#msgs)) {
933 print $msgs[$i];
934 }
9351347
936あるいはもっと簡潔に
1348 my(@msgs, @sub);
1349 my $msgno = -1;
1350 $/ = ''; # paragraph reads
1351 while (<>) {
1352 if (/^From /m) {
1353 /^Subject:\s*(?:Re:\s*)*(.*)/mi;
1354 $sub[++$msgno] = lc($1) || '';
1355 }
1356 $msgs[$msgno] .= $_;
1357 }
1358 for my $i (sort { $sub[$a] cmp $sub[$b] || $a <=> $b } (0 .. $#msgs)) {
1359 print $msgs[$i];
1360 }
9371361
938 #!/usr/bin/perl -n00
1362=begin original
939 # bysub2 - awki的な、サブジェクトによるソート
940 BEGIN { $msgno = -1 }
941 $sub[++$msgno] = (/^Subject:\s*(?:Re:\s*)*(.*)/mi)[0] if /^From/m;
942 $msg[$msgno] .= $_;
943 END { print @msg[ sort { $sub[$a] cmp $sub[$b] || $a <=> $b } (0 .. $#msg) ] }
9441363
945=head2 How do I find out my hostname/domainname/IP address?
1364Or more succinctly,
9461365
947(私の ホスト名/ドメイン名/IPアドレス を見つけるには?)
1366=end original
9481367
949多くのプログラムが C<`hostname`> プログラムを呼び出してます。
1368あるはもっと簡潔に:
950これは便利なときもありますが、高い移植性があるというわけではありません。
951これは、便利さと移植性とのトレードオフの一例です。
9521369
953Sys::Hostname モジュール(標準Perl配布キットの一部です)は
1370 #!/usr/bin/perl -n00
954gethostbyename()を使って(DNSが動作していることを仮定しています)
1371 # bysub2 - awkish sort-by-subject
955IPアドレスを取得できたあとで、ホスト名を返します。
1372 BEGIN { $msgno = -1 }
1373 $sub[++$msgno] = (/^Subject:\s*(?:Re:\s*)*(.*)/mi)[0] if /^From/m;
1374 $msg[$msgno] .= $_;
1375 END { print @msg[ sort { $sub[$a] cmp $sub[$b] || $a <=> $b } (0 .. $#msg) ] }
9561376
957 use Socket;
1377=head2 How do I find out my hostname, domainname, or IP address?
958 use Sys::Hostname;
1378X<hostname, domainname, IP address, host, domain, hostfqdn, inet_ntoa,
959 my $host = hostname();
1379gethostbyname, Socket, Net::Domain, Sys::Hostname>
960 my $addr = inet_ntoa(scalar gethostbyname($host || 'localhost'));
9611380
1381(私のホスト名/ドメイン名/IP アドレスを見つけるには?)
1382
9621383=begin original
9631384
964Probably the simplest way to learn your DNS domain name is to grok
1385(contributed by brian d foy)
965it out of /etc/resolv.conf, at least under Unix. Of course, this
966assumes several things about your resolv.conf configuration, including
967that it exists.
9681386
9691387=end original
9701388
971少なくとも UNIX では、おそらくあなたの使っている DNS
1389(brian d foy によって寄贈されました)
972ドメイン名を知る最も単純な方法は
973/etc/resolev.conf を解析することでしょう。
974もちろん、これはファイルが存在していることを含め、
975resolv.confの設定に関して幾つかの仮定を行っています。
9761390
9771391=begin original
9781392
979(We still need a good DNS domain name-learning method for non-Unix
1393The C<Net::Domain> module, which is part of the standard distribution starting
980systems.)
1394in perl5.7.3, can get you the fully qualified domain name (FQDN), the host
1395name, or the domain name.
9811396
9821397=end original
9831398
984(私たちは未だに非 UNIX システムのための良DNS ドメイン名所得手法
1399perl5.7.3 から標準配布されて C<Net::Domain> モジュール使うと、
985必要としています)
1400完全修飾ドメイン名 (FQDN)、ホスト名、ドメイン名が得られます
9861401
1402 use Net::Domain qw(hostname hostfqdn hostdomain);
1403
1404 my $host = hostfqdn();
1405
1406=begin original
1407
1408The C<Sys::Hostname> module, included in the standard distribution since
1409perl5.6, can also get the hostname.
1410
1411=end original
1412
1413perl5.6 から標準配布されている C<Sys::Hostname> モジュールでも
1414ホスト名を得られます。
1415
1416 use Sys::Hostname;
1417
1418 $host = hostname();
1419
1420=begin original
1421
1422To get the IP address, you can use the C<gethostbyname> built-in function
1423to turn the name into a number. To turn that number into the dotted octet
1424form (a.b.c.d) that most people expect, use the C<inet_ntoa> function
1425from the C<Socket> module, which also comes with perl.
1426
1427=end original
1428
1429IP アドレスを得るには、名前から数値に変換するために C<gethostbyname>
1430組み込み関数が使えます。
1431数値を、ほとんどの人が想定しているピリオド付きの形 (a.b.c.d) に変換するには、
1432標準配布されている C<Socket> モジュールの C<inet_ntoa> 関数を使います。
1433
1434 use Socket;
1435
1436 my $address = inet_ntoa(
1437 scalar gethostbyname( $host || 'localhost' )
1438 );
1439
9871440=head2 How do I fetch a news article or the active newsgroups?
9881441
989(ニュースのアーティクルやアクティブなニュースグループを取得するには?)
1442(ニュースの記事やアクティブなニュースグループを取得するには?)
9901443
9911444=begin original
9921445
993Use the Net::NNTP or News::NNTPClient modules, both available from CPAN.
1446Use the C<Net::NNTP> or C<News::NNTPClient> modules, both available from CPAN.
9941447This can make tasks like fetching the newsgroup list as simple as
9951448
9961449=end original
9971450
998Net::NNTP モジュールか News::NNTPClient モジュールのいずれかを使います。
1451C<Net::NNTP> モジュールか C<News::NNTPClient> モジュールのいずれかを使います。
999これらは両方ともCPANから入手可能です。これらは以下のように
1452これらは両方とも CPAN から入手可能です。
1000簡単にニュースグループのリストを取得するような作業ができます。
1453これらは以下のように簡単にニュースグループのリストを取得するような
1454作業ができます。
10011455
1002 perl -MNews::NNTPClient
1456 perl -MNews::NNTPClient
1003 -e 'print News::NNTPClient->new->list("newsgroups")'
1457 -e 'print News::NNTPClient->new->list("newsgroups")'
10041458
10051459=head2 How do I fetch/put an FTP file?
10061460
1007(FTPファイルをダウンロード/アップロードするには?)
1461(FTP ファイルをダウンロード/アップロードするには?)
10081462
10091463=begin original
10101464
1011LWP::Simple (available from CPAN) can fetch but not put. Net::FTP (also
1465(contributed by brian d foy)
1012available from CPAN) is more complex but can put as well as fetch.
10131466
10141467=end original
10151468
1016LWP::Simple (CPANで入手可能)はダウンロードができすがアップロードはできません。
1469(brian d foy によって寄贈されした)
1017Net::FTP(これもCPANで入手可能)はこれよりも複雑ですが、
1018ダウンロードとアップロードの両方ができます。
10191470
1471=begin original
1472
1473The C<LWP> family of modules (available on CPAN as the libwww-perl distribution)
1474can work with FTP just like it can with many other protocols. C<LWP::Simple>
1475makes it quite easy to fetch a file:
1476
1477=end original
1478
1479C<LWP> モジュールファミリー (CPAN で libwww-perl 配布として利用可能です) は
1480FTP をその他のプロトコルと同様に扱えます。
1481C<LWP::Simple> はファイルをダウンロードするのをとても簡単にします:
1482
1483 use LWP::Simple;
1484
1485 my $data = get( 'ftp://some.ftp.site/some/file.txt' );
1486
1487=begin original
1488
1489If you want more direct or low-level control of the FTP process, you can use
1490the C<Net::FTP> module (in the Standard Library since Perl 5.8). It's
1491documentation has examples showing you just how to do that.
1492
1493=end original
1494
1495FTP 処理をより直接的に、あるいは低レベルの制御がしたいなら、(Perl 5.8 以降
1496標準ライブラリに入っている) C<Net::FTP> モジュールが使えます。
1497その文書にはこれを行う方法を示すための例が載っています。
1498
10201499=head2 How can I do RPC in Perl?
10211500
1022(Perl で RPC を行うにはどうすればよいのですか?)
1501(Perl で RPC を行うには?)
10231502
10241503=begin original
10251504
1026A DCE::RPC module is being developed (but is not yet available) and
1505(contributed by brian d foy)
1027will be released as part of the DCE-Perl package (available from
1028CPAN). The rpcgen suite, available from CPAN/authors/id/JAKE/, is
1029an RPC stub generator and includes an RPC::ONC module.
10301506
10311507=end original
10321508
1033DCE::RPCが開発中です(ただし、まだ入手可能はなってせん)
1509(brian d foy って寄贈されした)
1034これは DCE-Perl パッケージ(CPAN から入手可能です)の一部として
1035リリースされるでしょう。
1036rpcgenスイートがCAPN/authors/id/JAKE/から入手可能です。
1037これはRPCスタブのジェネレーターであり、RPC::ONCモジュールを含んでいます。
10381510
1511=begin original
1512
1513Use one of the RPC modules you can find on CPAN (
1514http://search.cpan.org/search?query=RPC&mode=all ).
1515
1516=end original
1517
1518CPAN ( http://search.cpan.org/search?query=RPC&mode=all ) で見付かる
1519RFC モジュールの一つを使いましょう。
1520
10391521=head1 AUTHOR AND COPYRIGHT
10401522
1041Copyright (c) 1997-1999 Tom Christiansen and Nathan Torkington.
1523Copyright (c) 1997-2010 Tom Christiansen, Nathan Torkington, and
1042All rights reserved.
1524other authors as noted. All rights reserved.
10431525
1044When included as part of the Standard Version of Perl, or as part of
1526This documentation is free; you can redistribute it and/or modify it
1045its complete documentation whether printed or otherwise, this work
1527under the same terms as Perl itself.
1046may be distributed only under the terms of Perl's Artistic License.
1047Any distribution of this file or derivatives thereof I<outside>
1048of that package require that special arrangements be made with
1049copyright holder.
10501528
10511529Irrespective of its distribution, all code examples in this file
10521530are hereby placed into the public domain. You are permitted and
10531531encouraged to use this code in your own programs for fun
10541532or for profit as you see fit. A simple comment in the code giving
10551533credit would be courteous but is not required.
1534
1535=begin meta
1536
1537Translate: 吉村 寿人 <JAE00534@niftyserve.or.jp>
1538Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.6.1-)
1539Status: completed
1540
1541=end meta