1 |
|
2 |
=encoding euc-jp |
3 |
|
4 |
=head1 NAME |
5 |
|
6 |
=begin original |
7 |
|
8 |
perlembed - how to embed perl in your C program |
9 |
|
10 |
=end original |
11 |
|
12 |
perlembed - C プログラムへの Perl の埋め込み方 |
13 |
|
14 |
=head1 DESCRIPTION |
15 |
|
16 |
=head2 PREAMBLE |
17 |
|
18 |
=begin original |
19 |
|
20 |
Do you want to: |
21 |
|
22 |
=end original |
23 |
|
24 |
あなたのやりたいことは |
25 |
|
26 |
=over 5 |
27 |
|
28 |
=item B<Use C from Perl?> |
29 |
|
30 |
(B<Perl から C を使うことですか?>) |
31 |
|
32 |
=begin original |
33 |
|
34 |
Read L<perlxstut>, L<perlxs>, L<h2xs>, L<perlguts>, and L<perlapi>. |
35 |
|
36 |
=end original |
37 |
|
38 |
L<perlxstut>, L<perlxs>, L<h2xs>, L<perlguts>, L<perlapi> を |
39 |
読みましょう。 |
40 |
|
41 |
=item B<Use a Unix program from Perl?> |
42 |
|
43 |
(B<Perl から UNIX プログラムを使うことですか?>) |
44 |
|
45 |
=begin original |
46 |
|
47 |
Read about back-quotes and about C<system> and C<exec> in L<perlfunc>. |
48 |
|
49 |
=end original |
50 |
|
51 |
バッククォートに関することと、 |
52 |
L<perlfunc>にあるC<system>、C<exec>に関することを読みましょう。 |
53 |
|
54 |
=item B<Use Perl from Perl?> |
55 |
|
56 |
(B<Perl から Perl を使うことですか?>) |
57 |
|
58 |
=begin original |
59 |
|
60 |
Read about L<perlfunc/do> and L<perlfunc/eval> and L<perlfunc/require> |
61 |
and L<perlfunc/use>. |
62 |
|
63 |
=end original |
64 |
|
65 |
L<perlfunc/do>、 L<perlfunc/eval>、L<perlfunc/require>、 |
66 |
L<perlfunc/use>を読みましょう。 |
67 |
|
68 |
=item B<Use C from C?> |
69 |
|
70 |
(B<C から C を使うことですか?>) |
71 |
|
72 |
=begin original |
73 |
|
74 |
Rethink your design. |
75 |
|
76 |
=end original |
77 |
|
78 |
デザインを考え直しましょう。 |
79 |
|
80 |
=item B<Use Perl from C?> |
81 |
|
82 |
(B<C から Perl を使うことですか?>) |
83 |
|
84 |
=begin original |
85 |
|
86 |
Read on... |
87 |
|
88 |
=end original |
89 |
|
90 |
読み続けてください… |
91 |
|
92 |
=back |
93 |
|
94 |
=head2 ROADMAP |
95 |
|
96 |
(ロードマップ) |
97 |
|
98 |
=over 5 |
99 |
|
100 |
=item * |
101 |
|
102 |
=begin original |
103 |
|
104 |
Compiling your C program |
105 |
|
106 |
=end original |
107 |
|
108 |
C プログラムをコンパイルする |
109 |
|
110 |
=item * |
111 |
|
112 |
=begin original |
113 |
|
114 |
Adding a Perl interpreter to your C program |
115 |
|
116 |
=end original |
117 |
|
118 |
C プログラムに Perl インタプリタを追加する |
119 |
|
120 |
=item * |
121 |
|
122 |
=begin original |
123 |
|
124 |
Calling a Perl subroutine from your C program |
125 |
|
126 |
=end original |
127 |
|
128 |
C プログラムから Perl サブルーチンを呼び出す |
129 |
|
130 |
=item * |
131 |
|
132 |
=begin original |
133 |
|
134 |
Evaluating a Perl statement from your C program |
135 |
|
136 |
=end original |
137 |
|
138 |
C プログラムから Perl の文を評価する |
139 |
|
140 |
=item * |
141 |
|
142 |
=begin original |
143 |
|
144 |
Performing Perl pattern matches and substitutions from your C program |
145 |
|
146 |
=end original |
147 |
|
148 |
C プログラムから Perl のパターンマッチングや置換を実行する |
149 |
|
150 |
=item * |
151 |
|
152 |
=begin original |
153 |
|
154 |
Fiddling with the Perl stack from your C program |
155 |
|
156 |
=end original |
157 |
|
158 |
C プログラムから Perl のスタックを扱う |
159 |
|
160 |
=item * |
161 |
|
162 |
=begin original |
163 |
|
164 |
Maintaining a persistent interpreter |
165 |
|
166 |
=end original |
167 |
|
168 |
永続的(persistent)インタプリタを取り扱う |
169 |
|
170 |
=item * |
171 |
|
172 |
=begin original |
173 |
|
174 |
Maintaining multiple interpreter instances |
175 |
|
176 |
=end original |
177 |
|
178 |
複数のインタプリタのインスタンスを取り扱う |
179 |
|
180 |
=item * |
181 |
|
182 |
=begin original |
183 |
|
184 |
Using Perl modules, which themselves use C libraries, from your C program |
185 |
|
186 |
=end original |
187 |
|
188 |
C ライブラリを使っている Perl のモジュールを C プログラムから使う |
189 |
|
190 |
=item * |
191 |
|
192 |
=begin original |
193 |
|
194 |
Embedding Perl under Win32 |
195 |
|
196 |
=end original |
197 |
|
198 |
Win32 における組み込み Perl |
199 |
|
200 |
=back |
201 |
|
202 |
=head2 Compiling your C program |
203 |
|
204 |
(あなたのCプログラムをコンパイルする) |
205 |
|
206 |
=begin original |
207 |
|
208 |
If you have trouble compiling the scripts in this documentation, |
209 |
you're not alone. The cardinal rule: COMPILE THE PROGRAMS IN EXACTLY |
210 |
THE SAME WAY THAT YOUR PERL WAS COMPILED. (Sorry for yelling.) |
211 |
|
212 |
=end original |
213 |
|
214 |
このドキュメントにあるスクリプトのコンパイルがうまく行かなくても、 |
215 |
あなたは孤独ではありません。 |
216 |
基本的なルールはこうです: |
217 |
B<あなたが Perl をコンパイルしたときと全く同じやり方でプログラムを |
218 |
コンパイルしなさい>。 |
219 |
(叫んでしまってすみません。) |
220 |
|
221 |
=begin original |
222 |
|
223 |
Also, every C program that uses Perl must link in the I<perl library>. |
224 |
What's that, you ask? Perl is itself written in C; the perl library |
225 |
is the collection of compiled C programs that were used to create your |
226 |
perl executable (I</usr/bin/perl> or equivalent). (Corollary: you |
227 |
can't use Perl from your C program unless Perl has been compiled on |
228 |
your machine, or installed properly--that's why you shouldn't blithely |
229 |
copy Perl executables from machine to machine without also copying the |
230 |
I<lib> directory.) |
231 |
|
232 |
=end original |
233 |
|
234 |
また、Perlを使うすべての C プログラムは I<perl ライブラリ> にリンクして |
235 |
いなければなりません。 |
236 |
それって何、と聞きますか? |
237 |
Perl それ自身が C で書かれています。 |
238 |
Perl ライブラリはあなたの perl 実行ファイル(I</usr/bin/perl> か |
239 |
これに等価な)を作るために使われたコンパイル済みの C プログラムの |
240 |
集合体です(あなたの使うマシンで Perl がコンパイルされているか、正しく |
241 |
インストールされていなければ C プログラムから Perl を使うことはできません。 |
242 |
これが、Perl の実行ファイルをあるマシンからマシンへ I<lib> ディレクトリ |
243 |
抜きでコピーしてしまうような軽率なことをすべきでないという理由です)。 |
244 |
|
245 |
=begin original |
246 |
|
247 |
When you use Perl from C, your C program will--usually--allocate, |
248 |
"run", and deallocate a I<PerlInterpreter> object, which is defined by |
249 |
the perl library. |
250 |
|
251 |
=end original |
252 |
|
253 |
Perl を C から使うとき、その C プログラムは -- 普通は -- perl ライブラリで |
254 |
定義されている I<PerlInterpreter> オブジェクトの割り付け、「実行」、解放を |
255 |
行います。 |
256 |
|
257 |
=begin original |
258 |
|
259 |
If your copy of Perl is recent enough to contain this documentation |
260 |
(version 5.002 or later), then the perl library (and I<EXTERN.h> and |
261 |
I<perl.h>, which you'll also need) will reside in a directory |
262 |
that looks like this: |
263 |
|
264 |
=end original |
265 |
|
266 |
あなたの使っている Perl がこのドキュメントの内容にあっている(バージョン |
267 |
5.002 以降)であるのであれば、perl ライブラリ(とこれも必要な |
268 |
I<EXTERN.H> と I<perl.h>)が次のようなディレクトリに |
269 |
置かれていることでしょう。 |
270 |
|
271 |
/usr/local/lib/perl5/your_architecture_here/CORE |
272 |
|
273 |
=begin original |
274 |
|
275 |
or perhaps just |
276 |
|
277 |
=end original |
278 |
|
279 |
あるいは以下のような場所かもしれませんし |
280 |
|
281 |
/usr/local/lib/perl5/CORE |
282 |
|
283 |
=begin original |
284 |
|
285 |
or maybe something like |
286 |
|
287 |
=end original |
288 |
|
289 |
こうかもしれません |
290 |
|
291 |
/usr/opt/perl5/CORE |
292 |
|
293 |
=begin original |
294 |
|
295 |
Execute this statement for a hint about where to find CORE: |
296 |
|
297 |
=end original |
298 |
|
299 |
CORE の場所を見つけ出すために以下のような文を実行します。 |
300 |
|
301 |
perl -MConfig -e 'print $Config{archlib}' |
302 |
|
303 |
=begin original |
304 |
|
305 |
Here's how you'd compile the example in the next section, |
306 |
L</Adding a Perl interpreter to your C program>, on my Linux box: |
307 |
|
308 |
=end original |
309 |
|
310 |
次のセクション L</Adding a Perl interpreter to your C program> に |
311 |
ある例をコンパイルするこうします(私のLinux boxでの場合) |
312 |
|
313 |
% gcc -O2 -Dbool=char -DHAS_BOOL -I/usr/local/include |
314 |
-I/usr/local/lib/perl5/i586-linux/5.003/CORE |
315 |
-L/usr/local/lib/perl5/i586-linux/5.003/CORE |
316 |
-o interp interp.c -lperl -lm |
317 |
|
318 |
=begin original |
319 |
|
320 |
(That's all one line.) On my DEC Alpha running old 5.003_05, the |
321 |
incantation is a bit different: |
322 |
|
323 |
=end original |
324 |
|
325 |
(これは実際には一行です。) |
326 |
私の使う DEC Alpha 上で古い 5.003_05 を使う場合では、オマジナイはちょっと |
327 |
違います: |
328 |
|
329 |
% cc -O2 -Olimit 2900 -I/usr/local/include |
330 |
-I/usr/local/lib/perl5/alpha-dec_osf/5.00305/CORE |
331 |
-L/usr/local/lib/perl5/alpha-dec_osf/5.00305/CORE -L/usr/local/lib |
332 |
-D__LANGUAGE_C__ -D_NO_PROTO -o interp interp.c -lperl -lm |
333 |
|
334 |
=begin original |
335 |
|
336 |
How can you figure out what to add? Assuming your Perl is post-5.001, |
337 |
execute a C<perl -V> command and pay special attention to the "cc" and |
338 |
"ccflags" information. |
339 |
|
340 |
=end original |
341 |
|
342 |
追加するのものを見つけるにはどうすればよいでしょうか? |
343 |
あなたの使う Perl が 5.001 より後のものであれば、C<perl -V> を実行して、 |
344 |
"cc" と "ccflags" の情報に特に注意して見ましょう。 |
345 |
|
346 |
=begin original |
347 |
|
348 |
You'll have to choose the appropriate compiler (I<cc>, I<gcc>, et al.) for |
349 |
your machine: C<perl -MConfig -e 'print $Config{cc}'> will tell you what |
350 |
to use. |
351 |
|
352 |
=end original |
353 |
|
354 |
あなたは、あなたの使うマシンのために適切なコンパイラ(I<cc>, |
355 |
I<gcc> など)を選ぶ必要があります。 |
356 |
C<perl -MConfig -e 'print $Config{cc}'> はあなたに使うべき |
357 |
コンパイラを教えてくれるでしょう。 |
358 |
|
359 |
=begin original |
360 |
|
361 |
You'll also have to choose the appropriate library directory |
362 |
(I</usr/local/lib/...>) for your machine. If your compiler complains |
363 |
that certain functions are undefined, or that it can't locate |
364 |
I<-lperl>, then you need to change the path following the C<-L>. If it |
365 |
complains that it can't find I<EXTERN.h> and I<perl.h>, you need to |
366 |
change the path following the C<-I>. |
367 |
|
368 |
=end original |
369 |
|
370 |
同様に、適切なライブラリディレクトリ(I</usr/local/lib/...>) も |
371 |
選択せねばなりません。 |
372 |
もしあなたの使うコンパイラが幾つかの関数が |
373 |
未定義であるとか、I<-lperl> が見つからないという報告をしてきたら、 |
374 |
C<-L> の後に続くパスを変更する必要があります。 |
375 |
I<EXTERN.h> や I<perl.h> が見つからないというエラーであれば、 |
376 |
C<-I> の後にあるパスを変更する必要があります。 |
377 |
|
378 |
=begin original |
379 |
|
380 |
You may have to add extra libraries as well. Which ones? |
381 |
Perhaps those printed by |
382 |
|
383 |
=end original |
384 |
|
385 |
おそらく、さらにライブラリを追加指定する必要があるでしょう。 |
386 |
どれを? |
387 |
おそらく以下のようにすれば表示されます: |
388 |
|
389 |
perl -MConfig -e 'print $Config{libs}' |
390 |
|
391 |
=begin original |
392 |
|
393 |
Provided your perl binary was properly configured and installed the |
394 |
B<ExtUtils::Embed> module will determine all of this information for |
395 |
you: |
396 |
|
397 |
=end original |
398 |
|
399 |
あなたの使っている perl のバイナリが適切なコンフィギュレーションが |
400 |
行われているもので、かつ B<ExtUtils::Embed> モジュールが |
401 |
インストールされていれば、これによって必要な全ての情報が決定できます。 |
402 |
|
403 |
% cc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts` |
404 |
|
405 |
=begin original |
406 |
|
407 |
If the B<ExtUtils::Embed> module isn't part of your Perl distribution, |
408 |
you can retrieve it from |
409 |
L<http://www.perl.com/perl/CPAN/modules/by-module/ExtUtils/> |
410 |
(If this documentation came from your Perl distribution, then you're |
411 |
running 5.004 or better and you already have it.) |
412 |
|
413 |
=end original |
414 |
|
415 |
B<ExtUtils::Embed> モジュールは Perl 配布パッケージに含まれていなければ、 |
416 |
L<http://www.perl.com/perl/CPAN/modules/by-module/ExtUtils/> から |
417 |
入手できます(もしこのドキュメントがあなたの持っている |
418 |
Perl 配布パッケージにあったものならば、あなたは 5.004 以降のものを |
419 |
使っているはずで、すでにそのモジュールを持っているはずです。) |
420 |
|
421 |
=begin original |
422 |
|
423 |
The B<ExtUtils::Embed> kit on CPAN also contains all source code for |
424 |
the examples in this document, tests, additional examples and other |
425 |
information you may find useful. |
426 |
|
427 |
=end original |
428 |
|
429 |
CPAN にある B<ExtUtils::Embed> キットにもこのドキュメントにある例の |
430 |
すべてのソースコードがあり、テストと追加された例、その他の情報が |
431 |
あります。 |
432 |
|
433 |
=head2 Adding a Perl interpreter to your C program |
434 |
|
435 |
(あなたの C プログラムに Perl インタプリタを追加する) |
436 |
|
437 |
=begin original |
438 |
|
439 |
In a sense, perl (the C program) is a good example of embedding Perl |
440 |
(the language), so I'll demonstrate embedding with I<miniperlmain.c>, |
441 |
included in the source distribution. Here's a bastardized, non-portable |
442 |
version of I<miniperlmain.c> containing the essentials of embedding: |
443 |
|
444 |
=end original |
445 |
|
446 |
ある意味で、perl(C プログラムとしての perl)は組み込み Perl(言語としての |
447 |
Perl)の良い例です; ですから、ソース配布にある I<miniperlmain.c> を |
448 |
使って組込みのデモンストレーションをします。 |
449 |
以下に示すのは、組込みの本質を持っていて配布ソースにも含まれている |
450 |
(そして粗悪で移植性に欠ける)I<miniperlmain.c> です。 |
451 |
|
452 |
#include <EXTERN.h> /* from the Perl distribution */ |
453 |
#include <perl.h> /* from the Perl distribution */ |
454 |
|
455 |
static PerlInterpreter *my_perl; /*** The Perl interpreter ***/ |
456 |
|
457 |
int main(int argc, char **argv, char **env) |
458 |
{ |
459 |
PERL_SYS_INIT3(&argc,&argv,&env); |
460 |
my_perl = perl_alloc(); |
461 |
perl_construct(my_perl); |
462 |
PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
463 |
perl_parse(my_perl, NULL, argc, argv, (char **)NULL); |
464 |
perl_run(my_perl); |
465 |
perl_destruct(my_perl); |
466 |
perl_free(my_perl); |
467 |
PERL_SYS_TERM(); |
468 |
exit(EXIT_SUCCESS); |
469 |
} |
470 |
|
471 |
=begin original |
472 |
|
473 |
Notice that we don't use the C<env> pointer. Normally handed to |
474 |
C<perl_parse> as its final argument, C<env> here is replaced by |
475 |
C<NULL>, which means that the current environment will be used. |
476 |
|
477 |
=end original |
478 |
|
479 |
ここで、C<env> ポインタを使っていないことに注意してください。 |
480 |
通常は C<perl_parse> はその最終引数として C<env> を取りますが、 |
481 |
ここではカレントの環境をそのまま使うことを示す |
482 |
C<NULL> に置き換えられています。 |
483 |
|
484 |
=begin original |
485 |
|
486 |
The macros PERL_SYS_INIT3() and PERL_SYS_TERM() provide system-specific |
487 |
tune up of the C runtime environment necessary to run Perl interpreters; |
488 |
they should only be called once regardless of how many interpreters you |
489 |
create or destroy. Call PERL_SYS_INIT3() before you create your first |
490 |
interpreter, and PERL_SYS_TERM() after you free your last interpreter. |
491 |
|
492 |
=end original |
493 |
|
494 |
PERL_SYS_INIT3() と PERL_SYS_TERM() のマクロは、Perl インタプリタを |
495 |
実行するのに必要な C ランタイム環境の、システム固有の調整を提供します; |
496 |
これらはインタプリタが何回作成または破壊されたかに関わらず、1 回だけ |
497 |
呼び出されるべきです。 |
498 |
最初のインタプリタを作る前に PERL_SYS_INIT3() を呼び出し、 |
499 |
最後のインタプリタを解放した後に PERL_SYS_TERM() を呼び出します。 |
500 |
|
501 |
=begin original |
502 |
|
503 |
Since PERL_SYS_INIT3() may change C<env>, it may be more appropriate to |
504 |
provide C<env> as an argument to perl_parse(). |
505 |
|
506 |
=end original |
507 |
|
508 |
PERL_SYS_INIT3() は C<env> を変更するかもしれないので、perl_parse() の |
509 |
引数として C<env> を提供する方がより適切です。 |
510 |
|
511 |
=begin original |
512 |
|
513 |
Also notice that no matter what arguments you pass to perl_parse(), |
514 |
PERL_SYS_INIT3() must be invoked on the C main() argc, argv and env and |
515 |
only once. |
516 |
|
517 |
=end original |
518 |
|
519 |
また、どんな引数を perl_parse() に渡したかに関わらず、PERL_SYS_INIT3() は |
520 |
C main() argc, argv, env から一度だけ起動されなければなりません。 |
521 |
|
522 |
=begin original |
523 |
|
524 |
Mind that argv[argc] must be NULL, same as those passed to a main |
525 |
function in C. |
526 |
|
527 |
=end original |
528 |
|
529 |
C の main 関数に渡されるものと同じように、 |
530 |
argv[argc] は NULL でなければならないことを注意してください。 |
531 |
|
532 |
=begin original |
533 |
|
534 |
Now compile this program (I'll call it I<interp.c>) into an executable: |
535 |
|
536 |
=end original |
537 |
|
538 |
ここでこのプログラム(I<interp.c> と呼びましょう)をコンパイルして |
539 |
実行ファイルを作りましょう。 |
540 |
|
541 |
% cc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts` |
542 |
|
543 |
=begin original |
544 |
|
545 |
After a successful compilation, you'll be able to use I<interp> just |
546 |
like perl itself: |
547 |
|
548 |
=end original |
549 |
|
550 |
コンパイルに成功すれば、perl そのものと同じように |
551 |
I<interp> を使うことができるでしょう。 |
552 |
|
553 |
% interp |
554 |
print "Pretty Good Perl \n"; |
555 |
print "10890 - 9801 is ", 10890 - 9801; |
556 |
<CTRL-D> |
557 |
Pretty Good Perl |
558 |
10890 - 9801 is 1089 |
559 |
|
560 |
=begin original |
561 |
|
562 |
or |
563 |
|
564 |
=end original |
565 |
|
566 |
または |
567 |
|
568 |
% interp -e 'printf("%x", 3735928559)' |
569 |
deadbeef |
570 |
|
571 |
=begin original |
572 |
|
573 |
You can also read and execute Perl statements from a file while in the |
574 |
midst of your C program, by placing the filename in I<argv[1]> before |
575 |
calling I<perl_run>. |
576 |
|
577 |
=end original |
578 |
|
579 |
I<perl_run> を呼び出す前に I<argv[1]> にファイル名を置くことで、 |
580 |
C プログラムの中ほどで Perl 文をファイルから読み込んで実行するという |
581 |
こともできます。 |
582 |
|
583 |
=head2 Calling a Perl subroutine from your C program |
584 |
|
585 |
(あなたの C プログラムから Perl のサブルーチンを呼ぶ) |
586 |
|
587 |
=begin original |
588 |
|
589 |
To call individual Perl subroutines, you can use any of the B<call_*> |
590 |
functions documented in L<perlcall>. |
591 |
In this example we'll use C<call_argv>. |
592 |
|
593 |
=end original |
594 |
|
595 |
独立した Perl サブルーチンを呼び出すために、L<perlcall> に記載されている |
596 |
B<call_*> の類の関数を使うことができます。 |
597 |
次の例では C<call_argv> を使います。 |
598 |
|
599 |
=begin original |
600 |
|
601 |
That's shown below, in a program I'll call I<showtime.c>. |
602 |
|
603 |
=end original |
604 |
|
605 |
以下に示すのは、I<showtime.c> という名前のプログラムです。 |
606 |
|
607 |
#include <EXTERN.h> |
608 |
#include <perl.h> |
609 |
|
610 |
static PerlInterpreter *my_perl; |
611 |
|
612 |
int main(int argc, char **argv, char **env) |
613 |
{ |
614 |
char *args[] = { NULL }; |
615 |
PERL_SYS_INIT3(&argc,&argv,&env); |
616 |
my_perl = perl_alloc(); |
617 |
perl_construct(my_perl); |
618 |
|
619 |
perl_parse(my_perl, NULL, argc, argv, NULL); |
620 |
PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
621 |
|
622 |
/*** skipping perl_run() ***/ |
623 |
|
624 |
call_argv("showtime", G_DISCARD | G_NOARGS, args); |
625 |
|
626 |
perl_destruct(my_perl); |
627 |
perl_free(my_perl); |
628 |
PERL_SYS_TERM(); |
629 |
exit(EXIT_SUCCESS); |
630 |
} |
631 |
|
632 |
=begin original |
633 |
|
634 |
where I<showtime> is a Perl subroutine that takes no arguments (that's the |
635 |
I<G_NOARGS>) and for which I'll ignore the return value (that's the |
636 |
I<G_DISCARD>). Those flags, and others, are discussed in L<perlcall>. |
637 |
|
638 |
=end original |
639 |
|
640 |
I<showtime> は引数をとらない Perl サブルーチン(I<G_NOARGS>)で、その |
641 |
戻り値を無視します(I<G_DISCARD>)。 |
642 |
これらのフラグ等は L<perlcall> に説明されています。 |
643 |
|
644 |
=begin original |
645 |
|
646 |
I'll define the I<showtime> subroutine in a file called I<showtime.pl>: |
647 |
|
648 |
=end original |
649 |
|
650 |
I<showtime.pl> と呼ばれるファイルで、I<showtime> サブルーチンを |
651 |
定義しましょう: |
652 |
|
653 |
print "I shan't be printed."; |
654 |
|
655 |
sub showtime { |
656 |
print time; |
657 |
} |
658 |
|
659 |
=begin original |
660 |
|
661 |
Simple enough. Now compile and run: |
662 |
|
663 |
=end original |
664 |
|
665 |
単純にして十分です。 |
666 |
さあコンパイルして実行してみましょう: |
667 |
|
668 |
% cc -o showtime showtime.c \ |
669 |
`perl -MExtUtils::Embed -e ccopts -e ldopts` |
670 |
% showtime showtime.pl |
671 |
818284590 |
672 |
|
673 |
=begin original |
674 |
|
675 |
yielding the number of seconds that elapsed between January 1, 1970 |
676 |
(the beginning of the Unix epoch), and the moment I began writing this |
677 |
sentence. |
678 |
|
679 |
=end original |
680 |
|
681 |
1970 年 1 月 1 日(UNIX 紀元の開始時点)からこのセンテンスを書いた時点までの |
682 |
経過秒数が得られます。 |
683 |
|
684 |
=begin original |
685 |
|
686 |
In this particular case we don't have to call I<perl_run>, as we set |
687 |
the PL_exit_flag PERL_EXIT_DESTRUCT_END which executes END blocks in |
688 |
perl_destruct. |
689 |
|
690 |
=end original |
691 |
|
692 |
この特殊な状況では、私たちは I<perl_run> を呼ぶ必要はありません; |
693 |
perl_destruct で END ブロックを実行する |
694 |
PL_exit_flag PERL_EXIT_DESTRUCT_END をセットしているからです。 |
695 |
|
696 |
=begin original |
697 |
|
698 |
If you want to pass arguments to the Perl subroutine, you can add |
699 |
strings to the C<NULL>-terminated C<args> list passed to |
700 |
I<call_argv>. For other data types, or to examine return values, |
701 |
you'll need to manipulate the Perl stack. That's demonstrated in |
702 |
L</Fiddling with the Perl stack from your C program>. |
703 |
|
704 |
=end original |
705 |
|
706 |
Perl サブルーチンに引数を渡したいという場合、I<call_argv> に |
707 |
渡される C<NULL> で終端されたリスト C<args> に文字列を追加することが |
708 |
できます。 |
709 |
他のデータ型を使ったり戻り値をチェックするには Perl スタックを |
710 |
操作する必要があるでしょう。 |
711 |
これは本ドキュメントの最後の |
712 |
セクション L</Fiddling with the Perl stack from your C program> で |
713 |
説明されています。 |
714 |
|
715 |
=head2 Evaluating a Perl statement from your C program |
716 |
|
717 |
(CプログラムからPerlの文を評価する) |
718 |
|
719 |
=begin original |
720 |
|
721 |
Perl provides two API functions to evaluate pieces of Perl code. |
722 |
These are L<perlapi/eval_sv> and L<perlapi/eval_pv>. |
723 |
|
724 |
=end original |
725 |
|
726 |
Perl は Perl コードのかけら(pieces of Perl code)を評価するための二つの |
727 |
API 関数を提供しています。 |
728 |
L<perlapi/eval_sv> と L<perlapi/eval_pv> です。 |
729 |
|
730 |
=begin original |
731 |
|
732 |
Arguably, these are the only routines you'll ever need to execute |
733 |
snippets of Perl code from within your C program. Your code can be as |
734 |
long as you wish; it can contain multiple statements; it can employ |
735 |
L<perlfunc/use>, L<perlfunc/require>, and L<perlfunc/do> to |
736 |
include external Perl files. |
737 |
|
738 |
=end original |
739 |
|
740 |
これらの関数は、C プログラムの中で Perl のコードの断片を実行するのに |
741 |
必要だったルーチンにすぎません。 |
742 |
あなたはコードを好きなだけ長くでき、複数の文を含むことができます; |
743 |
また、外部の Perl ファイルを取り込むために L<perlfunc/use>, |
744 |
L<perlfunc/require>, L<perlfunc/do> を使うことができます。 |
745 |
|
746 |
=begin original |
747 |
|
748 |
I<eval_pv> lets us evaluate individual Perl strings, and then |
749 |
extract variables for coercion into C types. The following program, |
750 |
I<string.c>, executes three Perl strings, extracting an C<int> from |
751 |
the first, a C<float> from the second, and a C<char *> from the third. |
752 |
|
753 |
=end original |
754 |
|
755 |
I<eval_pv> は独立した Perl の文字列を評価し、強制的に C の型へと |
756 |
変数を展開します。 |
757 |
以下に示すプログラムは I<string.c> は、三つの |
758 |
Perl 文字列を実行し、最初のものを C<int >に、二番目のものを C<float> に、 |
759 |
三番目のものを C<char *>へ 展開します。 |
760 |
|
761 |
#include <EXTERN.h> |
762 |
#include <perl.h> |
763 |
|
764 |
static PerlInterpreter *my_perl; |
765 |
|
766 |
main (int argc, char **argv, char **env) |
767 |
{ |
768 |
char *embedding[] = { "", "-e", "0", NULL }; |
769 |
|
770 |
PERL_SYS_INIT3(&argc,&argv,&env); |
771 |
my_perl = perl_alloc(); |
772 |
perl_construct( my_perl ); |
773 |
|
774 |
perl_parse(my_perl, NULL, 3, embedding, NULL); |
775 |
PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
776 |
perl_run(my_perl); |
777 |
|
778 |
/** Treat $a as an integer **/ |
779 |
eval_pv("$a = 3; $a **= 2", TRUE); |
780 |
printf("a = %d\n", SvIV(get_sv("a", 0))); |
781 |
|
782 |
/** Treat $a as a float **/ |
783 |
eval_pv("$a = 3.14; $a **= 2", TRUE); |
784 |
printf("a = %f\n", SvNV(get_sv("a", 0))); |
785 |
|
786 |
/** Treat $a as a string **/ |
787 |
eval_pv( |
788 |
"$a = 'rekcaH lreP rehtonA tsuJ'; $a = reverse($a);", TRUE); |
789 |
printf("a = %s\n", SvPV_nolen(get_sv("a", 0))); |
790 |
|
791 |
perl_destruct(my_perl); |
792 |
perl_free(my_perl); |
793 |
PERL_SYS_TERM(); |
794 |
} |
795 |
|
796 |
=begin original |
797 |
|
798 |
All of those strange functions with I<sv> in their names help convert Perl |
799 |
scalars to C types. They're described in L<perlguts> and L<perlapi>. |
800 |
|
801 |
=end original |
802 |
|
803 |
名前に I<sv> の付いた奇妙な関数は Perl のスカラから C の型への変換を |
804 |
手助けします。 |
805 |
これらは L<perlguts> と L<perlapi> に記述されています。 |
806 |
|
807 |
=begin original |
808 |
|
809 |
If you compile and run I<string.c>, you'll see the results of using |
810 |
I<SvIV()> to create an C<int>, I<SvNV()> to create a C<float>, and |
811 |
I<SvPV()> to create a string: |
812 |
|
813 |
=end original |
814 |
|
815 |
I<string.c> をコンパイルして実行すれば、I<SvIV()> を使って C<int> を、 |
816 |
I<SvNV()> を使って C<float> を、I<SvPV()> を使って文字列を生成した |
817 |
結果を見ることになるでしょう。 |
818 |
|
819 |
a = 9 |
820 |
a = 9.859600 |
821 |
a = Just Another Perl Hacker |
822 |
|
823 |
=begin original |
824 |
|
825 |
In the example above, we've created a global variable to temporarily |
826 |
store the computed value of our eval'ed expression. It is also |
827 |
possible and in most cases a better strategy to fetch the return value |
828 |
from I<eval_pv()> instead. Example: |
829 |
|
830 |
=end original |
831 |
|
832 |
先の例では、私たちが式を評価した値を一時的に格納するためのグローバル |
833 |
変数を生成しました。 |
834 |
これと同様なことが I<eval_pv()> の戻り値を取ることで可能であり、かつ |
835 |
ほとんどの場合にはこれが良い戦略なのです。 |
836 |
例えば: |
837 |
|
838 |
... |
839 |
SV *val = eval_pv("reverse 'rekcaH lreP rehtonA tsuJ'", TRUE); |
840 |
printf("%s\n", SvPV_nolen(val)); |
841 |
... |
842 |
|
843 |
=begin original |
844 |
|
845 |
This way, we avoid namespace pollution by not creating global |
846 |
variables and we've simplified our code as well. |
847 |
|
848 |
=end original |
849 |
|
850 |
このやり方によればグローバル変数を生成せず、またプログラムを単純に |
851 |
したことによって名前空間の汚染を防ぎます。 |
852 |
|
853 |
=head2 Performing Perl pattern matches and substitutions from your C program |
854 |
|
855 |
(CプログラムからPerlのパターンマッチングと置換を使ってみる) |
856 |
|
857 |
=begin original |
858 |
|
859 |
The I<eval_sv()> function lets us evaluate strings of Perl code, so we can |
860 |
define some functions that use it to "specialize" in matches and |
861 |
substitutions: I<match()>, I<substitute()>, and I<matches()>. |
862 |
|
863 |
=end original |
864 |
|
865 |
関数 I<eval_pv> は Perl コードの文字列を評価するものなので、 |
866 |
私たちはマッチや置換に「特化」(specilalize)するために使う |
867 |
I<match()>, I<substitute()>, I<matches()>といった幾つかの関数を |
868 |
定義できました。 |
869 |
|
870 |
I32 match(SV *string, char *pattern); |
871 |
|
872 |
=begin original |
873 |
|
874 |
Given a string and a pattern (e.g., C<m/clasp/> or C</\b\w*\b/>, which |
875 |
in your C program might appear as "/\\b\\w*\\b/"), match() |
876 |
returns 1 if the string matches the pattern and 0 otherwise. |
877 |
|
878 |
=end original |
879 |
|
880 |
文字列と C<m/clasp/> や C</\b\w*\b/> のようなパターン(これは |
881 |
C プログラムでは"/\\b\\w*\\b/" のようになっているでしょう)を与えると、 |
882 |
match() は文字列がパターンにマッチしたときには1を、そうでないときは |
883 |
0 を返します。 |
884 |
|
885 |
int substitute(SV **string, char *pattern); |
886 |
|
887 |
=begin original |
888 |
|
889 |
Given a pointer to an C<SV> and an C<=~> operation (e.g., |
890 |
C<s/bob/robert/g> or C<tr[A-Z][a-z]>), substitute() modifies the string |
891 |
within the C<SV> as according to the operation, returning the number of |
892 |
substitutions made. |
893 |
|
894 |
=end original |
895 |
|
896 |
C<SV> へのポインタと C<=~> 操作(C<s/bob/robert/g> もしくは |
897 |
C<tr[A-Z][a-z]>)を与えると、substitue() はその操作に従って |
898 |
C<SV> の中の文字列の書き換えを行って、置換の回数を返します。 |
899 |
|
900 |
SSize_t matches(SV *string, char *pattern, AV **matches); |
901 |
|
902 |
=begin original |
903 |
|
904 |
Given an C<SV>, a pattern, and a pointer to an empty C<AV>, |
905 |
matches() evaluates C<$string =~ $pattern> in a list context, and |
906 |
fills in I<matches> with the array elements, returning the number of matches |
907 |
found. |
908 |
|
909 |
=end original |
910 |
|
911 |
C<SV>、パターン、空の C<AV> へのポインタを渡すと、 |
912 |
matchs() はリストコンテキストで C<$string =~ $pattern> を評価して |
913 |
I<matches> を配列の要素(メモリを割り当てます)で満たし、マッチした |
914 |
回数を返します。 |
915 |
|
916 |
=begin original |
917 |
|
918 |
Here's a sample program, I<match.c>, that uses all three (long lines have |
919 |
been wrapped here): |
920 |
|
921 |
=end original |
922 |
|
923 |
以下に示すのはサンプルプログラム I<match.c> で、三つの関数すべてを |
924 |
使います(長い行は整形されてます)。 |
925 |
|
926 |
#include <EXTERN.h> |
927 |
#include <perl.h> |
928 |
|
929 |
static PerlInterpreter *my_perl; |
930 |
|
931 |
/** my_eval_sv(code, error_check) |
932 |
** kinda like eval_sv(), |
933 |
** but we pop the return value off the stack |
934 |
**/ |
935 |
SV* my_eval_sv(SV *sv, I32 croak_on_error) |
936 |
{ |
937 |
dSP; |
938 |
SV* retval; |
939 |
|
940 |
PUSHMARK(SP); |
941 |
eval_sv(sv, G_SCALAR); |
942 |
|
943 |
SPAGAIN; |
944 |
retval = POPs; |
945 |
PUTBACK; |
946 |
|
947 |
if (croak_on_error && SvTRUE(ERRSV)) |
948 |
croak_sv(ERRSV); |
949 |
|
950 |
return retval; |
951 |
} |
952 |
|
953 |
/** match(string, pattern) |
954 |
** |
955 |
** Used for matches in a scalar context. |
956 |
** |
957 |
** Returns 1 if the match was successful; 0 otherwise. |
958 |
**/ |
959 |
|
960 |
I32 match(SV *string, char *pattern) |
961 |
{ |
962 |
SV *command = newSV(0), *retval; |
963 |
|
964 |
sv_setpvf(command, "my $string = '%s'; $string =~ %s", |
965 |
SvPV_nolen(string), pattern); |
966 |
|
967 |
retval = my_eval_sv(command, TRUE); |
968 |
SvREFCNT_dec(command); |
969 |
|
970 |
return SvIV(retval); |
971 |
} |
972 |
|
973 |
/** substitute(string, pattern) |
974 |
** |
975 |
** Used for =~ operations that |
976 |
** modify their left-hand side (s/// and tr///) |
977 |
** |
978 |
** Returns the number of successful matches, and |
979 |
** modifies the input string if there were any. |
980 |
**/ |
981 |
|
982 |
I32 substitute(SV **string, char *pattern) |
983 |
{ |
984 |
SV *command = newSV(0), *retval; |
985 |
|
986 |
sv_setpvf(command, "$string = '%s'; ($string =~ %s)", |
987 |
SvPV_nolen(*string), pattern); |
988 |
|
989 |
retval = my_eval_sv(command, TRUE); |
990 |
SvREFCNT_dec(command); |
991 |
|
992 |
*string = get_sv("string", 0); |
993 |
return SvIV(retval); |
994 |
} |
995 |
|
996 |
/** matches(string, pattern, matches) |
997 |
** |
998 |
** Used for matches in a list context. |
999 |
** |
1000 |
** Returns the number of matches, |
1001 |
** and fills in **matches with the matching substrings |
1002 |
**/ |
1003 |
|
1004 |
SSize_t matches(SV *string, char *pattern, AV **match_list) |
1005 |
{ |
1006 |
SV *command = newSV(0); |
1007 |
SSize_t num_matches; |
1008 |
|
1009 |
sv_setpvf(command, "my $string = '%s'; @array = ($string =~ %s)", |
1010 |
SvPV_nolen(string), pattern); |
1011 |
|
1012 |
my_eval_sv(command, TRUE); |
1013 |
SvREFCNT_dec(command); |
1014 |
|
1015 |
*match_list = get_av("array", 0); |
1016 |
num_matches = av_top_index(*match_list) + 1; |
1017 |
|
1018 |
return num_matches; |
1019 |
} |
1020 |
|
1021 |
main (int argc, char **argv, char **env) |
1022 |
{ |
1023 |
char *embedding[] = { "", "-e", "0", NULL }; |
1024 |
AV *match_list; |
1025 |
I32 num_matches, i; |
1026 |
SV *text; |
1027 |
|
1028 |
PERL_SYS_INIT3(&argc,&argv,&env); |
1029 |
my_perl = perl_alloc(); |
1030 |
perl_construct(my_perl); |
1031 |
perl_parse(my_perl, NULL, 3, embedding, NULL); |
1032 |
PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
1033 |
|
1034 |
text = newSV(0); |
1035 |
sv_setpv(text, "When he is at a convenience store and the " |
1036 |
"bill comes to some amount like 76 cents, Maynard is " |
1037 |
"aware that there is something he *should* do, something " |
1038 |
"that will enable him to get back a quarter, but he has " |
1039 |
"no idea *what*. He fumbles through his red squeezey " |
1040 |
"changepurse and gives the boy three extra pennies with " |
1041 |
"his dollar, hoping that he might luck into the correct " |
1042 |
"amount. The boy gives him back two of his own pennies " |
1043 |
"and then the big shiny quarter that is his prize. " |
1044 |
"-RICHH"); |
1045 |
|
1046 |
if (match(text, "m/quarter/")) /** Does text contain 'quarter'? **/ |
1047 |
printf("match: Text contains the word 'quarter'.\n\n"); |
1048 |
else |
1049 |
printf("match: Text doesn't contain the word 'quarter'.\n\n"); |
1050 |
|
1051 |
if (match(text, "m/eighth/")) /** Does text contain 'eighth'? **/ |
1052 |
printf("match: Text contains the word 'eighth'.\n\n"); |
1053 |
else |
1054 |
printf("match: Text doesn't contain the word 'eighth'.\n\n"); |
1055 |
|
1056 |
/** Match all occurrences of /wi../ **/ |
1057 |
num_matches = matches(text, "m/(wi..)/g", &match_list); |
1058 |
printf("matches: m/(wi..)/g found %d matches...\n", num_matches); |
1059 |
|
1060 |
for (i = 0; i < num_matches; i++) |
1061 |
printf("match: %s\n", |
1062 |
SvPV_nolen(*av_fetch(match_list, i, FALSE))); |
1063 |
printf("\n"); |
1064 |
|
1065 |
/** Remove all vowels from text **/ |
1066 |
num_matches = substitute(&text, "s/[aeiou]//gi"); |
1067 |
if (num_matches) { |
1068 |
printf("substitute: s/[aeiou]//gi...%lu substitutions made.\n", |
1069 |
(unsigned long)num_matches); |
1070 |
printf("Now text is: %s\n\n", SvPV_nolen(text)); |
1071 |
} |
1072 |
|
1073 |
/** Attempt a substitution **/ |
1074 |
if (!substitute(&text, "s/Perl/C/")) { |
1075 |
printf("substitute: s/Perl/C...No substitution made.\n\n"); |
1076 |
} |
1077 |
|
1078 |
SvREFCNT_dec(text); |
1079 |
PL_perl_destruct_level = 1; |
1080 |
perl_destruct(my_perl); |
1081 |
perl_free(my_perl); |
1082 |
PERL_SYS_TERM(); |
1083 |
} |
1084 |
|
1085 |
=begin original |
1086 |
|
1087 |
which produces the output (again, long lines have been wrapped here) |
1088 |
|
1089 |
=end original |
1090 |
|
1091 |
この出力は以下のようになります(繰り返しますが、長い行は |
1092 |
整形されています)。 |
1093 |
|
1094 |
match: Text contains the word 'quarter'. |
1095 |
|
1096 |
match: Text doesn't contain the word 'eighth'. |
1097 |
|
1098 |
matches: m/(wi..)/g found 2 matches... |
1099 |
match: will |
1100 |
match: with |
1101 |
|
1102 |
substitute: s/[aeiou]//gi...139 substitutions made. |
1103 |
Now text is: Whn h s t cnvnnc str nd th bll cms t sm mnt lk 76 cnts, |
1104 |
Mynrd s wr tht thr s smthng h *shld* d, smthng tht wll nbl hm t gt |
1105 |
bck qrtr, bt h hs n d *wht*. H fmbls thrgh hs rd sqzy chngprs nd |
1106 |
gvs th by thr xtr pnns wth hs dllr, hpng tht h mght lck nt th crrct |
1107 |
mnt. Th by gvs hm bck tw f hs wn pnns nd thn th bg shny qrtr tht s |
1108 |
hs prz. -RCHH |
1109 |
|
1110 |
substitute: s/Perl/C...No substitution made. |
1111 |
|
1112 |
=head2 Fiddling with the Perl stack from your C program |
1113 |
|
1114 |
(C プログラムから Perl のスタックを見つけだす) |
1115 |
|
1116 |
=begin original |
1117 |
|
1118 |
When trying to explain stacks, most computer science textbooks mumble |
1119 |
something about spring-loaded columns of cafeteria plates: the last |
1120 |
thing you pushed on the stack is the first thing you pop off. That'll |
1121 |
do for our purposes: your C program will push some arguments onto "the Perl |
1122 |
stack", shut its eyes while some magic happens, and then pop the |
1123 |
results--the return value of your Perl subroutine--off the stack. |
1124 |
|
1125 |
=end original |
1126 |
|
1127 |
スタックを説明しようとするとき、ほとんどのコンピュータ科学の教科書は |
1128 |
食堂のプレート皿のばね仕掛けの仕切りのようなはっきりとしない解説をします: |
1129 |
最後にスタックに押し込んだ(push)ものが最初に取り出す(pop)ものです。 |
1130 |
これが、私たちの目的のために行うことです: あなたの C プログラムでは、幾つかの |
1131 |
引数を「Perl スタック」へプッシュして、魔法が掛かる間に目を閉じれば結果、 |
1132 |
つまりあなたの使った Perl サブルーチンの戻り値がスタックの一番上に |
1133 |
できます -- これをポップします。 |
1134 |
|
1135 |
=begin original |
1136 |
|
1137 |
First you'll need to know how to convert between C types and Perl |
1138 |
types, with newSViv() and sv_setnv() and newAV() and all their |
1139 |
friends. They're described in L<perlguts> and L<perlapi>. |
1140 |
|
1141 |
=end original |
1142 |
|
1143 |
まず第一に、あなたは newSViv()、sv_setnv()、newAV() そしてその他の |
1144 |
関数を使った C の型と Perl の型との間の変換の方法を知る必要があります。 |
1145 |
これらは L<perlguts> と L<perlapi> に記述されています。 |
1146 |
|
1147 |
=begin original |
1148 |
|
1149 |
Then you'll need to know how to manipulate the Perl stack. That's |
1150 |
described in L<perlcall>. |
1151 |
|
1152 |
=end original |
1153 |
|
1154 |
それから Perl スタックの操作方法を知る必要があります。 |
1155 |
これは L<perlcall> に説明があります。 |
1156 |
|
1157 |
=begin original |
1158 |
|
1159 |
Once you've understood those, embedding Perl in C is easy. |
1160 |
|
1161 |
=end original |
1162 |
|
1163 |
これらを理解すれば、C に Perl を組み込むのは簡単です。 |
1164 |
|
1165 |
=begin original |
1166 |
|
1167 |
Because C has no builtin function for integer exponentiation, let's |
1168 |
make Perl's ** operator available to it (this is less useful than it |
1169 |
sounds, because Perl implements ** with C's I<pow()> function). First |
1170 |
I'll create a stub exponentiation function in I<power.pl>: |
1171 |
|
1172 |
=end original |
1173 |
|
1174 |
C には整数のべき乗を行う組み込み関数がないので、Perl の ** 演算子を |
1175 |
使えるようにしましょう(これは思ったほど便利ではありません; なぜなら Perl は |
1176 |
** を C の I<pow()> 関数を使って実装しているからです)。 |
1177 |
最初に I<power.pl> にべき乗関数を作成しましょう。 |
1178 |
|
1179 |
sub expo { |
1180 |
my ($a, $b) = @_; |
1181 |
return $a ** $b; |
1182 |
} |
1183 |
|
1184 |
=begin original |
1185 |
|
1186 |
Now I'll create a C program, I<power.c>, with a function |
1187 |
I<PerlPower()> that contains all the perlguts necessary to push the |
1188 |
two arguments into I<expo()> and to pop the return value out. Take a |
1189 |
deep breath... |
1190 |
|
1191 |
=end original |
1192 |
|
1193 |
今度は I<expo()> への二つの引数をプッシュするのとそこからの戻り値 |
1194 |
をポップするに必要なすべての perlguts を含んでいる I<PerlPower()> と |
1195 |
いう関数を持った C プログラム I<power.c> を作ります。 |
1196 |
深く息をすって… |
1197 |
|
1198 |
#include <EXTERN.h> |
1199 |
#include <perl.h> |
1200 |
|
1201 |
static PerlInterpreter *my_perl; |
1202 |
|
1203 |
static void |
1204 |
PerlPower(int a, int b) |
1205 |
{ |
1206 |
dSP; /* initialize stack pointer */ |
1207 |
ENTER; /* everything created after here */ |
1208 |
SAVETMPS; /* ...is a temporary variable. */ |
1209 |
PUSHMARK(SP); /* remember the stack pointer */ |
1210 |
XPUSHs(sv_2mortal(newSViv(a))); /* push the base onto the stack */ |
1211 |
XPUSHs(sv_2mortal(newSViv(b))); /* push the exponent onto stack */ |
1212 |
PUTBACK; /* make local stack pointer global */ |
1213 |
call_pv("expo", G_SCALAR); /* call the function */ |
1214 |
SPAGAIN; /* refresh stack pointer */ |
1215 |
/* pop the return value from stack */ |
1216 |
printf ("%d to the %dth power is %d.\n", a, b, POPi); |
1217 |
PUTBACK; |
1218 |
FREETMPS; /* free that return value */ |
1219 |
LEAVE; /* ...and the XPUSHed "mortal" args.*/ |
1220 |
} |
1221 |
|
1222 |
int main (int argc, char **argv, char **env) |
1223 |
{ |
1224 |
char *my_argv[] = { "", "power.pl", NULL }; |
1225 |
|
1226 |
PERL_SYS_INIT3(&argc,&argv,&env); |
1227 |
my_perl = perl_alloc(); |
1228 |
perl_construct( my_perl ); |
1229 |
|
1230 |
perl_parse(my_perl, NULL, 2, my_argv, (char **)NULL); |
1231 |
PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
1232 |
perl_run(my_perl); |
1233 |
|
1234 |
PerlPower(3, 4); /*** Compute 3 ** 4 ***/ |
1235 |
|
1236 |
perl_destruct(my_perl); |
1237 |
perl_free(my_perl); |
1238 |
PERL_SYS_TERM(); |
1239 |
exit(EXIT_SUCCESS); |
1240 |
} |
1241 |
|
1242 |
=begin original |
1243 |
|
1244 |
Compile and run: |
1245 |
|
1246 |
=end original |
1247 |
|
1248 |
コンパイルして実行しましょう: |
1249 |
|
1250 |
% cc -o power power.c `perl -MExtUtils::Embed -e ccopts -e ldopts` |
1251 |
|
1252 |
% power |
1253 |
3 to the 4th power is 81. |
1254 |
|
1255 |
=head2 Maintaining a persistent interpreter |
1256 |
|
1257 |
(永続的インタプリタの保持) |
1258 |
|
1259 |
=begin original |
1260 |
|
1261 |
When developing interactive and/or potentially long-running |
1262 |
applications, it's a good idea to maintain a persistent interpreter |
1263 |
rather than allocating and constructing a new interpreter multiple |
1264 |
times. The major reason is speed: since Perl will only be loaded into |
1265 |
memory once. |
1266 |
|
1267 |
=end original |
1268 |
|
1269 |
対話的であるとか、長い時間実行される可能性のあるアプリケーションを |
1270 |
開発するとき、複数回新しいインタプリタを割り当てて構築するよりは永続的な |
1271 |
インタプリタを保持するのが良い考えです。 |
1272 |
主な理由はスピードです: (永続的にすることで)Perl はメモリーに一度しか |
1273 |
ロードされません。 |
1274 |
|
1275 |
=begin original |
1276 |
|
1277 |
However, you have to be more cautious with namespace and variable |
1278 |
scoping when using a persistent interpreter. In previous examples |
1279 |
we've been using global variables in the default package C<main>. We |
1280 |
knew exactly what code would be run, and assumed we could avoid |
1281 |
variable collisions and outrageous symbol table growth. |
1282 |
|
1283 |
=end original |
1284 |
|
1285 |
しかしながら永続的なインタプリタを使うときには、名前空間や変数の |
1286 |
スコープをより注意深く扱わねばならないでしょう。 |
1287 |
先の例では、私たちはデフォルトパッケージ C<main> でグローバル変数を |
1288 |
使いました。 |
1289 |
私たちは実際にあのコードが実行できることを知りましたが、変数の衝突や |
1290 |
シンボルテーブル拡大の限界に当たらないことを仮定していました。 |
1291 |
|
1292 |
=begin original |
1293 |
|
1294 |
Let's say your application is a server that will occasionally run Perl |
1295 |
code from some arbitrary file. Your server has no way of knowing what |
1296 |
code it's going to run. Very dangerous. |
1297 |
|
1298 |
=end original |
1299 |
|
1300 |
あなたのアプリケーションが、時折幾つかの任意のファイルから Perl コードが |
1301 |
実行されるサーバーであるとしましょう。 |
1302 |
あなたのサーバーは実行させたコードを知る術がありません。 |
1303 |
非常に危険です。 |
1304 |
|
1305 |
=begin original |
1306 |
|
1307 |
If the file is pulled in by C<perl_parse()>, compiled into a newly |
1308 |
constructed interpreter, and subsequently cleaned out with |
1309 |
C<perl_destruct()> afterwards, you're shielded from most namespace |
1310 |
troubles. |
1311 |
|
1312 |
=end original |
1313 |
|
1314 |
ファイルが C<perl_parse()> を使って引き込まれた場合、新しく構築された |
1315 |
インタプリタへコンパイルされ、後の C<perl_destruct()> で始末されます; |
1316 |
これによってほとんどの名前空間にまつわるトラブルから守られます。 |
1317 |
|
1318 |
=begin original |
1319 |
|
1320 |
One way to avoid namespace collisions in this scenario is to translate |
1321 |
the filename into a guaranteed-unique package name, and then compile |
1322 |
the code into that package using L<perlfunc/eval>. In the example |
1323 |
below, each file will only be compiled once. Or, the application |
1324 |
might choose to clean out the symbol table associated with the file |
1325 |
after it's no longer needed. Using L<perlapi/call_argv>, We'll |
1326 |
call the subroutine C<Embed::Persistent::eval_file> which lives in the |
1327 |
file C<persistent.pl> and pass the filename and boolean cleanup/cache |
1328 |
flag as arguments. |
1329 |
|
1330 |
=end original |
1331 |
|
1332 |
このシナリオにおいて名前空間の衝突を防ぐ一つの方法は、ファイル名を |
1333 |
ユニークであることが保証されているパッケージ名に変換し、それから |
1334 |
L<perlfunc/eval> を使ったコードにコンパイルするのです。 |
1335 |
先の例では、各ファイルは一回だけコンパイルされました。 |
1336 |
あるいは、アプリケーションはファイルに結び付けられたシンボルテーブルを |
1337 |
それが必要なくなった時点で掃除してしまうことを選択するかもしれません。 |
1338 |
L<perlapi/call_argv> を使うことで、私たちは C<persistent.pl> という |
1339 |
ファイルにあるサブルーチン C<Embed::Persistent::eval_file> を呼び出し、それに |
1340 |
ファイル名と cleanup/cache を表わすブール値を引数として渡します。 |
1341 |
|
1342 |
=begin original |
1343 |
|
1344 |
Note that the process will continue to grow for each file that it |
1345 |
uses. In addition, there might be C<AUTOLOAD>ed subroutines and other |
1346 |
conditions that cause Perl's symbol table to grow. You might want to |
1347 |
add some logic that keeps track of the process size, or restarts |
1348 |
itself after a certain number of requests, to ensure that memory |
1349 |
consumption is minimized. You'll also want to scope your variables |
1350 |
with L<perlfunc/my> whenever possible. |
1351 |
|
1352 |
=end original |
1353 |
|
1354 |
プロセスはそれぞれのファイルを使うごとに大きくなるということに |
1355 |
注意してください。 |
1356 |
それに加えて、C<AUTOLOAD> されたサブルーチンなどの条件によっても、 |
1357 |
Perl のシンボルテーブルは大きくなります。 |
1358 |
あなたはプロセスの大きさを記録しつづける何等かのロジックを |
1359 |
欲するかもしれませんし、あるいはメモリの消費量を最少にするために |
1360 |
特定回数のリクエストの後で自分自身を |
1361 |
再スタートするようにしたいかもしれません。 |
1362 |
また、可能であるときにはいつでも L<perlfunc/my> を使ってあなたの |
1363 |
使う変数を見通したいでしょう。 |
1364 |
|
1365 |
package Embed::Persistent; |
1366 |
#persistent.pl |
1367 |
|
1368 |
use strict; |
1369 |
our %Cache; |
1370 |
use Symbol qw(delete_package); |
1371 |
|
1372 |
sub valid_package_name { |
1373 |
my($string) = @_; |
1374 |
$string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg; |
1375 |
# second pass only for words starting with a digit |
1376 |
$string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg; |
1377 |
|
1378 |
# Dress it up as a real package name |
1379 |
$string =~ s|/|::|g; |
1380 |
return "Embed" . $string; |
1381 |
} |
1382 |
|
1383 |
sub eval_file { |
1384 |
my($filename, $delete) = @_; |
1385 |
my $package = valid_package_name($filename); |
1386 |
my $mtime = -M $filename; |
1387 |
if(defined $Cache{$package}{mtime} |
1388 |
&& |
1389 |
$Cache{$package}{mtime} <= $mtime) |
1390 |
{ |
1391 |
# we have compiled this subroutine already, |
1392 |
# it has not been updated on disk, nothing left to do |
1393 |
print STDERR "already compiled $package->handler\n"; |
1394 |
} |
1395 |
else { |
1396 |
local *FH; |
1397 |
open FH, $filename or die "open '$filename' $!"; |
1398 |
local($/) = undef; |
1399 |
my $sub = <FH>; |
1400 |
close FH; |
1401 |
|
1402 |
#wrap the code into a subroutine inside our unique package |
1403 |
my $eval = qq{package $package; sub handler { $sub; }}; |
1404 |
{ |
1405 |
# hide our variables within this block |
1406 |
my($filename,$mtime,$package,$sub); |
1407 |
eval $eval; |
1408 |
} |
1409 |
die $@ if $@; |
1410 |
|
1411 |
#cache it unless we're cleaning out each time |
1412 |
$Cache{$package}{mtime} = $mtime unless $delete; |
1413 |
} |
1414 |
|
1415 |
eval {$package->handler;}; |
1416 |
die $@ if $@; |
1417 |
|
1418 |
delete_package($package) if $delete; |
1419 |
|
1420 |
#take a look if you want |
1421 |
#print Devel::Symdump->rnew($package)->as_string, $/; |
1422 |
} |
1423 |
|
1424 |
1; |
1425 |
|
1426 |
__END__ |
1427 |
|
1428 |
/* persistent.c */ |
1429 |
#include <EXTERN.h> |
1430 |
#include <perl.h> |
1431 |
|
1432 |
/* 1 = clean out filename's symbol table after each request, |
1433 |
0 = don't |
1434 |
*/ |
1435 |
#ifndef DO_CLEAN |
1436 |
#define DO_CLEAN 0 |
1437 |
#endif |
1438 |
|
1439 |
#define BUFFER_SIZE 1024 |
1440 |
|
1441 |
static PerlInterpreter *my_perl = NULL; |
1442 |
|
1443 |
int |
1444 |
main(int argc, char **argv, char **env) |
1445 |
{ |
1446 |
char *embedding[] = { "", "persistent.pl", NULL }; |
1447 |
char *args[] = { "", DO_CLEAN, NULL }; |
1448 |
char filename[BUFFER_SIZE]; |
1449 |
int failing, exitstatus; |
1450 |
|
1451 |
PERL_SYS_INIT3(&argc,&argv,&env); |
1452 |
if((my_perl = perl_alloc()) == NULL) { |
1453 |
fprintf(stderr, "no memory!"); |
1454 |
exit(EXIT_FAILURE); |
1455 |
} |
1456 |
perl_construct(my_perl); |
1457 |
|
1458 |
PL_origalen = 1; /* don't let $0 assignment update the |
1459 |
proctitle or embedding[0] */ |
1460 |
failing = perl_parse(my_perl, NULL, 2, embedding, NULL); |
1461 |
PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
1462 |
if(!failing) |
1463 |
failing = perl_run(my_perl); |
1464 |
if(!failing) { |
1465 |
while(printf("Enter file name: ") && |
1466 |
fgets(filename, BUFFER_SIZE, stdin)) { |
1467 |
|
1468 |
filename[strlen(filename)-1] = '\0'; /* strip \n */ |
1469 |
/* call the subroutine, |
1470 |
passing it the filename as an argument */ |
1471 |
args[0] = filename; |
1472 |
call_argv("Embed::Persistent::eval_file", |
1473 |
G_DISCARD | G_EVAL, args); |
1474 |
|
1475 |
/* check $@ */ |
1476 |
if(SvTRUE(ERRSV)) |
1477 |
fprintf(stderr, "eval error: %s\n", SvPV_nolen(ERRSV)); |
1478 |
} |
1479 |
} |
1480 |
|
1481 |
PL_perl_destruct_level = 0; |
1482 |
exitstatus = perl_destruct(my_perl); |
1483 |
perl_free(my_perl); |
1484 |
PERL_SYS_TERM(); |
1485 |
exit(exitstatus); |
1486 |
} |
1487 |
|
1488 |
=begin original |
1489 |
|
1490 |
Now compile: |
1491 |
|
1492 |
=end original |
1493 |
|
1494 |
さあ、コンパイルしましょう: |
1495 |
|
1496 |
% cc -o persistent persistent.c \ |
1497 |
`perl -MExtUtils::Embed -e ccopts -e ldopts` |
1498 |
|
1499 |
=begin original |
1500 |
|
1501 |
Here's an example script file: |
1502 |
|
1503 |
=end original |
1504 |
|
1505 |
スクリプトファイルの例です。 |
1506 |
|
1507 |
#test.pl |
1508 |
my $string = "hello"; |
1509 |
foo($string); |
1510 |
|
1511 |
sub foo { |
1512 |
print "foo says: @_\n"; |
1513 |
} |
1514 |
|
1515 |
=begin original |
1516 |
|
1517 |
Now run: |
1518 |
|
1519 |
=end original |
1520 |
|
1521 |
これを実行してみましょう: |
1522 |
|
1523 |
% persistent |
1524 |
Enter file name: test.pl |
1525 |
foo says: hello |
1526 |
Enter file name: test.pl |
1527 |
already compiled Embed::test_2epl->handler |
1528 |
foo says: hello |
1529 |
Enter file name: ^C |
1530 |
|
1531 |
=head2 Execution of END blocks |
1532 |
|
1533 |
(END ブロックの実行) |
1534 |
|
1535 |
=begin original |
1536 |
|
1537 |
Traditionally END blocks have been executed at the end of the perl_run. |
1538 |
This causes problems for applications that never call perl_run. Since |
1539 |
perl 5.7.2 you can specify C<PL_exit_flags |= PERL_EXIT_DESTRUCT_END> |
1540 |
to get the new behaviour. This also enables the running of END blocks if |
1541 |
the perl_parse fails and C<perl_destruct> will return the exit value. |
1542 |
|
1543 |
=end original |
1544 |
|
1545 |
伝統的に END ブロックは perl_run の最後に実行されてきました。 |
1546 |
これは、perl_run を呼び出さないアプリケーションで問題となります。 |
1547 |
perl 5.7.2 から、新しい振る舞いを取らせるために |
1548 |
C<PL_exit_flags |= PERL_EXIT_DESTRUCT_END> を指定できます。 |
1549 |
これはまた、もし perl_parse が失敗して C<perl_destruct> が終了値を返した |
1550 |
ときにも END ブロックを実行するようにします。 |
1551 |
|
1552 |
=head2 $0 assignments |
1553 |
|
1554 |
($0 への代入) |
1555 |
|
1556 |
=begin original |
1557 |
|
1558 |
When a perl script assigns a value to $0 then the perl runtime will |
1559 |
try to make this value show up as the program name reported by "ps" by |
1560 |
updating the memory pointed to by the argv passed to perl_parse() and |
1561 |
also calling API functions like setproctitle() where available. This |
1562 |
behaviour might not be appropriate when embedding perl and can be |
1563 |
disabled by assigning the value C<1> to the variable C<PL_origalen> |
1564 |
before perl_parse() is called. |
1565 |
|
1566 |
=end original |
1567 |
|
1568 |
perl スクリプトが $0 に値を代入すると、perl のランタイムは、perl_parse() に |
1569 |
渡された argv が指すメモリを更新して、利用可能なら setproctitle() のような |
1570 |
API 関数も呼び出すことによって、その値を "ps" で報告される |
1571 |
プログラム名として表示させようとします。 |
1572 |
この振る舞いは組み込み perl では不適切かもしれないので、perl_parse() が |
1573 |
呼び出される前に C<PL_origalen> に C<1> を代入することで無効にできます。 |
1574 |
|
1575 |
=begin original |
1576 |
|
1577 |
The F<persistent.c> example above is for instance likely to segfault |
1578 |
when $0 is assigned to if the C<PL_origalen = 1;> assignment is |
1579 |
removed. This because perl will try to write to the read only memory |
1580 |
of the C<embedding[]> strings. |
1581 |
|
1582 |
=end original |
1583 |
|
1584 |
例えば、上述の F<persistent.c> の例は、もし C<PL_origalen = 1;> 代入を |
1585 |
取り除くと、$0 が代入されたときにおそらくセグメンテーションフォルトが |
1586 |
起きるでしょう。 |
1587 |
これは C<embedding[]> 文字列の読み込み専用メモリに |
1588 |
書き込もうとするからです。 |
1589 |
|
1590 |
=head2 Maintaining multiple interpreter instances |
1591 |
|
1592 |
(複数のインタプリタのインスタンスを保持する) |
1593 |
|
1594 |
=begin original |
1595 |
|
1596 |
Some rare applications will need to create more than one interpreter |
1597 |
during a session. Such an application might sporadically decide to |
1598 |
release any resources associated with the interpreter. |
1599 |
|
1600 |
=end original |
1601 |
|
1602 |
ごく一部のアプリケーションでは、二つ以上のインタプリタを生成する |
1603 |
必要があるかもしれません。 |
1604 |
そのようなアプリケーションはインタプリタに結び付けられたリソースを |
1605 |
解放することを決定することがあるかもしれません。 |
1606 |
|
1607 |
=begin original |
1608 |
|
1609 |
The program must take care to ensure that this takes place I<before> |
1610 |
the next interpreter is constructed. By default, when perl is not |
1611 |
built with any special options, the global variable |
1612 |
C<PL_perl_destruct_level> is set to C<0>, since extra cleaning isn't |
1613 |
usually needed when a program only ever creates a single interpreter |
1614 |
in its entire lifetime. |
1615 |
|
1616 |
=end original |
1617 |
|
1618 |
プログラムは次のインタプリタが構築されるよりB<前に>、解放が |
1619 |
発生することに注意を払い、それを保証しなければなりません。 |
1620 |
デフォルトでは、perl が特殊なオプション付きでビルドされていなければ、 |
1621 |
グローバル変数 C<PL_perl_destruct_level> は C<0> に設定されています; |
1622 |
これはプログラムが生涯ただ一つのインタプリタを作っただけの |
1623 |
ときには、普通は余分な後始末が不要であるからです。 |
1624 |
|
1625 |
=begin original |
1626 |
|
1627 |
Setting C<PL_perl_destruct_level> to C<1> makes everything squeaky clean: |
1628 |
|
1629 |
=end original |
1630 |
|
1631 |
全てを始末させるためには C<PL_perl_destruct_level> を C<1> に設定します。 |
1632 |
|
1633 |
while(1) { |
1634 |
... |
1635 |
/* reset global variables here with PL_perl_destruct_level = 1 */ |
1636 |
PL_perl_destruct_level = 1; |
1637 |
perl_construct(my_perl); |
1638 |
... |
1639 |
/* clean and reset _everything_ during perl_destruct */ |
1640 |
PL_perl_destruct_level = 1; |
1641 |
perl_destruct(my_perl); |
1642 |
perl_free(my_perl); |
1643 |
... |
1644 |
/* let's go do it again! */ |
1645 |
} |
1646 |
|
1647 |
=begin original |
1648 |
|
1649 |
When I<perl_destruct()> is called, the interpreter's syntax parse tree |
1650 |
and symbol tables are cleaned up, and global variables are reset. The |
1651 |
second assignment to C<PL_perl_destruct_level> is needed because |
1652 |
perl_construct resets it to C<0>. |
1653 |
|
1654 |
=end original |
1655 |
|
1656 |
I<perl_destruct()> が呼ばれたとき、インタプリタの構文解析木と |
1657 |
シンボルテーブルは始末され、そしてグローバル変数がリセットされます。 |
1658 |
C<PL_perl_destruct_level> への第 2 引数は、perl_construct を C<0> に |
1659 |
リセットするために必要です。 |
1660 |
|
1661 |
=begin original |
1662 |
|
1663 |
Now suppose we have more than one interpreter instance running at the |
1664 |
same time. This is feasible, but only if you used the Configure option |
1665 |
C<-Dusemultiplicity> or the options C<-Dusethreads -Duseithreads> when |
1666 |
building perl. By default, enabling one of these Configure options |
1667 |
sets the per-interpreter global variable C<PL_perl_destruct_level> to |
1668 |
C<1>, so that thorough cleaning is automatic and interpreter variables |
1669 |
are initialized correctly. Even if you don't intend to run two or |
1670 |
more interpreters at the same time, but to run them sequentially, like |
1671 |
in the above example, it is recommended to build perl with the |
1672 |
C<-Dusemultiplicity> option otherwise some interpreter variables may |
1673 |
not be initialized correctly between consecutive runs and your |
1674 |
application may crash. |
1675 |
|
1676 |
=end original |
1677 |
|
1678 |
現在、二つ以上のインタプリタのインスタンスを同時に実行することが |
1679 |
サポートされています。 |
1680 |
これは可能なことですが、Perlビルドしたときに Configure オプション |
1681 |
C<-Dusemultiplicity> か C<-Dusethreads -Duseithreads> を |
1682 |
使ったときのみです。 |
1683 |
デフォルトでは、これらの Configure オプションの一つが有効になると |
1684 |
インタプリタ単位のグローバル変数 C<PL_perl_destruct_level> が C<1> に |
1685 |
設定され、これによって徹底的なクリーニングは自動的で、インタプリタ変数は |
1686 |
正しく初期化されます。 |
1687 |
同時に複数のインタプリタを実行するつもりがない場合でも、上述の例のように |
1688 |
順番に実行させるつもりなら、C<-Dusemultiplicity> オプションをつけて |
1689 |
perl をビルドすることを推奨します; さもなければ連続した実行の間に |
1690 |
インタプリタ変数が正しく初期化されずに、アプリケーションが |
1691 |
クラッシュするかもしれません。 |
1692 |
|
1693 |
=begin original |
1694 |
|
1695 |
See also L<perlxs/Thread-aware system interfaces>. |
1696 |
|
1697 |
=end original |
1698 |
|
1699 |
L<perlxs/Thread-aware system interfaces> も参照してください。 |
1700 |
|
1701 |
=begin original |
1702 |
|
1703 |
Using C<-Dusethreads -Duseithreads> rather than C<-Dusemultiplicity> |
1704 |
is more appropriate if you intend to run multiple interpreters |
1705 |
concurrently in different threads, because it enables support for |
1706 |
linking in the thread libraries of your system with the interpreter. |
1707 |
|
1708 |
=end original |
1709 |
|
1710 |
もし、異なるスレッドで同時に複数のインタプリタを実行するつもりなら、 |
1711 |
C<-Dusemultiplicity> ではなく C<-Dusethreads -Duseithreads> がより適切です; |
1712 |
なぜならこれはインタプリタにシステムのスレッドライブラリを |
1713 |
リンクすることへの対応を有効にするからです。 |
1714 |
|
1715 |
=begin original |
1716 |
|
1717 |
Let's give it a try: |
1718 |
|
1719 |
=end original |
1720 |
|
1721 |
試してみましょう: |
1722 |
|
1723 |
#include <EXTERN.h> |
1724 |
#include <perl.h> |
1725 |
|
1726 |
/* we're going to embed two interpreters */ |
1727 |
|
1728 |
#define SAY_HELLO "-e", "print qq(Hi, I'm $^X\n)" |
1729 |
|
1730 |
int main(int argc, char **argv, char **env) |
1731 |
{ |
1732 |
PerlInterpreter *one_perl, *two_perl; |
1733 |
char *one_args[] = { "one_perl", SAY_HELLO, NULL }; |
1734 |
char *two_args[] = { "two_perl", SAY_HELLO, NULL }; |
1735 |
|
1736 |
PERL_SYS_INIT3(&argc,&argv,&env); |
1737 |
one_perl = perl_alloc(); |
1738 |
two_perl = perl_alloc(); |
1739 |
|
1740 |
PERL_SET_CONTEXT(one_perl); |
1741 |
perl_construct(one_perl); |
1742 |
PERL_SET_CONTEXT(two_perl); |
1743 |
perl_construct(two_perl); |
1744 |
|
1745 |
PERL_SET_CONTEXT(one_perl); |
1746 |
perl_parse(one_perl, NULL, 3, one_args, (char **)NULL); |
1747 |
PERL_SET_CONTEXT(two_perl); |
1748 |
perl_parse(two_perl, NULL, 3, two_args, (char **)NULL); |
1749 |
|
1750 |
PERL_SET_CONTEXT(one_perl); |
1751 |
perl_run(one_perl); |
1752 |
PERL_SET_CONTEXT(two_perl); |
1753 |
perl_run(two_perl); |
1754 |
|
1755 |
PERL_SET_CONTEXT(one_perl); |
1756 |
perl_destruct(one_perl); |
1757 |
PERL_SET_CONTEXT(two_perl); |
1758 |
perl_destruct(two_perl); |
1759 |
|
1760 |
PERL_SET_CONTEXT(one_perl); |
1761 |
perl_free(one_perl); |
1762 |
PERL_SET_CONTEXT(two_perl); |
1763 |
perl_free(two_perl); |
1764 |
PERL_SYS_TERM(); |
1765 |
exit(EXIT_SUCCESS); |
1766 |
} |
1767 |
|
1768 |
=begin original |
1769 |
|
1770 |
Note the calls to PERL_SET_CONTEXT(). These are necessary to initialize |
1771 |
the global state that tracks which interpreter is the "current" one on |
1772 |
the particular process or thread that may be running it. It should |
1773 |
always be used if you have more than one interpreter and are making |
1774 |
perl API calls on both interpreters in an interleaved fashion. |
1775 |
|
1776 |
=end original |
1777 |
|
1778 |
PERL_SET_CONTEXT() の呼び出しに注意してください。 |
1779 |
実行されている個々のプロセスやスレッドで、どのインタプリタが |
1780 |
「カレント」なのかを追跡するグローバルな状態を初期化するために必要です。 |
1781 |
もし複数のインタプリタがあって、両方のインタプリタに交互に perl API |
1782 |
呼び出しを行うなら、常にこれを行うべきです。 |
1783 |
|
1784 |
=begin original |
1785 |
|
1786 |
PERL_SET_CONTEXT(interp) should also be called whenever C<interp> is |
1787 |
used by a thread that did not create it (using either perl_alloc(), or |
1788 |
the more esoteric perl_clone()). |
1789 |
|
1790 |
=end original |
1791 |
|
1792 |
(perl_alloc() か、より難解な perl_clone() を使って、) C<interp> を |
1793 |
作ったのではないスレッドによって使われるときはいつでも、 |
1794 |
PERL_SET_CONTEXT(interp) も呼び出されるべきです。 |
1795 |
|
1796 |
=begin original |
1797 |
|
1798 |
Compile as usual: |
1799 |
|
1800 |
=end original |
1801 |
|
1802 |
いつもと同じようにコンパイルします: |
1803 |
|
1804 |
% cc -o multiplicity multiplicity.c \ |
1805 |
`perl -MExtUtils::Embed -e ccopts -e ldopts` |
1806 |
|
1807 |
=begin original |
1808 |
|
1809 |
Run it, Run it: |
1810 |
|
1811 |
=end original |
1812 |
|
1813 |
実行、実行: |
1814 |
|
1815 |
% multiplicity |
1816 |
Hi, I'm one_perl |
1817 |
Hi, I'm two_perl |
1818 |
|
1819 |
=head2 Using Perl modules, which themselves use C libraries, from your C |
1820 |
program |
1821 |
|
1822 |
(C プログラムから、C のライブラリを使っている Perl モジュールを使う) |
1823 |
|
1824 |
=begin original |
1825 |
|
1826 |
If you've played with the examples above and tried to embed a script |
1827 |
that I<use()>s a Perl module (such as I<Socket>) which itself uses a C or C++ |
1828 |
library, this probably happened: |
1829 |
|
1830 |
=end original |
1831 |
|
1832 |
あなたがこれまでにでてきた例を使っていて、(I<Socket> のような)そ |
1833 |
れ自身が C や C++ のライブラリを使っている Perl モジュールを |
1834 |
I<use()> しているスクリプトを埋め込もうとしているのであれば、こうなる |
1835 |
可能性があります。 |
1836 |
|
1837 |
Can't load module Socket, dynamic loading not available in this perl. |
1838 |
(You may need to build a new perl executable which either supports |
1839 |
dynamic loading or has the Socket module statically linked into it.) |
1840 |
|
1841 |
=begin original |
1842 |
|
1843 |
What's wrong? |
1844 |
|
1845 |
=end original |
1846 |
|
1847 |
なにがまずいのでしょう? |
1848 |
|
1849 |
=begin original |
1850 |
|
1851 |
Your interpreter doesn't know how to communicate with these extensions |
1852 |
on its own. A little glue will help. Up until now you've been |
1853 |
calling I<perl_parse()>, handing it NULL for the second argument: |
1854 |
|
1855 |
=end original |
1856 |
|
1857 |
あなたの使うインタプリタはこういったエクステンションと |
1858 |
どのように交信するのかを知りません。 |
1859 |
ちょっとした糊(glue)が助けになります。 |
1860 |
これまでは、I<perl_parse()> を呼び出すときにはその第二引数として |
1861 |
NULL を使っていました: |
1862 |
|
1863 |
perl_parse(my_perl, NULL, argc, my_argv, NULL); |
1864 |
|
1865 |
=begin original |
1866 |
|
1867 |
That's where the glue code can be inserted to create the initial contact |
1868 |
between Perl and linked C/C++ routines. Let's take a look some pieces of |
1869 |
I<perlmain.c> to see how Perl does this: |
1870 |
|
1871 |
=end original |
1872 |
|
1873 |
ここは Perl と、リンクされた C/C++ ルーチンとの間の初期化時の交信を |
1874 |
生成するために挿入することのできる糊コード(glue code)の場所です。 |
1875 |
Perl がどのようにこれを行っているのかを知るために、I<perlmain.c> の |
1876 |
一部を見てみましょう。 |
1877 |
|
1878 |
static void xs_init (pTHX); |
1879 |
|
1880 |
EXTERN_C void boot_DynaLoader (pTHX_ CV* cv); |
1881 |
EXTERN_C void boot_Socket (pTHX_ CV* cv); |
1882 |
|
1883 |
EXTERN_C void |
1884 |
xs_init(pTHX) |
1885 |
{ |
1886 |
char *file = __FILE__; |
1887 |
/* DynaLoader is a special case */ |
1888 |
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); |
1889 |
newXS("Socket::bootstrap", boot_Socket, file); |
1890 |
} |
1891 |
|
1892 |
=begin original |
1893 |
|
1894 |
Simply put: for each extension linked with your Perl executable |
1895 |
(determined during its initial configuration on your |
1896 |
computer or when adding a new extension), |
1897 |
a Perl subroutine is created to incorporate the extension's |
1898 |
routines. Normally, that subroutine is named |
1899 |
I<Module::bootstrap()> and is invoked when you say I<use Module>. In |
1900 |
turn, this hooks into an XSUB, I<boot_Module>, which creates a Perl |
1901 |
counterpart for each of the extension's XSUBs. Don't worry about this |
1902 |
part; leave that to the I<xsubpp> and extension authors. If your |
1903 |
extension is dynamically loaded, DynaLoader creates I<Module::bootstrap()> |
1904 |
for you on the fly. In fact, if you have a working DynaLoader then there |
1905 |
is rarely any need to link in any other extensions statically. |
1906 |
|
1907 |
=end original |
1908 |
|
1909 |
あなたの使う Perl の実行ファイルにリンクされているエクステンション毎に、 |
1910 |
そのエクステンションのルーチンを組み込むための Perl サブルーチンが |
1911 |
生成されます。 |
1912 |
通常はこういったサブルーチンは I<Module::bootstrap()> という名称で、 |
1913 |
あなたが I<use Module> としたときに起動されます。 |
1914 |
次にこれは、各エクステンションの XSUB に対する Perl の counterpart として |
1915 |
生成されるXSUB I<boot_Module> にフックされます。 |
1916 |
この部分に関して心配することはありません; I<xsubpp> と、エクステンションの |
1917 |
作者に任せましょう。 |
1918 |
あなたの使うエクステンションが動的ロードを必要とするならば、 |
1919 |
DynaLoader はあなたのためにその場で I<Module::bootstrap()> を生成します。 |
1920 |
実際のところ、あなたが DynaLoader を使っているのであれば、他の |
1921 |
エクステンションを静的にリンクする必要性はほとんどないでしょう。 |
1922 |
|
1923 |
=begin original |
1924 |
|
1925 |
Once you have this code, slap it into the second argument of I<perl_parse()>: |
1926 |
|
1927 |
=end original |
1928 |
|
1929 |
このコードを一度書いてしまえば、それを I<perl_parse()> の第二引数に |
1930 |
置けます: |
1931 |
|
1932 |
perl_parse(my_perl, xs_init, argc, my_argv, NULL); |
1933 |
|
1934 |
=begin original |
1935 |
|
1936 |
Then compile: |
1937 |
|
1938 |
=end original |
1939 |
|
1940 |
そしてコンパイルします: |
1941 |
|
1942 |
% cc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts` |
1943 |
|
1944 |
% interp |
1945 |
use Socket; |
1946 |
use SomeDynamicallyLoadedModule; |
1947 |
|
1948 |
print "Now I can use extensions!\n"' |
1949 |
|
1950 |
=begin original |
1951 |
|
1952 |
B<ExtUtils::Embed> can also automate writing the I<xs_init> glue code. |
1953 |
|
1954 |
=end original |
1955 |
|
1956 |
B<ExtUtils::Embed> も I<xs_init> 糊コードを書くことを |
1957 |
自動化できます。 |
1958 |
|
1959 |
% perl -MExtUtils::Embed -e xsinit -- -o perlxsi.c |
1960 |
% cc -c perlxsi.c `perl -MExtUtils::Embed -e ccopts` |
1961 |
% cc -c interp.c `perl -MExtUtils::Embed -e ccopts` |
1962 |
% cc -o interp perlxsi.o interp.o `perl -MExtUtils::Embed -e ldopts` |
1963 |
|
1964 |
=begin original |
1965 |
|
1966 |
Consult L<perlxs>, L<perlguts>, and L<perlapi> for more details. |
1967 |
|
1968 |
=end original |
1969 |
|
1970 |
詳しくは L<perlxs>, L<perlguts>, L<perlapi> を参照してください。 |
1971 |
|
1972 |
=head2 Using embedded Perl with POSIX locales |
1973 |
|
1974 |
(POSIX ロケールで組み込み Perl を使う) |
1975 |
|
1976 |
=begin original |
1977 |
|
1978 |
(See L<perllocale> for information about these.) |
1979 |
When a Perl interpreter normally starts up, it tells the system it wants |
1980 |
to use the system's default locale. This is often, but not necessarily, |
1981 |
the "C" or "POSIX" locale. Absent a S<C<"use locale">> within the perl |
1982 |
code, this mostly has no effect (but see L<perllocale/Not within the |
1983 |
scope of "use locale">). Also, there is not a problem if the |
1984 |
locale you want to use in your embedded perl is the same as the system |
1985 |
default. However, this doesn't work if you have set up and want to use |
1986 |
a locale that isn't the system default one. Starting in Perl v5.20, you |
1987 |
can tell the embedded Perl interpreter that the locale is already |
1988 |
properly set up, and to skip doing its own normal initialization. It |
1989 |
skips if the environment variable C<PERL_SKIP_LOCALE_INIT> is set (even |
1990 |
if set to 0 or C<"">). A perl that has this capability will define the |
1991 |
C pre-processor symbol C<HAS_SKIP_LOCALE_INIT>. This allows code that |
1992 |
has to work with multiple Perl versions to do some sort of work-around |
1993 |
when confronted with an earlier Perl. |
1994 |
|
1995 |
=end original |
1996 |
|
1997 |
(これらに関する情報については L<perllocale> を参照してください。) |
1998 |
Perl インタプリタが普通に開始したとき、システムのデフォルトのロケールを |
1999 |
使うということをシステムに伝えます。 |
2000 |
これはたいてい "C" または "POSIX" ロケールですが、そうである必要はありません。 |
2001 |
perl のコードに S<C<"use locale">> がなければ、これはほとんど何の影響も |
2002 |
ありません (しかし L<perllocale/Not within the |
2003 |
scope of "use locale"> を参照してください)。 |
2004 |
また、組み込み Perl で使いたいロケールがシステムのデフォルトと同じ場合は |
2005 |
問題になりません。 |
2006 |
しかし、システムのデフォルトではないロケールを設定して使いたい場合は |
2007 |
動作しません。 |
2008 |
Perl v5.20 から、ロケールは既に適切に設定されているので、通常の初期化を |
2009 |
飛ばすように組み込み Perl インタプリタに伝えられます。 |
2010 |
環境変数 C<PERL_SKIP_LOCALE_INIT> が設定されていれば (たとえ |
2011 |
0 または C<""> に設定されていても) 飛ばされます。 |
2012 |
この機能のある Perl は C プリプロセッサシンボル C<HAS_SKIP_LOCALE_INIT> が |
2013 |
設定されています。 |
2014 |
これにより、古い Perl に直面したときに、複数の Perl バージョンに対して何らかの |
2015 |
回避策をとれるようになります。 |
2016 |
|
2017 |
=begin original |
2018 |
|
2019 |
If your program is using the POSIX 2008 multi-thread locale |
2020 |
functionality, you should switch into the global locale and set that up |
2021 |
properly before starting the Perl interpreter. It will then properly |
2022 |
switch back to using the thread-safe functions. |
2023 |
|
2024 |
=end original |
2025 |
|
2026 |
あなたのプログラムが POSIX 2008 マルチスレッドロケール機能を使っている場合、 |
2027 |
Perl インタプリタを起動する前にグローバルロケールに切り替えて適切に |
2028 |
設定するべきです。 |
2029 |
それからスレッドセーフ関数を使うように適切に戻します。 |
2030 |
|
2031 |
=head1 Hiding Perl_ |
2032 |
|
2033 |
(Perl_ を隠す) |
2034 |
|
2035 |
=begin original |
2036 |
|
2037 |
If you completely hide the short forms of the Perl public API, |
2038 |
add -DPERL_NO_SHORT_NAMES to the compilation flags. This means that |
2039 |
for example instead of writing |
2040 |
|
2041 |
=end original |
2042 |
|
2043 |
もし Perl public API の短い形式を完全に隠したいなら、 |
2044 |
コンパイルオプションに -DPERL_NO_SHORT_NAMES を追加してください。 |
2045 |
これは、例えば以下のように書く代わりに: |
2046 |
|
2047 |
warn("%d bottles of beer on the wall", bottlecount); |
2048 |
|
2049 |
=begin original |
2050 |
|
2051 |
you will have to write the explicit full form |
2052 |
|
2053 |
=end original |
2054 |
|
2055 |
以下のように明示的に完全な形式で書く必要があります: |
2056 |
|
2057 |
Perl_warn(aTHX_ "%d bottles of beer on the wall", bottlecount); |
2058 |
|
2059 |
=begin original |
2060 |
|
2061 |
(See L<perlguts/"Background and PERL_IMPLICIT_CONTEXT"> for the explanation |
2062 |
of the C<aTHX_>. ) Hiding the short forms is very useful for avoiding |
2063 |
all sorts of nasty (C preprocessor or otherwise) conflicts with other |
2064 |
software packages (Perl defines about 2400 APIs with these short names, |
2065 |
take or leave few hundred, so there certainly is room for conflict.) |
2066 |
|
2067 |
=end original |
2068 |
|
2069 |
(C<aTHX_> の説明については |
2070 |
L<perlguts/"Background and PERL_IMPLICIT_CONTEXT"> を参照してください。) |
2071 |
短い形式を隠すことは、その他のソフトウェアパッケージとのあらゆる種類の |
2072 |
不快なもの(C プリプロセッサやその他のもの)との衝突を避けるためにとても |
2073 |
便利です。 |
2074 |
|
2075 |
=head1 MORAL |
2076 |
|
2077 |
=begin original |
2078 |
|
2079 |
You can sometimes I<write faster code> in C, but |
2080 |
you can always I<write code faster> in Perl. Because you can use |
2081 |
each from the other, combine them as you wish. |
2082 |
|
2083 |
=end original |
2084 |
|
2085 |
あなたは時々 C で B<より速いコードを> 書くことができるかもしれませんが、 |
2086 |
あなた常に Perl で B<コードをより早く> 書くことができるのです。 |
2087 |
これは片方でもう片方のものを作ることができ、望むままに |
2088 |
組み合わせることができるからです。 |
2089 |
|
2090 |
=head1 AUTHOR |
2091 |
|
2092 |
=begin original |
2093 |
|
2094 |
Jon Orwant <F<orwant@media.mit.edu>> and Doug MacEachern |
2095 |
<F<dougm@covalent.net>>, with small contributions from Tim Bunce, Tom |
2096 |
Christiansen, Guy Decoux, Hallvard Furuseth, Dov Grobgeld, and Ilya |
2097 |
Zakharevich. |
2098 |
|
2099 |
=end original |
2100 |
|
2101 |
Jon Orwant <F<orwant@media.mit.edu>> と Doug MacEachern |
2102 |
<F<dougm@covalent.net>>; さらに Tim Bunce, Tom Christiansen, |
2103 |
Guy Decoux, Hallvard Furuseth, Dov Grobgeld, Ilya Zakharevich からの |
2104 |
小さな貢献によります。 |
2105 |
|
2106 |
=begin original |
2107 |
|
2108 |
Doug MacEachern has an article on embedding in Volume 1, Issue 4 of |
2109 |
The Perl Journal ( L<http://www.tpj.com/> ). Doug is also the developer of the |
2110 |
most widely-used Perl embedding: the mod_perl system |
2111 |
(perl.apache.org), which embeds Perl in the Apache web server. |
2112 |
Oracle, Binary Evolution, ActiveState, and Ben Sugars's nsapi_perl |
2113 |
have used this model for Oracle, Netscape and Internet Information |
2114 |
Server Perl plugins. |
2115 |
|
2116 |
=end original |
2117 |
|
2118 |
Doug MacEachern は The Perl Journal ( L<http://www.tpj.com/> ) の |
2119 |
Volume 1, Issue 4 に組み込みの記事を書いています。 |
2120 |
Doug はまた、もっとも広く使われている Perl 組み込みの作者です: |
2121 |
mod_perl (perl.apache.org) は Apache ウェブサーバに Perl を組み込みます。 |
2122 |
Oracle, Binary Evolution, ActiveState, Ben Sugars の nsapi_perl は |
2123 |
このモデルを Oracle, Netscape, Internet Information Server の |
2124 |
Perl プラグインに使っています。 |
2125 |
|
2126 |
=head1 COPYRIGHT |
2127 |
|
2128 |
Copyright (C) 1995, 1996, 1997, 1998 Doug MacEachern and Jon Orwant. All |
2129 |
Rights Reserved. |
2130 |
|
2131 |
This document may be distributed under the same terms as Perl itself. |
2132 |
|
2133 |
=begin meta |
2134 |
|
2135 |
Translate: KIMURA Koichi (5.005) |
2136 |
Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.10.0-) |
2137 |
Status: completed |
2138 |
|
2139 |
=end meta |
2140 |
|