/
perlport.pod
executable file
·5548 lines (3755 loc) · 158 KB
/
perlport.pod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=encoding euc-jp
=head1 NAME
=begin original
perlport - Writing portable Perl
=end original
perlport - 移植性のある Perl を書く
=head1 DESCRIPTION
=begin original
Perl runs on numerous operating systems. While most of them share
much in common, they also have their own unique features.
=end original
Perl は多くのオペレーティングシステム上で動作します。
これらのほとんどは一般的にかなりの部分を共有していますが、それぞれ固有の
機能も持っています。
=begin original
This document is meant to help you to find out what constitutes portable
Perl code. That way once you make a decision to write portably,
you know where the lines are drawn, and you can stay within them.
=end original
この文書は移植性のある Perl コードの構成要素を発見する助けになるための
ものです。
移植性のある形で書こうと決心したら、どこに線が引かれているかを知ることで、
その内側に留まることができます。
=begin original
There is a tradeoff between taking full advantage of one particular
type of computer and taking advantage of a full range of them.
Naturally, as you broaden your range and become more diverse, the
common factors drop, and you are left with an increasingly smaller
area of common ground in which you can operate to accomplish a
particular task. Thus, when you begin attacking a problem, it is
important to consider under which part of the tradeoff curve you
want to operate. Specifically, you must decide whether it is
important that the task that you are coding have the full generality
of being portable, or whether to just get the job done right now.
This is the hardest choice to be made. The rest is easy, because
Perl provides many choices, whichever way you want to approach your
problem.
=end original
ある特定の種類のコンピュータの利点を使うことと、あらゆる範囲の
コンピュータの利点を使うことの間にはトレードオフがあります。
当然ながら、より範囲を広げてより多様性のある形にすると、共通の要素が
減っていき、特定のタスクを達成するために操作できる共通の地盤が徐々に
小さくなっていきます。
従って、問題に取りかかるときに、トレードオフのカーブのどの部分を
使うかを考えることは重要です。
特に、コーディングしようとするタスクが移植性に関して完全な一般性が
重要かどうか、またすぐにジョブを終わらせるかどうかを
決定しなければなりません。
残りのことは簡単です。
なぜならあなたが問題にどのようにアプローチしたいとしても Perl は多くの
選択肢を提供するからです。
=begin original
Looking at it another way, writing portable code is usually about
willfully limiting your available choices. Naturally, it takes
discipline and sacrifice to do that. The product of portability
and convenience may be a constant. You have been warned.
=end original
これを他の方法で見てみると、移植性のあるコードを書くことは通常あなたが
取り得る選択肢を故意に制限します。
当然ながら、これは規律と犠牲が伴います。
おそらく移植性と利便性の積は一定です。
あなたは警告されましたよ。
=begin original
Be aware of two important points:
=end original
二つの重要な点に注意してください:
=over 4
=item Not all Perl programs have to be portable
(全ての Perl プログラムが移植性がある必要はありません)
=begin original
There is no reason you should not use Perl as a language to glue Unix
tools together, or to prototype a Macintosh application, or to manage the
Windows registry. If it makes no sense to aim for portability for one
reason or another in a given program, then don't bother.
=end original
Unix ツールを互いにくっつけたり、Macintosh アプリケーションのプロトタイプを
作ったり、Windows レジストリを操作するための言語として Perl を
使うべきではないという理由はありません。
プログラムにとって何らかの理由で移植性を目標とすることが無意味なら、
気にしないでください。
=item Nearly all of Perl already I<is> portable
(ほとんど全ての Perl は既に移植性が I<あります>)
=begin original
Don't be fooled into thinking that it is hard to create portable Perl
code. It isn't. Perl tries its level-best to bridge the gaps between
what's available on different platforms, and all the means available to
use those features. Thus almost all Perl code runs on any machine
without modification. But there are some significant issues in
writing portable code, and this document is entirely about those issues.
=end original
移植性のある Perl コードを作るのが難しいという考えに騙されないでください。
そうではありません。
Perl は、異なったプラットフォームで何が利用可能かとこの機能を使うために
利用可能なもの全てとの間のずれを出来るだけ橋渡ししようとします。
従って、ほとんど全ての Perl コードは修正なしにどのマシンでも動作します。
しかし移植性のあるコードを書くにはいくつかの重要な問題があり、この文書は
全体的にそのような問題を扱っています。
=back
=begin original
Here's the general rule: When you approach a task commonly done
using a whole range of platforms, think about writing portable
code. That way, you don't sacrifice much by way of the implementation
choices you can avail yourself of, and at the same time you can give
your users lots of platform choices. On the other hand, when you have to
take advantage of some unique feature of a particular platform, as is
often the case with systems programming (whether for Unix, Windows,
VMS, etc.), consider writing platform-specific code.
=end original
一般的なルールを挙げます: プラットフォーム全体で使われて一般的に
処理されるようなタスクに迫るとき、移植性のあるコードを書くことを
考えてください。
その方向で、自分自身の実装の選択肢を多く犠牲にすることはなく、
同時にユーザーに多くのプラットフォームの選択肢を与えることができます。
一方、特定のプラットフォームで固有の機能の利点を使う必要がある場合、
例えば (Unix, Windows, VMS など専用の) システムプログラムのような
場合、プラットフォーム固有のコードを書くことを考えてください。
=begin original
When the code will run on only two or three operating systems, you
may need to consider only the differences of those particular systems.
The important thing is to decide where the code will run and to be
deliberate in your decision.
=end original
コードが二つか三つのオペレーティングシステムでだけ動作するときは、
それらの特定のシステムでの違いのみを考慮する必要があります。
重要なことは、どこでコードを実行するかと、決定を熟考することです。
=begin original
The material below is separated into three main sections: main issues of
portability (L<"ISSUES">), platform-specific issues (L<"PLATFORMS">), and
built-in perl functions that behave differently on various ports
(L<"FUNCTION IMPLEMENTATIONS">).
=end original
以下の材料は三つの主な章に分割されています: 主な移植性の問題
(L<"ISSUES">)、プラットフォーム固有の問題 (L<"PLATFORMS">)、
OS によって異なった振る舞いをする perl 組み込み関数
(L<"FUNCTION IMPLEMENTATIONS">) です。
=begin original
This information should not be considered complete; it includes possibly
transient information about idiosyncrasies of some of the ports, almost
all of which are in a state of constant evolution. Thus, this material
should be considered a perpetual work in progress
(C<< <IMG SRC="yellow_sign.gif" ALT="Under Construction"> >>).
=end original
この情報は完全であると考えるべきではありません; これは一部の OS に対する
特異性に関するおそらく一時的な情報を含んでいて、それらのほとんどは常に
進化しているものです。
従って、この材料は永遠に作業中であると考えるべきです
(C<< <IMG SRC="yellow_sign.gif" ALT="Under Construction"> >>)。
=head1 ISSUES
(問題)
=head2 Newlines
(改行)
=begin original
In most operating systems, lines in files are terminated by newlines.
Just what is used as a newline may vary from OS to OS. Unix
traditionally uses C<\012>, one type of DOSish I/O uses C<\015\012>,
and S<Mac OS> uses C<\015>.
=end original
ほとんどのオペレーティングシステムで、ファイルの行は改行で終端されます。
単に改行として何を使うかが OS によって異なります。
Unix は伝統的に C<\012> を使い、DOS 風の I/O は C<\015\012> を使い、
S<Mac OS> は C<\015> を使います。
=begin original
Perl uses C<\n> to represent the "logical" newline, where what is
logical may depend on the platform in use. In MacPerl, C<\n> always
means C<\015>. In DOSish perls, C<\n> usually means C<\012>, but when
accessing a file in "text" mode, perl uses the C<:crlf> layer that
translates it to (or from) C<\015\012>, depending on whether you're
reading or writing. Unix does the same thing on ttys in canonical
mode. C<\015\012> is commonly referred to as CRLF.
=end original
Perl は「論理的な」改行を表現するのに C<\n> を使います; 何が論理的かは
使っているプラットフォームに依存しています。
MacPerl では C<\n> は常に C<\015> を意味します。
DOS 風の perl では、C<\n> は普通 C<\012> を意味しますが、ファイルを
「テキスト」モードでアクセスすると、perl は C<\015\012> との間で
変換する C<:crlf> を使います。
Unix はカノニカルモードの tty で同じことをします。
C<\015\012> は一般的には CRLF として参照されます。
=begin original
To trim trailing newlines from text lines use chomp(). With default
settings that function looks for a trailing C<\n> character and thus
trims in a portable way.
=end original
テキスト行から末尾の改行を切り落とすには、chomp() を使います。
この関数のデフォルト設定は末尾の C<\n> 文字を探すので、移植性のある形で
切り落とします。
=begin original
When dealing with binary files (or text files in binary mode) be sure
to explicitly set $/ to the appropriate value for your file format
before using chomp().
=end original
バイナリファイル (またはバイナリモードでのテキストファイル) を扱うときには、
chomp() を使う前にファイル形式に適切な値を $/ に明示的に設定してください。
=begin original
Because of the "text" mode translation, DOSish perls have limitations
in using C<seek> and C<tell> on a file accessed in "text" mode.
Stick to C<seek>-ing to locations you got from C<tell> (and no
others), and you are usually free to use C<seek> and C<tell> even
in "text" mode. Using C<seek> or C<tell> or other file operations
may be non-portable. If you use C<binmode> on a file, however, you
can usually C<seek> and C<tell> with arbitrary values in safety.
=end original
「テキスト」モード変換によって、DOS 的な perl は「テキスト」モードで
アクセスするファイルに対する C<seek> と C<tell> の使用に制限があります。
C<tell> で得た位置へ C<seek> する(そして他の方法を使わない)ことに
専念することで、「テキスト」モードでも自由に C<seek> と C<tell> を
使えます。
C<seek> や C<tell> やその他のファイル操作は互換性がないかもしれません。
しかし、ファイルに対して C<binmode> を使うと、普通は任意の値を C<seek> と
C<tell> に使っても安全です。
=begin original
A common misconception in socket programming is that C<\n> eq C<\012>
everywhere. When using protocols such as common Internet protocols,
C<\012> and C<\015> are called for specifically, and the values of
the logical C<\n> and C<\r> (carriage return) are not reliable.
=end original
ソケットプログラミングでのよくある誤解は、C<\n> はどこでも C<\012> に
等しいということです。
一般的なインターネットプロトコルのようなプロトコルを使うとき、
C<\012> と C<\015> は明確に記述されていて、論理的な
C<\n> と C<\r> (復帰) の値は信頼できません。
print SOCKET "Hi there, client!\r\n"; # WRONG
print SOCKET "Hi there, client!\015\012"; # RIGHT
=begin original
However, using C<\015\012> (or C<\cM\cJ>, or C<\x0D\x0A>) can be tedious
and unsightly, as well as confusing to those maintaining the code. As
such, the Socket module supplies the Right Thing for those who want it.
=end original
しかし、C<\015\012> (または C<\cM\cJ> または C<\x0D\x0A>) を使うのは
退屈で見苦しいかもしれませんし、コードの保守に混乱するかもしれません。
そのようなものとして、Socket モジュールは求められていることに対する
正しいものを供給します。
use Socket qw(:DEFAULT :crlf);
print SOCKET "Hi there, client!$CRLF" # RIGHT
=begin original
When reading from a socket, remember that the default input record
separator C<$/> is C<\n>, but robust socket code will recognize as
either C<\012> or C<\015\012> as end of line:
=end original
ソケットから読み込むとき、デフォルト入力レコード区切り C<$/> は
C<\n> だけれども、堅牢なソケットコードは C<\012> と C<\015\012> の
どちらも行の末尾として認識することを忘れないでください:
while (<SOCKET>) {
# ...
}
=begin original
Because both CRLF and LF end in LF, the input record separator can
be set to LF and any CR stripped later. Better to write:
=end original
CRLF と LF は両方とも LF で終わっているので、入力レコード区切りを
LF に設定して、後から CR を削除できます。
よりよく書くと:
use Socket qw(:DEFAULT :crlf);
local($/) = LF; # not needed if $/ is already \012
while (<SOCKET>) {
s/$CR?$LF/\n/; # not sure if socket uses LF or CRLF, OK
# s/\015?\012/\n/; # same thing
}
=begin original
This example is preferred over the previous one--even for Unix
platforms--because now any C<\015>'s (C<\cM>'s) are stripped out
(and there was much rejoicing).
=end original
この例は -- 例え Unix プラットフォームでも -- 以前のものよりよいものです;
なぜなら全ての C<\015> (C<\cM>) が削除される(そしてこれはとても喜ばしい)
からです。
=begin original
Similarly, functions that return text data--such as a function that
fetches a web page--should sometimes translate newlines before
returning the data, if they've not yet been translated to the local
newline representation. A single line of code will often suffice:
=end original
同様に、-- web ページを取得する関数のような -- テキストデータを返す関数は、
まだローカルな改行表現に変換されていないなら、データを返す前に改行を
変換するべき場合もあります。
しばしば 1 行のコードで十分です:
$data =~ s/\015?\012/\n/g;
return $data;
=begin original
Some of this may be confusing. Here's a handy reference to the ASCII CR
and LF characters. You can print it out and stick it in your wallet.
=end original
これらには混乱があるかもしれません。
以下は ASCII CR と LF 文字の便利なリファレンスです。
これを印刷して財布に貼ることができます。
LF eq \012 eq \x0A eq \cJ eq chr(10) eq ASCII 10
CR eq \015 eq \x0D eq \cM eq chr(13) eq ASCII 13
| Unix | DOS | Mac |
---------------------------
\n | LF | LF | CR |
\r | CR | CR | LF |
\n * | LF | CRLF | CR |
\r * | CR | CR | LF |
---------------------------
* text-mode STDIO
=begin original
The Unix column assumes that you are not accessing a serial line
(like a tty) in canonical mode. If you are, then CR on input becomes
"\n", and "\n" on output becomes CRLF.
=end original
Unix の列は、カノニカルモードで(tty のような)シリアルインターフェースに
アクセスしているのではないことを仮定しています。
もしそうなら、入力の CR は "\n" になり、出力の "\n" は CRLF になります。
=begin original
These are just the most common definitions of C<\n> and C<\r> in Perl.
There may well be others. For example, on an EBCDIC implementation
such as z/OS (OS/390) or OS/400 (using the ILE, the PASE is ASCII-based)
the above material is similar to "Unix" but the code numbers change:
=end original
これらは単に Perl でのもっとも一般的な C<\n> と C<\r> の定義です。
他のものもあり得ます。
例えば、z/OS (OS/390) や OS/400 (ILE を使っている場合; PASE は ASCII
ベース) のような EBCDIC 実装では、上述の資料は "Unix" と同様ですが、
コード番号が変更されます:
LF eq \025 eq \x15 eq \cU eq chr(21) eq CP-1047 21
LF eq \045 eq \x25 eq chr(37) eq CP-0037 37
CR eq \015 eq \x0D eq \cM eq chr(13) eq CP-1047 13
CR eq \015 eq \x0D eq \cM eq chr(13) eq CP-0037 13
| z/OS | OS/400 |
----------------------
\n | LF | LF |
\r | CR | CR |
\n * | LF | LF |
\r * | CR | CR |
----------------------
* text-mode STDIO
=head2 Numbers endianness and Width
(数値のエンディアンと幅)
=begin original
Different CPUs store integers and floating point numbers in different
orders (called I<endianness>) and widths (32-bit and 64-bit being the
most common today). This affects your programs when they attempt to transfer
numbers in binary format from one CPU architecture to another,
usually either "live" via network connection, or by storing the
numbers to secondary storage such as a disk file or tape.
=end original
CPU が異なると、整数と浮動小数点数の順序 (I<エンディアン> (endianness) と
呼ばれます) と幅 (最近ではほとんど 32 ビットと 64 ビットです) が異なります。
これは、ある CPU アーキテクチャから他のものへ数値をバイナリ形式で、
普通はネットワーク接続経由で「ライブ」で、またはディスクファイルや
テープのような二次ストレージに保管することで移そうとしたときに
影響します。
=begin original
Conflicting storage orders make utter mess out of the numbers. If a
little-endian host (Intel, VAX) stores 0x12345678 (305419896 in
decimal), a big-endian host (Motorola, Sparc, PA) reads it as
0x78563412 (2018915346 in decimal). Alpha and MIPS can be either:
Digital/Compaq used/uses them in little-endian mode; SGI/Cray uses
them in big-endian mode. To avoid this problem in network (socket)
connections use the C<pack> and C<unpack> formats C<n> and C<N>, the
"network" orders. These are guaranteed to be portable.
=end original
保管の順序が章とすると値が完全におかしくなります。
リトルエンディアンのホスト (Intel, VAX) が 0x12345678 (10 進数では
305419896) を保管すると、ビッグエンディアンのホスト (Motorola, Sparc, PA) は
これを 0x78563412 (10 進数では 2018915346) として読み込みます。
Alpha と MIPS はどちらもあり得ます: Digital/Compaq はこれを
リトルエンティアンモードで使います; SGI/Cray はこれを
ビッグエンディアンモードで使います。
ネットワーク(ソケット)接続でこの問題を避けるには、C<pack> と C<unpack> の
「ネットワーク」順序フォーマットである C<n> および C<N> を使ってください。
これらは移植性があることを保証します。
=begin original
As of perl 5.9.2, you can also use the C<E<gt>> and C<E<lt>> modifiers
to force big- or little-endian byte-order. This is useful if you want
to store signed integers or 64-bit integers, for example.
=end original
perl 5.9.2 から、ビッグエンディアンとリトルエンディアンにバイト順を
強制するための C<E<gt>> と C<E<lt>> の修飾子も使えます。
これは例えば、符号付き整数や 64 ビット整数を保管したいときに有用です。
=begin original
You can explore the endianness of your platform by unpacking a
data structure packed in native format such as:
=end original
次のように、ネイティブな形式で pack されたデータ構造を unpack することで
プラットフォームのエンディアンを調べることができます:
print unpack("h*", pack("s2", 1, 2)), "\n";
# '10002000' on e.g. Intel x86 or Alpha 21064 in little-endian mode
# '00100020' on e.g. Motorola 68040
=begin original
If you need to distinguish between endian architectures you could use
either of the variables set like so:
=end original
エンディアンアーキテクチャを区別する必要があるなら、以下のような変数の
どちらかを使えます:
$is_big_endian = unpack("h*", pack("s", 1)) =~ /01/;
$is_little_endian = unpack("h*", pack("s", 1)) =~ /^1/;
=begin original
Differing widths can cause truncation even between platforms of equal
endianness. The platform of shorter width loses the upper parts of the
number. There is no good solution for this problem except to avoid
transferring or storing raw binary numbers.
=end original
幅の違いは同じエンディアンのプラットフォームの間でも切り詰めを
引き起こすことがあります。
幅がより短い側のプラットフォームは数値の上位部分を失います。
生のバイナリ数値を転送したり保管したりしないようにする以外に、この問題への
よい解決法はありません。
=begin original
One can circumnavigate both these problems in two ways. Either
transfer and store numbers always in text format, instead of raw
binary, or else consider using modules like Data::Dumper (included in
the standard distribution as of Perl 5.005) and Storable (included as
of perl 5.8). Keeping all data as text significantly simplifies matters.
=end original
これらの問題は二つの方法で避けることが出来ます。
数値を常に生のバイナリではなくテキスト形式で転送して保管するか、
(Perl 5.005 から標準配布に含まれている) Data::Dumper や (perl 5.8 から
含まれている) Storable のようなモジュールを使うことを考慮します。
全てのデータをテキストで扱うことは問題をかなり単純化します。
=begin original
The v-strings are portable only up to v2147483647 (0x7FFFFFFF), that's
how far EBCDIC, or more precisely UTF-EBCDIC will go.
=end original
v-文字列は v2147483647 (0x7FFFFFFF) 以下でのみ移植性があります; これは
EBCDIC、より正確には UTF-EBCDIC よりも遙かに遠いです。
=head2 Files and Filesystems
(ファイルとファイルシステム)
=begin original
Most platforms these days structure files in a hierarchical fashion.
So, it is reasonably safe to assume that all platforms support the
notion of a "path" to uniquely identify a file on the system. How
that path is really written, though, differs considerably.
=end original
最近のほとんどのプラットフォームではファイルの構造は階層的です。
従って、全てのプラットフォームがシステム中のファイルをユニークに
識別するための「パス」記法に対応していると仮定することは合理的に安全です。
パスが実際にどのように書かれるかはかなり異なります。
=begin original
Although similar, file path specifications differ between Unix,
Windows, S<Mac OS>, OS/2, VMS, VOS, S<RISC OS>, and probably others.
Unix, for example, is one of the few OSes that has the elegant idea
of a single root directory.
=end original
似てはいるものの、ファイルパスの指定方法は Unix,
Windows, S<Mac OS>, OS/2, VMS, VOS, S<RISC OS> そしておそらくその他で
異なります。
例えば、Unix は一つのルートディレクトリというエレガントな考え方を持つ
数少ない OS の一つです。
=begin original
DOS, OS/2, VMS, VOS, and Windows can work similarly to Unix with C</>
as path separator, or in their own idiosyncratic ways (such as having
several root directories and various "unrooted" device files such NIL:
and LPT:).
=end original
DOS, OS/2, VMS, VOS, Windows は C</> をパス区切りとして、(複数の
ルートディレクトリや、NIL: や LPT: のような様々な「ルートでない」
デバイスファイルといった)独自の風変わりな方法で Unix と似たように
動作します。
=begin original
S<Mac OS> 9 and earlier used C<:> as a path separator instead of C</>.
=end original
S<Mac OS> 9 以前はパス区切りに C</> ではなく C<:> を使います。
=begin original
The filesystem may support neither hard links (C<link>) nor
symbolic links (C<symlink>, C<readlink>, C<lstat>).
=end original
ファイルシステムはハードリンク (C<link>) やシンボリックリンク (C<symlink>,
C<readlink>, C<lstat>) に対応していないかもしれません。
=begin original
The filesystem may support neither access timestamp nor change
timestamp (meaning that about the only portable timestamp is the
modification timestamp), or one second granularity of any timestamps
(e.g. the FAT filesystem limits the time granularity to two seconds).
=end original
ファイルシステムはアクセスタイムスタンプや変更タイムスタンプに
対応していないかもしれません (つまり移植性のあるタイムスタンプは
変更タイムスタンプだけです); またタイムスタンプは 1 秒単位では
ないかもしれません (例えば、FAT ファイルシステムは時刻の単位は
2 秒単位です)。
=begin original
The "inode change timestamp" (the C<-C> filetest) may really be the
"creation timestamp" (which it is not in Unix).
=end original
「inode 変更タイムスタンプ」 (C<-C> ファイルテスト) は (Unix 以外では)
実際には「作成タイムスタンプ」かもしれません。
=begin original
VOS perl can emulate Unix filenames with C</> as path separator. The
native pathname characters greater-than, less-than, number-sign, and
percent-sign are always accepted.
=end original
VOS perl は C</> をパス区切りとして Unix ファイル名をエミュレートできます。
ネイティブなパス名文字である大なり、小なり、シャープ、パーセントは常に
受け入れられます。
=begin original
S<RISC OS> perl can emulate Unix filenames with C</> as path
separator, or go native and use C<.> for path separator and C<:> to
signal filesystems and disk names.
=end original
S<RISC OS> perl は C</> をパス区切りとして Unix ファイル名をエミュレート
するか、ネイティブのままで C<.> をパス区切り、C<:> をファイルシステムと
ディスクの名前として使えます。
=begin original
Don't assume Unix filesystem access semantics: that read, write,
and execute are all the permissions there are, and even if they exist,
that their semantics (for example what do r, w, and x mean on
a directory) are the Unix ones. The various Unix/POSIX compatibility
layers usually try to make interfaces like chmod() work, but sometimes
there simply is no good mapping.
=end original
Unix のファイルシステムアクセスの意味を仮定しないで下さい: 読み込み、
書き込み、実行のどれもです; たとえあったとしても、その意味論
(例えばディレクトリに対して r, w, x が何をするか) は Unix のものです。
様々な Unix/POSIX 互換層は普通 chmod() のようなものが動作するための
インターフェースとなっていますが、ときどき単にいいマッピングが
ないこともあります。
=begin original
If all this is intimidating, have no (well, maybe only a little)
fear. There are modules that can help. The File::Spec modules
provide methods to do the Right Thing on whatever platform happens
to be running the program.
=end original
もしこれら全てが怯えさせるものであるなら、恐れないでください(あー、多分
ほんの少しだけ恐れてください)。
助けになるモジュールがあります。
File::Spec モジュールはプログラムがどのプラットフォームで動作していても
「正しいこと」をします。
use File::Spec::Functions;
chdir(updir()); # go up one directory
my $file = catfile(curdir(), 'temp', 'file.txt');
# on Unix and Win32, './temp/file.txt'
# on Mac OS Classic, ':temp:file.txt'
# on VMS, '[.temp]file.txt'
=begin original
File::Spec is available in the standard distribution as of version
5.004_05. File::Spec::Functions is only in File::Spec 0.7 and later,
and some versions of perl come with version 0.6. If File::Spec
is not updated to 0.7 or later, you must use the object-oriented
interface from File::Spec (or upgrade File::Spec).
=end original
File::Spec はバージョン 5.004_05 から標準配布で利用可能です。
File::Spec::Functions は File::Spec 0.7 以降のみにあり、一部のバージョンの
perl はバージョン 0.6 を含んでいます。
File::Spec が 0.7 以降に更新されていないなら、File::Spec の
オブジェクト指向インターフェースを使わなければなりません
(あるいは File::Spec を更新しなければなりません)。
=begin original
In general, production code should not have file paths hardcoded.
Making them user-supplied or read from a configuration file is
better, keeping in mind that file path syntax varies on different
machines.
=end original
一般的に、製品コードはファイルパスをハードコーディングするべきでは
ありません。
ユーザーが指定できるようにするか、設定ファイルから読み込む方がよいです;
ファイルパスの文法はマシンによって異なることを忘れないでください。
=begin original
This is especially noticeable in scripts like Makefiles and test suites,
which often assume C</> as a path separator for subdirectories.
=end original
これは、しばしば C</> がサブディレクトリのパス区切りと仮定されている
Makefile やテストスイートのようなスクリプトで特に注意が必要です。
=begin original
Also of use is File::Basename from the standard distribution, which
splits a pathname into pieces (base filename, full path to directory,
and file suffix).
=end original
もう一つの有用なものは標準配布に含まれている File::Basename で、これは
パス名をベースファイル名、ディレクトリのフルパス、ファイルの拡張子に
分解します。
=begin original
Even when on a single platform (if you can call Unix a single platform),
remember not to count on the existence or the contents of particular
system-specific files or directories, like F</etc/passwd>,
F</etc/sendmail.conf>, F</etc/resolv.conf>, or even F</tmp/>. For
example, F</etc/passwd> may exist but not contain the encrypted
passwords, because the system is using some form of enhanced security.
Or it may not contain all the accounts, because the system is using NIS.
If code does need to rely on such a file, include a description of the
file and its format in the code's documentation, then make it easy for
the user to override the default location of the file.
=end original
単一のプラットフォームでさえ(Unix を単一のプラットフォームと呼ぶなら)、
F</etc/passwd>, F</etc/sendmail.conf>, F</etc/resolv.conf> あるいは
F</tmp/> でさえ、特定のシステム固有のファイルやディレクトリの存在や
その内容を当てにできないことを忘れないでください。
例えば、F</etc/passwd> は存在するかもしれませんが、システムがある種の
強化されたセキュリティを使っているために、暗号化されたパスワードを
含んでいないかもしれません。
あるいは、NIS を使っているために、全てのアカウントを
含んでいないかもしれません。
コードがこのようなファイルに依存する必要がある場合、コードの文書に
ファイルの説明とその形式を含めて、ユーザーがファイルのデフォルトの位置を
簡単に上書きできるようにします。
=begin original
Don't assume a text file will end with a newline. They should,
but people forget.
=end original
テキストファイルが改行で終わっていると仮定しないでください。
そうあるべきですが、人は忘れます。
=begin original
Do not have two files or directories of the same name with different
case, like F<test.pl> and F<Test.pl>, as many platforms have
case-insensitive (or at least case-forgiving) filenames. Also, try
not to have non-word characters (except for C<.>) in the names, and
keep them to the 8.3 convention, for maximum portability, onerous a
burden though this may appear.
=end original
F<test.pl> と F<Test.pl> のような、大文字と小文字が違うだけの名前の二つの
ファイルやディレクトリを作らないでください; 多くのプラットフォームは
大文字小文字を無視する(あるいは少なくとも大文字小文字に寛容な)
ファイル名を持つからです。
また、最大限の互換性のため、起きるかも知れない厄介事のために、
(C<.> 以外の)非単語文字を使わないようにして、8.3 の規約を維持してください。
=begin original
Likewise, when using the AutoSplit module, try to keep your functions to
8.3 naming and case-insensitive conventions; or, at the least,
make it so the resulting files have a unique (case-insensitively)
first 8 characters.
=end original
同様に、AutoSplit モジュールを使う場合、関数の 8.3 の命名と大文字小文字を
無視する規約を維持するようにしてください; あるいは、少なくとも、
結果のファイルが最初の 8 文字が(大文字小文字を無視して)ユニークに
なるようにしてください。
=begin original
Whitespace in filenames is tolerated on most systems, but not all,
and even on systems where it might be tolerated, some utilities
might become confused by such whitespace.
=end original
ファイル名の空白はほとんどのシステムで許容されますが、全てではなく、
許容しているシステムでも、そのような空白によって混乱するユーティリティも
あります。
=begin original
Many systems (DOS, VMS ODS-2) cannot have more than one C<.> in their
filenames.
=end original
多くのシステム (DOS, VMS ODS-2) はファイル名に二つ以上の C<.> を
使えません。
=begin original
Don't assume C<< > >> won't be the first character of a filename.
Always use C<< < >> explicitly to open a file for reading, or even
better, use the three-arg version of open, unless you want the user to
be able to specify a pipe open.
=end original
C<< > >> がファイル名の最初の文字ではないと仮定しないでください。
ユーザーがパイプで開けるようにしたいのでない限り、読み込みのためにファイルを
開くときには常に明示的に C<< < >> を使うか、もっとよいのは、3 引数版の
open を使ってください。
open my $fh, '<', $existing_file) or die $!;
=begin original
If filenames might use strange characters, it is safest to open it
with C<sysopen> instead of C<open>. C<open> is magic and can
translate characters like C<< > >>, C<< < >>, and C<|>, which may
be the wrong thing to do. (Sometimes, though, it's the right thing.)
Three-arg open can also help protect against this translation in cases
where it is undesirable.
=end original
もしファイル名におかしな文字が使われているかも知れないなら、
C<open> ではなく C<sysopen> を使って開くのがもっとも安全です。
C<open> はマジカルで、C<< > >>, C<< < >>, C<|> のような文字を
変換することがあり、これは間違ったことかもしれません。
(しかし、時々は、これは正しいことです。)
3 引数形式の open も予想していない状況でこの変換から守る助けになります。
=begin original
Don't use C<:> as a part of a filename since many systems use that for
their own semantics (Mac OS Classic for separating pathname components,
many networking schemes and utilities for separating the nodename and
the pathname, and so on). For the same reasons, avoid C<@>, C<;> and
C<|>.
=end original
C<:> をファイル名の一部として使わないでください; 多くのシステムがこれを
同時の意味で使っているからです (Mac OS Classic はパス名要素を分割するために、
多くのネットワークスキームとユーティリティではノード名とパス名を
分割するために、など)。
同じ理由で、C<@>, C<;>, C<|> も避けてください。
=begin original
Don't assume that in pathnames you can collapse two leading slashes
C<//> into one: some networking and clustering filesystems have special
semantics for that. Let the operating system to sort it out.
=end original
パス名の先頭の二つのスラッシュ C<//> を一つに圧縮できると
仮定しないでください: ある種のネットワーキングとクラスタリングの
ファイルシステムはこれに対して特別な意味論を持ちます。
オペレーティングシステムに任せてください。
=begin original
The I<portable filename characters> as defined by ANSI C are
=end original
ANSI C で定義されている、I<移植性のあるファイル名の文字> は:
a b c d e f g h i j k l m n o p q r t u v w x y z
A B C D E F G H I J K L M N O P Q R T U V W X Y Z
0 1 2 3 4 5 6 7 8 9
. _ -
=begin original
and the "-" shouldn't be the first character. If you want to be
hypercorrect, stay case-insensitive and within the 8.3 naming
convention (all the files and directories have to be unique within one
directory if their names are lowercased and truncated to eight
characters before the C<.>, if any, and to three characters after the
C<.>, if any). (And do not use C<.>s in directory names.)
=end original
かつ "-" は最初の文字には使えません。
もし超完全にしたいなら、大文字小文字は無視して、8.3 命名規約に従います
(全てのファイルとディレクトリは、名前を小文字にして、(もしあれば) C<.> の
前の 8 文字と (もしあれば) C<.> の後の 3 文字に切り詰めたときに、
ディレクトリ内でユニークである必要があります)。
(そしてディレクトリ名に C<.> を使わないでください。)
=head2 System Interaction
(システムの相互作用)
=begin original
Not all platforms provide a command line. These are usually platforms
that rely primarily on a Graphical User Interface (GUI) for user
interaction. A program requiring a command line interface might
not work everywhere. This is probably for the user of the program
to deal with, so don't stay up late worrying about it.
=end original
全てのプラットフォームがコマンドラインを提供しているわけではありません。
これらは普通ユーザーとの相互作用にグラフィカルユーザーインターフェース
(GUI) に基本的に依存しています。
コマンドラインインターフェースを要求するプログラムはどこでも
動作するわけではありません。
これはおそらくプログラムを扱うユーザーの問題なので、心配して遅くまで
遅くまで起きていないでください。
=begin original
Some platforms can't delete or rename files held open by the system,
this limitation may also apply to changing filesystem metainformation
like file permissions or owners. Remember to C<close> files when you