=encoding euc-jp =head1 NAME =begin original perlXStut - Tutorial for writing XSUBs =end original perlXStut - XSUB ��񤯤���Υ��塼�ȥꥢ�� =head1 DESCRIPTION =begin original This tutorial will educate the reader on the steps involved in creating a Perl extension. The reader is assumed to have access to L, L and L. =end original ���Υ��塼�ȥꥢ����ɼԤ�Perl�Υ������ƥ󥷥�����Τ�ɬ�פ� ���ƥåפ򶵤����ΤǤ��� �ɼԤ� L, L, L �˥��������Ǥ���Ȥ������Ȥ� ���ꤷ�Ƥ��ޤ��� =begin original This tutorial starts with very simple examples and becomes more complex, with each new example adding new features. Certain concepts may not be completely explained until later in the tutorial in order to slowly ease the reader into building extensions. =end original ���Υ��塼�ȥꥢ�������ñ����㤫��Ϥ�ơ���������˿ʤऴ�Ȥ� �����ʵ�ǽ���ղä������ʣ���ʤ�ΤؤȤʤ�ޤ��� ���Ĥ��Υ��󥻥ץȤϡ��ɼԤ��������ƥ󥷥����������Τ������� ��ñ�ˤʤ�褦�ˡ����塼�ȥꥢ��θ�Τۤ��ޤǴ����ˤ���������ޤ��� =begin original This tutorial was written from a Unix point of view. Where I know them to be otherwise different for other platforms (e.g. Win32), I will list them. If you find something that was missed, please let me know. =end original ���Υ��塼�ȥꥢ��� Unix �δ�������񤫤�Ƥ��ޤ��� (Win32 �Τ褦��)�ۤ��Υץ�åȥե�����Ǥϰۤʤ뤳�Ȥˤʤ���ΤäƤ��� ���Ǥϡ�����򵭤��Ƥ��ޤ��� ��������Ȥ���ȯ�������顢���Ҷ����Ƥ��������� =head1 SPECIAL NOTES (���̤�����) =head2 make =begin original This tutorial assumes that the make program that Perl is configured to use is called C. Instead of running "make" in the examples that follow, you may have to substitute whatever make program Perl has been configured to use. Running B should tell you what it is. =end original ���Υ��塼�ȥꥢ��Ǥϡ�Perl �����Ѥ���褦�����ꤵ��Ƥ��� make �ץ�����ब C �Ȥ���̾���Ǥ�������ꤷ�Ƥ��ޤ��� �ʲ������ "make" ��¹Ԥ�������ˡ�Perl ���Ȥ��褦�����ꤵ�줿¾�� make �ץ����������Ѥ���ɬ�פ����뤫�⤷��ޤ��� B �Ȥ���ȡ����줬�������狼��ޤ��� =head2 Version caveat (�С������˴ؤ���ٹ�) =begin original When writing a Perl extension for general consumption, one should expect that the extension will be used with versions of Perl different from the version available on your machine. Since you are reading this document, the version of Perl on your machine is probably 5.005 or later, but the users of your extension may have more ancient versions. =end original ���̸����� Perl �������ƥ󥷥���񤯤Ȥ������ʤ��Υޥ���� ���Ѳ�ǽ�ʥС������Ȱۤʤ�С������� Perl �ǥ������ƥ󥷥�� �Ȥ��뤳�Ȥ����ꤹ��٤��Ǥ��� ����ʸ����ɤ�Ǥ���Ȥ������Ȥϡ����ʤ��Υޥ���� Perl �ΥС������� �����餯 5.005 �ʹߤǤ��礦�����������ƥ󥷥��Υ桼�����Ϥ�äȸŤ� �С�����󤫤⤷��ޤ��� =begin original To understand what kinds of incompatibilities one may expect, and in the rare case that the version of Perl on your machine is older than this document, see the section on "Troubleshooting these Examples" for more information. =end original �ɤΤ褦����ߴ��������ꤵ��뤫�����򤹤뤿��ˡ��ޤ����������Ȥ��ơ� �ޥ�������äƤ��� Perl �ΥС�����󤬤��Υɥ�����Ȥ��Ť���硢 ����ʤ����� "Troubleshooting these Examples" �ξϤ򻲾Ȥ��Ƥ��������� =begin original If your extension uses some features of Perl which are not available on older releases of Perl, your users would appreciate an early meaningful warning. You would probably put this information into the F file, but nowadays installation of extensions may be performed automatically, guided by F module or other tools. =end original �⤷�������ƥ󥷥�󤬡�Perl �θŤ���꡼���Ǥ����ѤǤ��ʤ��褦�� Perl �� ��ǽ��ȤäƤ���ʤ顢�桼����������ΰ�̣�Τ���ٹ�����򤷤ޤ��� �����餯���ξ���� F �ե�������ɲä���٤��Ǥ��礦�����Ƕ�� �������ƥ󥷥��Υ��󥹥ȡ���ϡ�F �⥸�塼��䤽��¾�� �ġ���ˤ�äƼ�ưŪ�˹Ԥ��뤫�⤷��ޤ��� =begin original In MakeMaker-based installations, F provides the earliest opportunity to perform version checks. One can put something like this in F for this purpose: =end original MakeMaker �١����Υ��󥹥ȡ���Ǥϡ�F �����ץ�åȥե������ �С����������å��κǤ��ᤤ������󶡤��ޤ��� ������Ū�Τ���ˡ��ʲ��Τ褦�ʤ�Τ� F �˽񤱤ޤ�: eval { require 5.007 } or die < build them, but you must link the XSUBs subroutines with the rest of Perl, creating a new executable. This situation is similar to Perl 4. =end original ����Ū�ˤϡ������ƥब�饤�֥���ưŪ�˥����ɤ��뵡ǽ����äƤ��ʤ���� XSUB ��������뤳�ȤϤǤ��ʤ��ȹͤ����Ƥ��ޤ��� ���������������ޤ��� ���ʤ��� XSUB ���뤳�Ȥ� I<�Ǥ��ޤ�>�� �����������ʤ��Ϥ��� XSUB �Υ��֥롼����ȡ�Perl �Ȥ��󥯤��ƿ����� �¹ԥե��������ʤ���Фʤ�ޤ��� ���ξ����� perl4 ��Ʊ���Ǥ��� =begin original This tutorial can still be used on such a system. The XSUB build mechanism will check the system and build a dynamically-loadable library if possible, or else a static library and then, optionally, a new statically-linked executable with that static library linked in. =end original ���Υ��塼�ȥꥢ��Ϥޤ��������ä������ƥ��ȤäƤ��Ƥ�����פǤ��� XSUB �κ�����ǽ�ϡ������ƥ������å����Ʋ�ǽ�Ǥ����ưŪ�����ɲ�ǽ �饤�֥�����������Ǥ��ʤ���Х����ƥ��å��饤�֥���������Ƥ��� ���Υ����ƥ��å��饤�֥����󥯤��ƥ����ƥ��å���󥯤��줿 �¹ԥե������������ޤ�(�Ǹ����ʬ�ϥ��ץ����)�� =begin original Should you wish to build a statically-linked executable on a system which can dynamically load libraries, you may, in all the following examples, where the command "C" with no arguments is executed, run the command "C" instead. =end original ���줫������Ȥäơ�ưŪ�����ɲ�ǽ�饤�֥�꤬�Ȥ��륷���ƥ�� ���äƤ⥹���ƥ��å���󥯤��줿�¹ԥե����������������Ȥ�����硢 "C"�Ȥ��������ʤ��Υ��ޥ�ɤ�¹Ԥ�������ˡ� "C" �Ȥ������ޥ�ɤ�¹Ԥ��Ƥ��������� =begin original If you have generated such a statically-linked executable by choice, then instead of saying "C", you should say "C". On systems that cannot build dynamically-loadable libraries at all, simply saying "C" is sufficient. =end original �⤷�����ƥ��å���󥯤��줿�¹ԥե������������뤳�Ȥ�������Τʤ顢 "C" ������� "C" ��ȤäƤ��������� ưŪ�����ɲ�ǽ�饤�֥�꤬�����Ǥ��ʤ������ƥ�ξ��ˤ�ñ�� "C" �Ȥ�������� OK �Ǥ��� =head1 TUTORIAL (���塼�ȥꥢ��) =begin original Now let's go on with the show! =end original �Ǥϡ����Ƥ����ޤ��礦! =head2 EXAMPLE 1 (�� 1) =begin original Our first extension will be very simple. When we call the routine in the extension, it will print out a well-known message and return. =end original �ǽ�Υ������ƥ󥷥�������ñ��Ǥ��� �������ƥ󥷥�����ǥ롼�����ƤӽФ��Ȥ��ˡ��ɤ��Τ�줿��å������� ���Ϥ��ƥ꥿���󤷤ޤ��� =begin original Run "C". This creates a directory named Mytest, possibly under ext/ if that directory exists in the current working directory. Several files will be created under the Mytest dir, including MANIFEST, Makefile.PL, lib/Mytest.pm, Mytest.xs, t/Mytest.t, and Changes. =end original "C" ��¹Ԥ��ޤ��� ����� Mytest �Ȥ���̾���Υǥ��쥯�ȥ��������ޤ����������Ȥ� ��ȥǥ��쥯�ȥ�� ext/ �Ȥ����ǥ��쥯�ȥ꤬����Ф��β��˺������ޤ��� MANIFEST, Makefile.PL, lib/Mytest.pm, Mytest.xs, t/Mytest.t, Changes �� �ޤ᤿���Ĥ��Υե����뤬�ǥ��쥯�ȥ� Mytest �β��˺�������ޤ��� =begin original The MANIFEST file contains the names of all the files just created in the Mytest directory. =end original MANIFEST �Ȥ����ե�����ˤ� Mytest �ǥ��쥯�ȥ���������줿�ե����� ���٤ƤΥե�����̾������ޤ��� =begin original The file Makefile.PL should look something like this: =end original Makefile.PL �Ȥ����ե�����ϰʲ��Τ褦�ʷ����Ǥ���٤��Ǥ��� use ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( NAME => 'Mytest', VERSION_FROM => 'Mytest.pm', # finds $VERSION LIBS => [''], # e.g., '-lm' DEFINE => '', # e.g., '-DHAVE_SOMETHING' INC => '', # e.g., '-I/usr/include/other' ); =begin original The file Mytest.pm should start with something like this: =end original Mytest.pm �ϰʲ��Τ褦�����ƤǻϤޤ�ޤ��� package Mytest; use 5.008008; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( ); our $VERSION = '0.01'; require XSLoader; XSLoader::load('Mytest', $VERSION); # Preloaded methods go here. 1; __END__ # Below is the stub of documentation for your module. You better edit it! =begin original The rest of the .pm file contains sample code for providing documentation for the extension. =end original .pm �ե�����λĤ�ϥ������ƥ󥷥��Τ���Υɥ�����Ȥ���󶡤��뤿��� ����ץ륳���ɤ���ʤ�ޤ��� =begin original Finally, the Mytest.xs file should look something like this: =end original �Ǹ�ˡ�Mytest.xs �����Ƥϰʲ��Τ褦�ˤʤ�ޤ��� #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" MODULE = Mytest PACKAGE = Mytest =begin original Let's edit the .xs file by adding this to the end of the file: =end original .xs �ե�����ν����˰ʲ��ιԤ��ɲä��ޤ��� void hello() CODE: printf("Hello, world!\n"); =begin original It is okay for the lines starting at the "CODE:" line to not be indented. However, for readability purposes, it is suggested that you indent CODE: one level and the lines following one more level. =end original "CODE:" �ǻϤޤ�Ԥ�����ǥ�Ȥ���Ƥ��ʤ��Τ����ꤢ��ޤ��� ���������ɤߤ䤹���δ������顢CODE: �� 1 ��٥륤��ǥ�Ȥ��ơ� ����³���Ԥ򤵤�� 1 ��٥륤��ǥ�Ȥ��뤳�Ȥ򴫤�ޤ��� =begin original Now we'll run "C". This will create a real Makefile, which make needs. Its output looks something like: =end original �����ǡ�"C" ��¹Ԥ��ޤ��� ����� make ��ɬ�פȤ��������� Makefile ����������ޤ��� ���ν��Ϥϰʲ��Τ褦�ˤʤ�ޤ�: % perl Makefile.PL Checking if your kit is complete... Looks good Writing Makefile for Mytest % =begin original Now, running make will produce output that looks something like this (some long lines have been shortened for clarity and some extraneous lines have been deleted): =end original ������make��¹Ԥ���С��ʲ��Τ褦�ʽ��Ϥ���������ޤ� (������Ĺ���Ԥ����������ݤĤ����û������������;ʬ�ʹԤϺ�����Ƥ��ޤ�): % make cp lib/Mytest.pm blib/lib/Mytest.pm perl xsubpp -typemap typemap Mytest.xs > Mytest.xsc && mv Mytest.xsc Mytest.c Please specify prototyping behavior for Mytest.xs (see perlxs manual) cc -c Mytest.c Running Mkbootstrap for Mytest () chmod 644 Mytest.bs rm -f blib/arch/auto/Mytest/Mytest.so cc -shared -L/usr/local/lib Mytest.o -o blib/arch/auto/Mytest/Mytest.so \ \ chmod 755 blib/arch/auto/Mytest/Mytest.so cp Mytest.bs blib/arch/auto/Mytest/Mytest.bs chmod 644 blib/arch/auto/Mytest/Mytest.bs Manifying blib/man3/Mytest.3pm % =begin original You can safely ignore the line about "prototyping behavior" - it is explained in L. =end original "prototyping behavior" �˴ؤ���Ԥϰ�����̵��Ǥ��ޤ� - ����� L ����������Ƥ��ޤ��� =begin original If you are on a Win32 system, and the build process fails with linker errors for functions in the C library, check if your Perl is configured to use PerlCRT (running B should show you if this is the case). If Perl is configured to use PerlCRT, you have to make sure PerlCRT.lib is copied to the same location that msvcrt.lib lives in, so that the compiler can find it on its own. msvcrt.lib is usually found in the Visual C compiler's lib directory (e.g. C:/DevStudio/VC/lib). =end original Win32 �����ƥ�ξ��ǡ�C �饤�֥����δؿ��˴ؤ����󥫤Υ��顼�� �ӥ�ɽ��������Ԥ����硢Perl �� PerlCRT ��Ȥ��褦�����ꤵ��Ƥ��뤫�� Ĵ�٤Ƹ��Ƥ������� (���ξ�硢B ��¹Ԥ���Ф狼��Ϥ��Ǥ�)�� �⤷ Perl �� PerlCRT ��Ȥ��褦�����ꤵ��Ƥ���ʤ顢PerlCRT.lib �� msvcrt.lib ��Ʊ�����֤˥��ԡ����ơ�����ѥ��餬����򸫤Ĥ�����褦�� ����ɬ�פ�����ޤ��� msvcrt.lib ������ Visual C ����ѥ���� lib �ǥ��쥯�ȥ� (�㤨�� C:/DevStudio/VC/lib) �ˤ���ޤ��� =begin original Perl has its own special way of easily writing test scripts, but for this example only, we'll create our own test script. Create a file called hello that looks like this: =end original Perl �ˤϥƥ��ȥ�����ץȤ��ñ�˽񤯤�����ȼ������̤���ˡ������ޤ����� ������Τ�������ˡ���ʬ�ǥƥ��ȥ�����ץȤ���ޤ��� hello �Ȥ���̾���Ρ��ʲ��Τ褦�����ƤΥե��������ޤ��� #! /opt/perl5/bin/perl use ExtUtils::testlib; use Mytest; Mytest::hello(); =begin original Now we make the script executable (C), run the script and we should see the following output: =end original �����ǥ�����ץȤ�¹Բ�ǽ�ˤ���(C)����¹Ԥ���С� �ʲ��Τ褦�ʽ��Ϥˤʤ�Ϥ��Ǥ��� % ./hello Hello, world! % =head2 EXAMPLE 2 (�� 2) =begin original Now let's add to our extension a subroutine that will take a single numeric argument as input and return 0 if the number is even or 1 if the number is odd. =end original ���٤ϡ����Ͱ��������ϤȤ��ư�ļ�ꡢ���ο��ͤ������ʤ� 1 �򡢴���ʤ� 0 ���֤��褦�ʥ��֥롼������ɲä��ޤ��礦�� =begin original Add the following to the end of Mytest.xs: =end original �ʲ��ιԤ� Mytest.xs ���������ɲä��ޤ�: int is_even(input) int input CODE: RETVAL = (input % 2 == 0); OUTPUT: RETVAL =begin original There does not need to be whitespace at the start of the "C" line, but it is useful for improving readability. Placing a semi-colon at the end of that line is also optional. Any amount and kind of whitespace may be placed between the "C" and "C". =end original "C" �ιԤλϤ�ˤ�������ɬ�ܤǤϤ���ޤ��󤬡����줬����� �ɤߤ䤹���������ޤ��� Ʊ�ͤˡ������Υ��ߥ�������ά��ǽ�Ǥ��� Ǥ�դζ���� "C" �� "C" �δ֤��֤����Ȥ��Ǥ��ޤ��� =begin original Now re-run make to rebuild our new shared library. =end original make ����ټ¹Ԥ��ơ������ʶ�ͭ�饤�֥�����ޤ��� =begin original Now perform the same steps as before, generating a Makefile from the Makefile.PL file, and running make. =end original ���Υ��ƥåפ�Ʊ���褦�ˡ�Makefile.PL ���� Makefile ���ä� make ��¹Ԥ��ޤ��� =begin original In order to test that our extension works, we now need to look at the file Mytest.t. This file is set up to imitate the same kind of testing structure that Perl itself has. Within the test script, you perform a number of tests to confirm the behavior of the extension, printing "ok" when the test is correct, "not ok" when it is not. =end original ���������������ƥ󥷥��ư��뤫���ǧ���뤿��ˡ�Mytest.t �� �褦�ʥե����뤬ɬ�פȤʤ�ޤ��� ���Υե������ Perl ���Ȥ����äƤ��빽¤������å�����Τ�Ʊ�ͤΤ�Τ� ���路�ƺ��ޤ��� �ƥ��ȥ�����ץȤ���Ǥϡ��������ƥ󥷥���ư����ǧ���뤿������� �ƥ��Ȥ��֤���������ư��򤷤��� "ok" ���������ʤ���� "not ok" �� ���Ϥ���褦�ˤ��ޤ��� BEGIN �֥��å��ˤ���ʸ�� print "1..4" ���Ѥ����ե�����������˰ʲ��ιԤ� �ɲä��ޤ��� use Test::More tests => 4; BEGIN { use_ok('Mytest') }; ######################### # Insert your test code below, the Test::More module is use()ed here so read # its man page ( perldoc Test::More ) for help writing this test script. is(&Mytest::is_even(0), 1); is(&Mytest::is_even(1), 0); is(&Mytest::is_even(2), 1); =begin original We will be calling the test script through the command "C". You should see output that looks something like this: =end original "C" �Ȥ������ޥ�ɤǥƥ��ȥ�����ץȤ�ƤӽФ��ޤ��� �ʲ��Τ褦�ʽ��Ϥ��Ф�Ϥ��Ǥ��� %make test PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t t/Mytest....ok All tests successful. Files=1, Tests=4, 0 wallclock secs ( 0.03 cusr + 0.00 csys = 0.03 CPU) % =head2 What has gone on? (���򤷤���?) =begin original The program h2xs is the starting point for creating extensions. In later examples we'll see how we can use h2xs to read header files and generate templates to connect to C routines. =end original �ץ������ h2xs �ϥ������ƥ󥷥�����������ȯ���Ȥʤ�ޤ��� �����Ǥϡ��إå��ե�������ɤ߹���� C �Υ롼������Ф��� �����Υƥ�ץ졼�Ȥ���������Τ� h2xs ��ɤΤ褦�˻Ȥ����򼨤��ޤ��� =begin original h2xs creates a number of files in the extension directory. The file Makefile.PL is a perl script which will generate a true Makefile to build the extension. We'll take a closer look at it later. =end original h2xs �ϥ������ƥ󥷥��Υǥ��쥯�ȥ�˿�¿���Υե������������ޤ��� Makefile.PL �Ȥ����ե�����ϥ������ƥ󥷥���������뤿��� ������ Makefile ���������� perl ������ץȤǤ��� �ܺ٤ϸ�Ҥ��ޤ��� =begin original The .pm and .xs files contain the meat of the extension. The .xs file holds the C routines that make up the extension. The .pm file contains routines that tell Perl how to load your extension. =end original .pm �� .xs �Ȥ��ä��ե�����ϥ������ƥ󥷥������Τ�ޤߤޤ��� .xs �ե�����ˤϥ������ƥ󥷥����뤿��� C �Υ롼���󤬤���ޤ��� .pm �ե�����ˤ� Perl ���ɤΤ褦�ˤ��ʤ��κ�ä��������ƥ󥷥��� �����ɤ��뤫��ؼ�����롼���󤬤���ޤ��� =begin original Generating the Makefile and running C created a directory called blib (which stands for "build library") in the current working directory. This directory will contain the shared library that we will build. Once we have tested it, we can install it into its final location. =end original Makefile ����������C ��¹Ԥ��뤳�Ȥǥ����Ȥκ�ȥǥ��쥯�ȥ�� ���� blib ( "build library" ���̣���ޤ�)�ȸƤФ��ǥ��쥯�ȥ꤬ ����ޤ��� ���Υǥ��쥯�ȥ�ˤϡ��䤿�����������붦ͭ�饤�֥�꤬�֤���ޤ��� ���٤����ƥ��Ȥ���С��ǽ�Ū�ʾ��˥��󥹥ȡ��뤹�뤳�Ȥ��Ǥ��ޤ��� =begin original Invoking the test script via "C" did something very important. It invoked perl with all those C<-I> arguments so that it could find the various files that are part of the extension. It is I important that while you are still testing extensions that you use "C". If you try to run the test script all by itself, you will get a fatal error. Another reason it is important to use "C" to run your test script is that if you are testing an upgrade to an already-existing version, using "C" ensures that you will test your new extension, not the already-existing version. =end original "C" ��ͳ�ǥƥ��ȥ�����ץȤ�¹Ԥ��뤳�Ȥˤ�äơ����˽��פ� ���Ĥ��Τ��Ȥ�Ԥ��ޤ��� ����� perl �� C<-I> �������դ��Ƶ�ư��������ˤ�ꥨ�����ƥ󥷥��� �����Ǥ����͡��ʥե�����򸫤Ĥ��뤳�Ȥ��Ǥ���褦�ˤ��ޤ��� �������ƥ󥷥���ƥ��Ȥ���Τ� "make test" ��Ȥ��Τ� I<����> ���פǤ��� �⤷���ƥ��ȥ�����ץȤ򤽤Τޤ޼¹Ԥ��Ƥ��ޤä��顢��̿Ū�ʥ��顼�� ȯ������Ǥ��礦�� �ƥ��ȥ�����ץȤ�¹Ԥ���Τ� "C" ��Ȥ��⤦��Ĥν���� ��ͳ�ϡ����ˤ��륨�����ƥ󥷥��򥢥åץ��졼�ɤ�����Τ� �ƥ��Ȥ��褦�Ȥ����Ȥ��ˡ�"C" ��ȤäƤ���С����� �����ΤǤϤʤ�����������Τ�ƥ��Ȥ��뤳�Ȥ��ݾڤ���뤿��Ǥ��� =begin original When Perl sees a C, it searches for a file with the same name as the C'd extension that has a .pm suffix. If that file cannot be found, Perl dies with a fatal error. The default search path is contained in the C<@INC> array. =end original Perl �� C �Ȥ����Ԥ򸫤Ĥ����Ȥ���C ���� �������ƥ󥷥���Ʊ��̾���� .pm �Ȥ�����ĥ����ĥե�����򸡺����ޤ��� �⤷�������ä��ե����뤬���Ĥ���ʤ���С�Perl ����̿Ū���顼�ˤ�� ��λ���ޤ��� �ǥե���Ȥθ����ѥ��� C<@INC> �Ȥ���������֤���ޤ��� =begin original In our case, Mytest.pm tells perl that it will need the Exporter and Dynamic Loader extensions. It then sets the C<@ISA> and C<@EXPORT> arrays and the C<$VERSION> scalar; finally it tells perl to bootstrap the module. Perl will call its dynamic loader routine (if there is one) and load the shared library. =end original Mytest.pm �� perl �� Exporter �������ƥ󥷥�� �� Dynamic Loader �������ƥ󥷥��ɬ�פǤ��뤤�����Ȥ򶵤��ޤ��� ���θ����� C<@ISA>, C<@EXPORT> �ȥ����顼 C<$VERSION> ���ͤ����ꤷ���Ǹ�� �⥸�塼���֡��ȥ��ȥ�åפ���褦�� perl �˻ؼ����ޤ��� perl �Ϥ��Υ����ʥߥå����������롼�����(�����)�ƤӽФ�����ͭ�饤�֥��� �����ɤ��ޤ��� =begin original The two arrays C<@ISA> and C<@EXPORT> are very important. The C<@ISA> array contains a list of other packages in which to search for methods (or subroutines) that do not exist in the current package. This is usually only important for object-oriented extensions (which we will talk about much later), and so usually doesn't need to be modified. =end original C<@ISA> �� C<@EXPORT> ����Ĥ���������˽��פǤ��� ���� C<@ISA> �ˤϡ��᥽�å�(�⤷���ϥ��֥롼����)�������ȥѥå������� �ʤ��ä��Ȥ���õ���˹Ԥ�¾�Υѥå������Υꥹ�Ȥ�����ޤ��� ��������̥��֥������Ȼظ��������ƥ󥷥��(����ˤĤ��ƤϤ��äȸ������ �������ޤ�)�ǤΤ߽��פʤΤǡ����̤Ͻ�������ɬ�פϤ���ޤ��� =begin original The C<@EXPORT> array tells Perl which of the extension's variables and subroutines should be placed into the calling package's namespace. Because you don't know if the user has already used your variable and subroutine names, it's vitally important to carefully select what to export. Do I export method or variable names I without a good reason. =end original ���� C<@EXPORT> �� Perl ���Ф��ơ��ƤӽФ��줿�Ȥ��˥ѥå�������̾�����֤� �֤��٤��������ƥ󥷥����ѿ��ȥ��֥롼�����ؼ����ޤ��� �桼���������ˤ��ʤ����ѿ�̾�䥵�֥롼����̾��ȤäƤ��뤫�ɤ����� �狼��ʤ��Τǡ����򥨥����ݡ��Ȥ��뤫�򿵽Ť����򤹤뤳�Ȥ϶ˤ�� ���פǤ��� ������������٤���ͳ���ʤ��¤�ϡ��᥽�å�̾���ѿ�̾�� I<�ǥե���ȤǤ�> �������ݡ��� I<���ʤ�> �褦�ˤ��Ƥ��������� =begin original As a general rule, if the module is trying to be object-oriented then don't export anything. If it's just a collection of functions and variables, then you can export them via another array, called C<@EXPORT_OK>. This array does not automatically place its subroutine and variable names into the namespace unless the user specifically requests that this be done. =end original ����Ū�ʵ�§�Ȥ��ơ��⥸�塼�뤬���֥������Ȼظ����褦�Ȥ������ˤ� ���⥨�����ݡ��Ȥ��ޤ��� �⥸�塼�뤬ñ�ʤ�ؿ����ѿ��ν��ޤ�Ǥ���С��̤����� @EXPORT_OK �� �̤��Ƥ����򥨥����ݡ��ȤǤ��ޤ��� ��������ϡ��桼�������������Ƥۤ�������Ū�˻��ꤷ�ʤ��¤ꡢ ��ưŪ�ˤϥ��֥롼����̾���ѿ�̾��̾�����֤��֤��ޤ��� =begin original See L for more information. =end original �ܺ٤� L �򻲾Ȥ��Ƥ��������� =begin original The C<$VERSION> variable is used to ensure that the .pm file and the shared library are "in sync" with each other. Any time you make changes to the .pm or .xs files, you should increment the value of this variable. =end original �ѿ� C<$VERSION> �� .pm �ե�����ȶ�ͭ�饤�֥�꤬���줾�� ��Ʊ�����Ƥ���פ��Ȥ��ݾڤ��ޤ��� .pm �ե������ .xs �ե�������ѹ������Ȥ��������ѿ����ͤ� ���ä�����٤��Ǥ��� =head2 Writing good test scripts (�ɤ��ƥ��ȥ�����ץȤ��) =begin original The importance of writing good test scripts cannot be over-emphasized. You should closely follow the "ok/not ok" style that Perl itself uses, so that it is very easy and unambiguous to determine the outcome of each test case. When you find and fix a bug, make sure you add a test case for it. =end original �ɤ��ƥ��ȥ�����ץȤ�񤯤��Ȥν������ϡ������鶯Ĵ���Ƥ�­��ޤ��� Perl ���Ȥ��ȤäƤ��� "ok/not ok" �η�������¤˼�ꡢ�ƥ��ȥ������� �ɤ��ʤä������ñ�ˡ�ۣ��ʤ��Ȥʤ��狼��褦�ˤ��ޤ��� �Х���ȯ�����ƽ��������顢�ƥ��ȥ������ˤ�����ɲä��ޤ��礦�� =begin original By running "C", you ensure that your Mytest.t script runs and uses the correct version of your extension. If you have many test cases, save your test files in the "t" directory and use the suffix ".t". When you run "C", all of these test files will be executed. =end original "C" ��¹Ԥ��뤳�Ȥˤ�ꡢ������������ץ� Mytest.t ��¹Ԥ��� Ŭ�ڤʥС������Υ������ƥ󥷥���Ȥ����Ȥ��ݾڤ���ޤ��� ��������Υƥ��ȥ�����������Τʤ顢 "t" �Ȥ���̾���Υǥ��쥯�ȥ�ˡ�".t" �γ�ĥ�Ҥǥƥ��ȥե��������¸���ޤ��� "C" ��¹Ԥ���ȡ������Υƥ��ȥե��������Ƥ��¹Ԥ���ޤ��� =head2 EXAMPLE 3 (�� 3) =begin original Our third extension will take one argument as its input, round off that value, and set the I to the rounded value. =end original �����ܤ���ϡ����ϤȤ��ư������ĤȤäƤ����ͤ�ݤ�Ƥ��餽�η�̤� �����˥��åȤ���Ȥ�����ΤǤ��� =begin original Add the following to the end of Mytest.xs: =end original �ʲ��ιԤ� Mytest.xs ���������ɲä��ޤ�: void round(arg) double arg CODE: if (arg > 0.0) { arg = floor(arg + 0.5); } else if (arg < 0.0) { arg = ceil(arg - 0.5); } else { arg = 0.0; } OUTPUT: arg =begin original Edit the Makefile.PL file so that the corresponding line looks like this: =end original Makefile.pl �Ȥ����ե�������Խ������б�����Ԥ�ʲ��˼����褦�� ���Ƥ��������� 'LIBS' => ['-lm'], # e.g., '-lm' =begin original Generate the Makefile and run make. Change the test number in Mytest.t to "9" and add the following tests: =end original Makefile ���������Ƥ��� make ��¹Ԥ��ޤ��� Mytest.t �Υƥ����ֹ�� "9" ���ѹ���������˰ʲ��Υƥ��Ȥ��ɲä��ޤ��� $i = -1.5; &Mytest::round($i); is( $i, -2.0 ); $i = -1.1; &Mytest::round($i); is( $i, -1.0 ); $i = 0.0; &Mytest::round($i); is( $i, 0.0 ); $i = 0.5; &Mytest::round($i); is( $i, 1.0 ); $i = 1.2; &Mytest::round($i); is( $i, 1.0 ); =begin original Running "C" should now print out that all nine tests are okay. =end original "C" ��¹Ԥ��ޤ��� �����ǡ���ĤΥƥ��Ȥ��٤Ƥ� ok �Ƚ��Ϥ����Ϥ��Ǥ��� =begin original Notice that in these new test cases, the argument passed to round was a scalar variable. You might be wondering if you can round a constant or literal. To see what happens, temporarily add the following line to Mytest.t: =end original �����ο������ƥ��ȥ������Ǥϡ��ݤ�뤿����Ϥ��������� �������ѿ��Ǥ��뤳�Ȥ����ܤ��Ƥ��������� ������ƥ���ݤ᤿��ɤ��ʤ�����������Ȥ����������ä����⤷��ޤ��� ���������뤫��Τ���뤿��ˡ��ʲ��ιԤ� Mytest.t �˰��Ū�� �ɲä��Ƥ��������� &Mytest::round(3); =begin original Run "C" and notice that Perl dies with a fatal error. Perl won't let you change the value of constants! =end original "C" ��¹Ԥ���ȡ�Perl ����̿Ū���顼�򵯤����� ��λ���Ƥ��ޤ��ޤ��� Perl �ϡ�����ͤ��ѹ������ʤ��ΤǤ�! =head2 What's new here? (������������?) =over 4 =item * =begin original We've made some changes to Makefile.PL. In this case, we've specified an extra library to be linked into the extension's shared library, the math library libm in this case. We'll talk later about how to write XSUBs that can call every routine in a library. =end original Makefile.pl ���Ф����ѹ��򤷤ޤ����� ���ξ�硢�������ƥ󥷥��ζ�ͭ�饤�֥��ȥ�󥯤��٤��ɲäΥ饤�֥�� (����ξ��Ͽ��إ饤�֥�� libm)����ꤷ�ޤ��� ��ǡ��饤�֥��ˤ��뤹�٤ƤΥ롼�����ƤӽФ����ȤΤǤ��� XSUB �� �����ˤĤ����������ޤ��� =item * =begin original The value of the function is not being passed back as the function's return value, but by changing the value of the variable that was passed into the function. You might have guessed that when you saw that the return value of round is of type "void". =end original �ؿ����ͤ��ؿ�������ͤȤ����֤����ΤǤϤʤ����ؿ����Ϥ��줿 �ѿ����ͤ��ѹ����뤳�Ȥ��֤���ޤ��� round ���֤��ͤη��� "void" �Ǥ���ȿ�¬�������⤷��ޤ��� =back =head2 Input and Output Parameters (���ϥѥ�᡼���Ƚ��ϥѥ�᡼��) =begin original You specify the parameters that will be passed into the XSUB on the line(s) after you declare the function's return value and name. Each input parameter line starts with optional whitespace, and may have an optional terminating semicolon. =end original �ؿ�������ͤ�̾�������������ιԤ� XSUB ���Ϥ��ѥ�᡼�������Ǥ��ޤ��� �ƥѥ�᡼���Ԥ�(��ά��ǽ��)����ǻϤޤꡢ(��ά��ǽ��)��ü���� ���ߥ����󤬤���ޤ��� =begin original The list of output parameters occurs at the very end of the function, just after the OUTPUT: directive. The use of RETVAL tells Perl that you wish to send this value back as the return value of the XSUB function. In Example 3, we wanted the "return value" placed in the original variable which we passed in, so we listed it (and not RETVAL) in the OUTPUT: section. =end original ���ϥѥ�᡼���ϴؿ��κǸ塢OUTPUT: �ؼ��Ҥ�ľ����֤��ޤ��� RETVAL ��Ȥ����Ȥǡ�Perl �� XSUB �ؿ�������ͤ��֤������Ȥ����ջ֤� �����ޤ��� �� 3 �Ǥϡ����֤��͡פ�䤿�����Ϥ��������ѿ�������Ƥۤ����Τǡ� (RETVAL �Ǥʤ�) �����ѿ��� OUTPUT: �����������¤٤��ΤǤ��� =head2 The XSUBPP Program (XSUBPP �ץ������) =begin original The B program takes the XS code in the .xs file and translates it into C code, placing it in a file whose suffix is .c. The C code created makes heavy use of the C functions within Perl. =end original B �ץ������� .xs �ե�����ˤ��� XS �����ɤ��ꡢ����� C �� ���������η�̤� .c �Ȥ�����ĥ�ҤΥե�����˽��Ϥ��ޤ��� �Ѵ����줿 C �����ɤ� Perl ����ˤ��� C �δؿ���Ȥ��褦�˺���ޤ��� =head2 The TYPEMAP file (TYPEMAP �ե�����) =begin original The B program uses rules to convert from Perl's data types (scalar, array, etc.) to C's data types (int, char, etc.). These rules are stored in the typemap file ($PERLLIB/ExtUtils/typemap). This file is split into three parts. =end original B �ץ������� Perl �Υǡ�����(�����顼�����󡢤ʤ�)���� C �Υǡ�����(int, char, �ʤ�)���Ѵ����뵬§��Ȥ��ޤ��� �����Υ롼��� typemap �ե����� ($PERLLIB/ExtUtils/typemap)�� ��Ǽ����ޤ��� ���Υե�����ϻ��Ĥ���ʬ��ʬ�����ޤ��� =begin original The first section maps various C data types to a name, which corresponds somewhat with the various Perl types. The second section contains C code which B uses to handle input parameters. The third section contains C code which B uses to handle output parameters. =end original �ǽ�Υ����������͡��� C �Υǡ��������͡��� Perl �η��ˤ����餫�б����� ̾���˥ޥåԥ󥰤����ΤǤ��� �����ܤΥ������������ϥѥ�᡼���򰷤������ B �����Ѥ��� C �����ɤ���ʤ�ޤ��� �����ܤΥ��������Ͻ��ϥѥ�᡼�����Ф��� B �����Ѥ��� C �����ɤ���ʤ�ޤ��� =begin original Let's take a look at a portion of the .c file created for our extension. The file name is Mytest.c: =end original �������䤿���Υ������ƥ󥷥��Τ���˺��줿 .c �ե�����ΰ����� ���Ƥߤޤ��礦�� �ե�����̾�� Mytest.c �Ǥ�: XS(XS_Mytest_round) { dXSARGS; if (items != 1) Perl_croak(aTHX_ "Usage: Mytest::round(arg)"); PERL_UNUSED_VAR(cv); /* -W */ { double arg = (double)SvNV(ST(0)); /* XXXXX */ if (arg > 0.0) { arg = floor(arg + 0.5); } else if (arg < 0.0) { arg = ceil(arg - 0.5); } else { arg = 0.0; } sv_setnv(ST(0), (double)arg); /* XXXXX */ SvSETMAGIC(ST(0)); } XSRETURN_EMPTY; } =begin original Notice the two lines commented with "XXXXX". If you check the first section of the typemap file, you'll see that doubles are of type T_DOUBLE. In the INPUT section, an argument that is T_DOUBLE is assigned to the variable arg by calling the routine SvNV on something, then casting it to double, then assigned to the variable arg. Similarly, in the OUTPUT section, once arg has its final value, it is passed to the sv_setnv function to be passed back to the calling subroutine. These two functions are explained in L; we'll talk more later about what that "ST(0)" means in the section on the argument stack. =end original "XXXXX" �Ȥ��������Ȥ��դ���줿��ĤιԤ����դ��Ƥ��������� typemap �ե�����κǽ�Υ���������Τ����С�doublesT_DOUBLE �Ȥ������� ���뤳�Ȥ��狼��Ǥ��礦�� INPUT ���������Ǥ� T_DOUBLE �Ǥ�������ϡ�SvNv �Ȥ����롼����� �ƤӽФ����Ȥˤ�ä��ѿ���������Ƥ�졢���θ�� double �� ���㥹�Ȥ���Ƥ�������Ǥ����ѿ�����������ޤ��� Ʊ�ͤˡ�OUTPUT ���������Ǥϰ��ٰ������ǽ�Ū���ͤ���ƤС������ �ƤӽФ��줿�ؿ����֤�����˴ؿ� sv_setnv ���Ϥ���ޤ��� �������Ĥδؿ��� L ����������Ƥ��ޤ��� ���������å��ˤ��륻�������� "ST(0)" �ΰ�̣�ϸ���������ޤ��� =head2 Warning about Output Arguments (���ϰ����˴ؤ���ٹ�) =begin original In general, it's not a good idea to write extensions that modify their input parameters, as in Example 3. Instead, you should probably return multiple values in an array and let the caller handle them (we'll do this in a later example). However, in order to better accommodate calling pre-existing C routines, which often do modify their input parameters, this behavior is tolerated. =end original ����Ū�ˤ��äơ��� 3 �ˤ���褦�����ϰ�����񤭴�����褦�� �������ƥ󥷥����뤳�ȤϤ褯����ޤ��� ����ˡ������餯��ʣ�����ͤ�������֤��ơ��ƤӽФ����ˤ���򰷤碌��褦�� ����٤��Ǥ�(����ϸ����ǹԤ��ޤ�)�� �������ʤ��顢���Ǥˤ������ϥѥ�᡼����񤭴�����褦�� C �Υ롼����� �ƤӽФ��ˤ���ɤ�Ŭ�����뤿��ˡ����ο����񤤤�������Ƥ���ΤǤ��� ������Ǥϡ������ɤ��Ԥ������������ޤ��� =head2 EXAMPLE 4 (�� 4) =begin original In this example, we'll now begin to write XSUBs that will interact with pre-defined C libraries. To begin with, we will build a small library of our own, then let h2xs write our .pm and .xs files for us. =end original ������ǡ����Ǥˤ��� C �饤�֥��Ȥ��Ȥꤹ��褦�� XSUB �ε��Ҥ� �Ϥ�ޤ��� �����Ϥ�뤿��ˡ��ȼ��ξ����ʥ饤�֥�����ޤ��� ���줫�� .pm �� .xs �Τ���� h2xs �򵭽Ҥ��ޤ��� =begin original Create a new directory called Mytest2 at the same level as the directory Mytest. In the Mytest2 directory, create another directory called mylib, and cd into that directory. =end original �ǥ��쥯�ȥ� Mytest ��Ʊ����٥�ˡ�Mytest2 �ǥ��쥯�ȥ�򿷤��� ���ޤ��� Mytest2 �ǥ��쥯�ȥ�ǡ�mylib �Ȥ����̤Υǥ��쥯�ȥ��������������� cd ���ޤ��� =begin original Here we'll create some files that will generate a test library. These will include a C source file and a header file. We'll also create a Makefile.PL in this directory. Then we'll make sure that running make at the Mytest2 level will automatically run this Makefile.PL file and the resulting Makefile. =end original �����ǡ��ƥ����ѤΥ饤�֥����������뤿��δ��Ĥ��Υե��������ޤ��� �������ä��ե�����ˤϡ�C �������ե�����ȥإå����ե����뤬�ޤޤ�ޤ��� �ޤ������Υǥ��쥯�ȥ�� Makefile.PL ����ޤ��� ���θ�ǡ�Mytest2 �Υ�٥�� make ��¹Ԥ��뤳�Ȥˤ�꼫ưŪ�� Makefile.PL ���¹Ԥ��졢���η�� Makefile ����������ޤ��� =begin original In the mylib directory, create a file mylib.h that looks like this: =end original �ǥ��쥯�ȥ� mylib �ǡ��ʲ��Τ褦�����Ƥ� mylib.h �Ȥ����ե������ �������ޤ��� #define TESTVAL 4 extern double foo(int, long, const char*); =begin original Also create a file mylib.c that looks like this: =end original #include #include "./mylib.h" #include #include "./mylib.h" double foo(int a, long b, const char *c) { return (a + b + atof(c) + TESTVAL); } =begin original And finally create a file Makefile.PL that looks like this: =end original use ExtUtils::MakeMaker; $Verbose = 1; WriteMakefile( NAME => 'Mytest2::mylib', SKIP => [qw(all static static_lib dynamic dynamic_lib)], clean => {'FILES' => 'libmylib$(LIB_EXT)'}, ); use ExtUtils::MakeMaker; $Verbose = 1; WriteMakefile( NAME => 'Mytest2::mylib', SKIP => [qw(all static static_lib dynamic dynamic_lib)], clean => {'FILES' => 'libmylib$(LIB_EXT)'}, ); sub MY::top_targets { ' all :: static pure_all :: static static :: libmylib$(LIB_EXT) libmylib$(LIB_EXT): $(O_FILES) $(AR) cr libmylib$(LIB_EXT) $(O_FILES) $(RANLIB) libmylib$(LIB_EXT) '; } =begin original Make sure you use a tab and not spaces on the lines beginning with "$(AR)" and "$(RANLIB)". Make will not function properly if you use spaces. It has also been reported that the "cr" argument to $(AR) is unnecessary on Win32 systems. =end original "$(AR)" �� "$(RANLIB)" �ǻϤޤ�ԤǤ϶���ǤϤʤ����֤�ȤäƤ��뤳�Ȥ� ��ǧ���Ƥ��������� �ޤ���Win32 �����ƥ�Ǥϡ�$(AR) �� "cr" ���������פǤ��뤳�Ȥ� ��𤵤�Ƥ��ޤ��� =begin original We will now create the main top-level Mytest2 files. Change to the directory above Mytest2 and run the following command: =end original �����ǥȥåץ�٥�� Mytest2 �Ȥ����ե������������ޤ��� Mytest2 �ǥ��쥯�ȥ� ���ѹ����ơ��ʲ��Υ��ޥ�ɤ�¹Ԥ��ޤ��� % h2xs -O -n Mytest2 ./Mytest2/mylib/mylib.h =begin original This will print out a warning about overwriting Mytest2, but that's okay. Our files are stored in Mytest2/mylib, and will be untouched. =end original ����ˤ�ꡢMytest2 ���񤭤���Ȥ����ٹ𤬽Фޤ��������ˤ��뤳�Ȥ� ����ޤ��� �䤿���Υե������ Mytest2/mylib �ˤ���ޤ����������ޤ��� =begin original The normal Makefile.PL that h2xs generates doesn't know about the mylib directory. We need to tell it that there is a subdirectory and that we will be generating a library in it. Let's add the argument MYEXTLIB to the WriteMakefile call so that it looks like this: =end original h2xs �����������̾�� Makefile.PL �� mylib �ǥ��쥯�ȥ�˴ؤ��Ƥʤˤ� �Τ�ޤ��� �䤿���Ϥ������ä����֥ǥ��쥯�ȥ꤬���ꡢ�����˥饤�֥����������� �������Ȥ򶵤��ʤ���Фʤ�ޤ��� ���� MYEXTLIB �� WriteMakefile �ƤӽФ����ɲä��ơ����Τ褦�ʴ����� ���ޤ��礦: WriteMakefile( 'NAME' => 'Mytest2', 'VERSION_FROM' => 'Mytest2.pm', # finds $VERSION 'LIBS' => [''], # e.g., '-lm' 'DEFINE' => '', # e.g., '-DHAVE_SOMETHING' 'INC' => '', # e.g., '-I/usr/include/other' 'MYEXTLIB' => 'mylib/libmylib$(LIB_EXT)', ); =begin original and then at the end add a subroutine (which will override the pre-existing subroutine). Remember to use a tab character to indent the line beginning with "cd"! =end original ���줫�顢�����˥��֥롼������դ��ä��ޤ�(����ϴ���¸�ߤ��륵�֥롼����� ��񤭤��ޤ�)�� "cd" �ǻϤޤ�ԤΥ���ǥ�Ȥ˥���ʸ����Ȥ����Ȥ�˺��ʤ��Ǥ�������! sub MY::postamble { ' $(MYEXTLIB): mylib/Makefile cd mylib && $(MAKE) $(PASSTHRU) '; } =begin original Let's also fix the MANIFEST file so that it accurately reflects the contents of our extension. The single line that says "mylib" should be replaced by the following three lines: =end original �ޤ����ե����� MANIFEST ��䤿���Υ������ƥ󥷥������Ƥ�ȿ�Ǥ���褦�� �������ޤ��礦�� "mylib" �Ȥ���ñ��ιԤ�ʲ��λ��Ԥ��֤������ޤ��� mylib/Makefile.PL mylib/mylib.c mylib/mylib.h =begin original To keep our namespace nice and unpolluted, edit the .pm file and change the variable C<@EXPORT> to C<@EXPORT_OK>. Finally, in the .xs file, edit the #include line to read: =end original �䤿����̾�����֤����ǡ���������Ƥ��ʤ����֤��ݤĤ���� .pm �ե������ �Խ����ơ�C<@EXPORT> �����ꤷ�Ƥ���Ԥ� C<@EXPORT_OK> ���ѹ����ޤ��� �Ǹ�� .xs �ե�����ǡ�#include �ιԤ��Խ����ޤ��� #include "mylib/mylib.h" =begin original And also add the following function definition to the end of the .xs file: =end original ����ˡ��ʲ��δؿ������ .xs �ե�������������ɲä��ޤ��� double foo(a,b,c) int a long b const char * c OUTPUT: RETVAL =begin original Now we also need to create a typemap file because the default Perl doesn't currently support the const char * type. Create a file called typemap in the Mytest2 directory and place the following in it: =end original �����ǡ��ǥե���Ȥ� Perl �Ϻ��ΤȤ��� const char * �Ȥ������򥵥ݡ��Ȥ��� ���ʤ��Τǡ�typemap �ե�������������ɬ�פ�����ޤ��� Mytest2 �ǥ��쥯�ȥ�� typemap �ȸƤФ��ե������������������ʲ��� ���Ƥˤ��ޤ�: const char * T_PV =begin original Now run perl on the top-level Makefile.PL. Notice that it also created a Makefile in the mylib directory. Run make and watch that it does cd into the mylib directory and run make in there as well. =end original ���˥ȥåץ�٥�� Makefile.PL �� perl ��¹Ԥ��ޤ��� mylib �ǥ��쥯�ȥ�ˤ��� Makefile ������Ȥ������Ȥ����դ��Ƥ��������� make ��¹Ԥ�������ˤ�� mylib �ǥ��쥯�ȥ�� cd ���������� make �� �¹Ԥ����Τ��ǧ���ޤ��� =begin original Now edit the Mytest2.t script and change the number of tests to "4", and add the following lines to the end of the script: =end original Mytest2.t ������ץȤ��Խ����ơ��ƥ��Ȥο��� "4" ���ѹ����ơ� ������ץȤ������˰ʲ��ιԤ��ɲä��ޤ��� is( &Mytest2::foo(1, 2, "Hello, world!"), 7 ); is( &Mytest2::foo(1, 2, "0.0"), 7 ); ok( abs(&Mytest2::foo(0, 0, "-3.4") - 0.6) <= 0.01 ); =begin original (When dealing with floating-point comparisons, it is best to not check for equality, but rather that the difference between the expected and actual result is below a certain amount (called epsilon) which is 0.01 in this case) =end original (��ư������������Ӥ�Ԥ��Ȥ����������Ȥ������Ȥ�����å����ʤ����Ȥ� ���ɤʤΤǤ����������ͽ�ۤ�����ͤȼºݤη�̤κ��������� (�ŤȸƤФ�ޤ�)�����ξ��Ǥ� 0.01 ���⤫��Ĵ�٤Ƥ��ޤ�) =begin original Run "C" and all should be well. There are some warnings on missing tests for the Mytest2::mylib extension, but you can ignore them. =end original "C" ��¹Ԥ���С����٤Ƥ����ޤ������Ϥ��Ǥ��� Mytest2::mylib �������ƥ󥷥��˥ƥ��Ȥ��ʤ��Ȥ����ٹ𤬽Фޤ����� ̵�뤷�ƹ����ޤ��� =head2 What has happened here? (�����Dz��������Ƥ����?) =begin original Unlike previous examples, we've now run h2xs on a real include file. This has caused some extra goodies to appear in both the .pm and .xs files. =end original �����Ȥϰ�äơ�����ϼºݤΥ��󥯥롼�ɥե�������Ф��� h2xs �� �¹Ԥ��ޤ����� ����� .pm �ե������ .xs �ե������ξ�����Ф��ƴ��Ĥ����ɲä�����Τ� �⤿�餷�ޤ��� =over 4 =item * =begin original In the .xs file, there's now a #include directive with the absolute path to the mylib.h header file. We changed this to a relative path so that we could move the extension directory if we wanted to. =end original .xs �ե�����ˤϡ����ߤΤȤ������Хѥ��� mylib.h �إå����ե������ ���ꤹ�� #include �ؼ��Ҥ�����ޤ��� ��������Хѥ����ѹ����뤳�Ȥǡ��⤷˾��ʤ饨�����ƥ󥷥��� �ǥ��쥯�ȥ���ư�Ǥ���褦�ˤ��ޤ��� =item * =begin original There's now some new C code that's been added to the .xs file. The purpose of the C routine is to make the values that are #define'd in the header file accessible by the Perl script (by calling either C or C<&Mytest2::TESTVAL>). There's also some XS code to allow calls to the C routine. =end original .xs �ե�������ɲä��줿 C �Υ����ɤ�����ޤ��� C �롼�������Ū�ϡ��إå����ե������ #define ����Ƥ����ͤ� Perl ������ץ�(���ξ��ϡ�C<&Mytest2::TESTVAL> �θƤӽФ��ˤ��ޤ�)�� ��äƥ��������Ǥ���褦�ˤ��뤳�ȤǤ��� Ʊ�ͤ� C �롼�����ƤӽФ������ XS �����ɤ�����ޤ��� =item * =begin original The .pm file originally exported the name C in the C<@EXPORT> array. This could lead to name clashes. A good rule of thumb is that if the #define is only going to be used by the C routines themselves, and not by the user, they should be removed from the C<@EXPORT> array. Alternately, if you don't mind using the "fully qualified name" of a variable, you could move most or all of the items from the C<@EXPORT> array into the C<@EXPORT_OK> array. =end original .pm �ե�����ϸ��������� C<@EXPORT> �ˤ��� C ��̾���� �������ݡ��Ȥ��ޤ��� �����̾���������ǽ��������ޤ��� �褤�и�§�� #define �� C �Υ롼����ǤΤ߻Ȥ��ƥ桼��������� �Ȥ��ʤ��ΤǤ���С���������� C<@EXPORT> ������������ۤ��� �ɤ��Ȥ�����ΤǤ��� �⤦��Ĥϡ��ѿ��δ�������̾��Ȥ����Ȥ�Ȥ��Τ����ˤʤ�ʤ��ΤǤ���С� ���� C<@EXPORT> �ˤ������ǤΤۤȤ�ɤ��٤Ƥ����� C<@EXPORT_OK> �� �ܤ����Ȥ��Ǥ��ޤ��� =item * =begin original If our include file had contained #include directives, these would not have been processed by h2xs. There is no good solution to this right now. =end original ���󥯥롼�ɥե������ #include �ؼ��Ҥ���ΤǤ���С������� h2xs �ˤ�äƽ�������ޤ��� �������ǤϤ������������������Τ��ɤ������Ϥ���ޤ��� =item * =begin original We've also told Perl about the library that we built in the mylib subdirectory. That required only the addition of the C variable to the WriteMakefile call and the replacement of the postamble subroutine to cd into the subdirectory and run make. The Makefile.PL for the library is a bit more complicated, but not excessively so. Again we replaced the postamble subroutine to insert our own code. This code simply specified that the library to be created here was a static archive library (as opposed to a dynamically loadable library) and provided the commands to build it. =end original �䤿���Ϥ��Ǥ˥��֥ǥ��쥯�ȥ� mylib �Ǻ��������饤�֥��ˤĤ��Ƥ� �ؼ��� Perl �ˤ��Ƥ��ޤ��� �������׵ᤵ��Ƥ���Τϡ��ѿ� C �� WriteMakefile �θƤӽФ��� �ɲä���postamble ���֥롼�������Ū�Υǥ��쥯�ȥ�� cd ���� make �� �¹Ԥ���褦�ˤ��뤳�Ȥ����Ǥ��� �饤�֥��Τ���� Makefile.PL ��¿��ʣ���ˤϤʤ�ޤ����� ����ۤɤǤ⤢��ޤ��� ���Υ����ɤ�ñ��˺������٤��饤�֥�꤬��Ū�饤�֥��(ưŪ�����ɲ�ǽ �饤�֥���ȿ�ФΤ��)�Ǥ��뤳�Ȥ���ꤷ���饤�֥���������뤿��� ���ޥ�ɤ��󶡤��ޤ��� =back =head2 Anatomy of .xs file (.xs �ե�����β�˶) =begin original The .xs file of L<"EXAMPLE 4"> contained some new elements. To understand the meaning of these elements, pay attention to the line which reads =end original L<"EXAMPLE 4"> �� .xs �ե�����ˤϤ����Ĥ����������Ǥ�ޤ�Ǥ��ޤ��� ���������Ǥΰ�̣�����򤹤뤿��ˡ��ʲ��ιԤ����ܤ��Ƥ������� MODULE = Mytest2 PACKAGE = Mytest2 =begin original Anything before this line is plain C code which describes which headers to include, and defines some convenience functions. No translations are performed on this part, apart from having embedded POD documentation skipped over (see L) it goes into the generated output C file as is. =end original ���ι԰��������Ƥϥ��󥯥롼�ɤ���إå�����������ʴؿ���������� �ץ졼��� C �����ɤǤ��� ������ʬ�Ǥϡ��Ȥ߹��ߤ� POD ʸ���ɤ����Ф����(L �� ���Ȥ��Ƥ�������)���Ȥ�����Ƥϲ����Ѵ��ϹԤ�줺�����Τޤ޽��� C �ե����뤬 ���Ϥ���ޤ��� =begin original Anything after this line is the description of XSUB functions. These descriptions are translated by B into C code which implements these functions using Perl calling conventions, and which makes these functions visible from Perl interpreter. =end original ���ι԰ʹߤ����Ƥ� XSUB �ؿ��ε��ҤǤ��� �����ε��Ҥϡ��ؿ��� Perl �ƤӽФ���§��ȤäƼ��������ؿ��� Perl ���󥿥ץ꥿���鸫����褦�ˡ�B �� C �����ɤ��Ѵ����ޤ��� =begin original Pay a special attention to the function C. This name appears twice in the generated .xs file: once in the first part, as a static C function, then another time in the second part, when an XSUB interface to this static C function is defined. =end original C �ؿ��ˤ����̤����դ�ʧ�äƤ��������� ����̾�����������줿 .xs �ե������ 2 �󸽤�ޤ�: 1 ���ܤϺǽ����ʬ�� static C �ؿ��Ȥ��ơ�2 ���ܤ� 2 ���ܤ���ʬ�ˡ� ���� static C �ؿ��ؤ� XSUB ���󥿡��ե�������������줿�Ȥ��Ǥ��� =begin original This is quite typical for .xs files: usually the .xs file provides an interface to an existing C function. Then this C function is defined somewhere (either in an external library, or in the first part of .xs file), and a Perl interface to this function (i.e. "Perl glue") is described in the second part of .xs file. The situation in L<"EXAMPLE 1">, L<"EXAMPLE 2">, and L<"EXAMPLE 3">, when all the work is done inside the "Perl glue", is somewhat of an exception rather than the rule. =end original ����� .xs �ե�����ǤϤ��ʤ�ŵ��Ū�Ǥ�: ���� .xs �ե�����ϴ��ˤ��� C �ؿ��ؤΥ��󥿡��ե��������󶡤��ޤ��� ���줫�餳�� C �ؿ��Ϥɤ���(�����饤�֥�꤫��.xs �ե�����κǽ����ʬ)�� ������졢���δؿ��ؤ� Perl ���󥿡��ե�����(�Ĥޤ��Perl �θҡ�)�� .xs �ե������ 2 ���ܤ���ʬ�˵��Ҥ���ޤ��� L<"EXAMPLE 1">, L<"EXAMPLE 2">, L<"EXAMPLE 3"> �ξ����� �Ĥޤ����Ƥ�ư���Perl �θҡפ���¦�ǹԤ���Ȥ��ϡ� ��§�ǤϤʤ����餫���㳰������ޤ��� =head2 Getting the fat out of XSUBs (���ä���ʬ�� XSUB �γ��˽Ф�) =begin original In L<"EXAMPLE 4"> the second part of .xs file contained the following description of an XSUB: =end original L<"EXAMPLE 4"> �ǡ�.xs �ե������ 2 ���ܤ���ʬ�ˤϡ��ʲ��� XSUB �� ���Ҥ�ޤ�Ǥ��ޤ�: double foo(a,b,c) int a long b const char * c OUTPUT: RETVAL =begin original Note that in contrast with L<"EXAMPLE 1">, L<"EXAMPLE 2"> and L<"EXAMPLE 3">, this description does not contain the actual I for what is done is done during a call to Perl function foo(). To understand what is going on here, one can add a CODE section to this XSUB: =end original L<"EXAMPLE 1">, L<"EXAMPLE 2">, L<"EXAMPLE 3"> ����Ӥ��ơ����ε��Ҥ� Perl �ؿ� foo() �θƤӽФ��˲��������Ƥ���Τ��μºݤ� I<������> �� �ޤ�Ǥ��ޤ��� �����Dz��������Ƥ���Τ������򤹤뤿��ˡ����� XSUB �� CODE ���������� �ɲäǤ��ޤ�: double foo(a,b,c) int a long b const char * c CODE: RETVAL = foo(a,b,c); OUTPUT: RETVAL =begin original However, these two XSUBs provide almost identical generated C code: B compiler is smart enough to figure out the C section from the first two lines of the description of XSUB. What about C section? In fact, that is absolutely the same! The C section can be removed as well, I section or C section> is not specified: B can see that it needs to generate a function call section, and will autogenerate the OUTPUT section too. Thus one can shortcut the XSUB to become: =end original ������������� 2 �Ĥ� XSUB �ϤۤȤ��Ʊ�� C �����ɤ��������ޤ�: B ����ѥ���ϡ�XSUB �ε��Ҥκǽ�� 2 �Ԥ��� C ���������򸫤Ĥ��Ф����Ȥ��Ǥ��뤰�餤�����Ǥ��� C ���������ˤĤ��ƤϤɤ��Ǥ��礦? �ºݡ����������Ʊ���Ǥ�! I ���������� C ��������󤬻��ꤵ��ʤ��¤�>�� C ����������Ʊ�ͤ˺���Ǥ���Ʊ�ͤ� OUTPUT ���������� ��ư�������ޤ��� ���äơ�XSUB ��ʲ��Τ褦�˾�ά�Ǥ��ޤ�: double foo(a,b,c) int a long b const char * c =begin original Can we do the same with an XSUB =end original Ʊ�����Ȥ� L<"EXAMPLE 2"> �� XSUB: int is_even(input) int input CODE: RETVAL = (input % 2 == 0); OUTPUT: RETVAL =begin original of L<"EXAMPLE 2">? To do this, one needs to define a C function C. As we saw in L, a proper place for this definition is in the first part of .xs file. In fact a C function =end original �ǽ����Ǥ��礦��? ����򤹤뤿��ˤϡ�C �ؿ� C ��������� ɬ�פ�����ޤ��� L �Ǹ����褦�ˡ����������Ŭ�ڤʾ��� .xs �ե������ �ǽ����ʬ�Ǥ��� �ºݡ�C �ؿ� int is_even(int arg) { return (arg % 2 == 0); } =begin original is probably overkill for this. Something as simple as a C<#define> will do too: =end original �Ȥ����ΤϤ����餯�����ǤϤ�ꤹ���Ǥ��� C<#define> ��Ʊ�����餤ñ��ʤ��Ȥ�Ǥ��ޤ�: #define is_even(arg) ((arg) % 2 == 0) =begin original After having this in the first part of .xs file, the "Perl glue" part becomes as simple as =end original .xs �ե�����κǽ����ʬ�ˤ�������줿�塢 ��Perl �θҡפ���ʬ�ϰʲ��Τ褦��ñ��Ǥ� int is_even(input) int input =begin original This technique of separation of the glue part from the workhorse part has obvious tradeoffs: if you want to change a Perl interface, you need to change two places in your code. However, it removes a lot of clutter, and makes the workhorse part independent from idiosyncrasies of Perl calling convention. (In fact, there is nothing Perl-specific in the above description, a different version of B might have translated this to TCL glue or Python glue as well.) =end original �ºݤ�ư�����ʬ����Ҥ���ʬ��ʬΥ���뵻�Ѥ����餫�ʥȥ졼�ɥ��դǤ�: �⤷ Perl ���󥿡��ե������򤷤褦�Ȥ���ȡ������ɤ� 2 �ս���ѹ����� ɬ�פ�����ޤ��� �������������¿�����𻨤ʤ�Τ���������ºݤ�ư�����ʬ�� Perl �ƤӽФ� ��§���ð���������Ω�����ޤ��� (�ºݤΤȤ�������Ҥε��Ҥˤ� Perl ��ͭ�Τ�ΤϤʤ����ۤʤ�С������� B �Ϥ���� TCL �θҤ� Python �θҤˤ��Ѵ��Ǥ��뤫�⤷��ޤ���) =head2 More about XSUB arguments (����� XSUB �����ˤĤ���) =begin original With the completion of Example 4, we now have an easy way to simulate some real-life libraries whose interfaces may not be the cleanest in the world. We shall now continue with a discussion of the arguments passed to the B compiler. =end original �� 4 ���䴰���뤿��ˡ��䤿���������ǺǤ⥯�꡼��Ȥ����櫓�ǤϤʤ��Ǥ����� ���¤Υ饤�֥��Υ��ߥ�졼�Ȥ򤹤뤿��δ�ñ����ˡ������ޤ��� �䤿���� B ����ѥ�����Ϥ������ˤĤ��Ƥε����� ³���ʤ���Фʤ�ޤ��� =begin original When you specify arguments to routines in the .xs file, you are really passing three pieces of information for each argument listed. The first piece is the order of that argument relative to the others (first, second, etc). The second is the type of argument, and consists of the type declaration of the argument (e.g., int, char*, etc). The third piece is the calling convention for the argument in the call to the library function. =end original .xs �ե�����ǥ롼����ؤΰ�������ꤷ���Ȥ������ʤ��Ϥ��줾��� �������ꥹ�ȥ��åפ��줿���Ĥξ���֥��å����Ϥ��Ƥ���Ǥ��礦�� �ǽ�Υ֥��å��ϡ�����������Ū�ʽ��(�����ܡ������ܡ���)�Ǥ��� �����ܤΥ֥��å��ϰ����η��ǡ������η��������ʤ�ޤ��� �����ܤΥ֥��å��ϡ��������饤�֥��ؿ���ƤӽФ��Ȥ��˻Ȥ�������� �ƤӽФ�����Ǥ��� =begin original While Perl passes arguments to functions by reference, C passes arguments by value; to implement a C function which modifies data of one of the "arguments", the actual argument of this C function would be a pointer to the data. Thus two C functions with declarations =end original Perl �ϴؿ��˰����򻲾��Ϥ����ޤ�����C �ϰ��������Ϥ����ޤ�; �ְ����פ� ��ĤΥǡ����������� C �ؿ����������ˤϡ����� C �ؿ��μºݤΰ����� ���Υǡ����ؤΥݥ��󥿤ˤʤ�ޤ��� ���äơ�2 �Ĥ� C �ؿ����: int string_length(char *s); int upper_case_char(char *cp); =begin original may have completely different semantics: the first one may inspect an array of chars pointed by s, and the second one may immediately dereference C and manipulate C<*cp> only (using the return value as, say, a success indicator). From Perl one would use these functions in a completely different manner. =end original �ϴ����˰ۤʤä���̣������ޤ�: ���Ԥ� s �Ǽ����줿 char ������򸡺����ޤ�; ��Ԥ�ľ���� C �� �ǥ�ե���󥹤��ơ�C<*cp> ���������ޤ�(�֤��ͤ������򼨤���ΤȤ��� �Ȥ��ޤ�)�� Perl ����Ϥ����δؿ��������ۤʤä���ˡ�ǻȤ��ޤ��� =begin original One conveys this info to B by replacing C<*> before the argument by C<&>. C<&> means that the argument should be passed to a library function by its address. The above two function may be XSUB-ified as =end original C<&> �ˤ����������� C<*> ���֤������뤳�Ȥǡ����ξ���� B �� �������ޤ��� C<&> �ϡ����������ɥ쥹�ǥ饤�֥��ؿ����Ϥ���뤳�Ȥ��̣���ޤ��� �嵭�� 2 �Ĥδؿ��ϰʲ��Τ褦�� XSUB ���Ǥ��ޤ� int string_length(s) char * s int upper_case_char(cp) char &cp =begin original For example, consider: =end original ��Ȥ��ơ����Τ�Τ�ͤ��ޤ�: int foo(a,b) char &a char * b =begin original The first Perl argument to this function would be treated as a char and assigned to the variable a, and its address would be passed into the function foo. The second Perl argument would be treated as a string pointer and assigned to the variable b. The I of b would be passed into the function foo. The actual call to the function foo that B generates would look like this: =end original �ǽ�� Perl �ΰ����Ϥ��δؿ��Ǥ� char �Ȥ��ư���졢�ѿ� a ���������졢 �����Ƥ��Υ��ɥ쥹�ϴؿ� foo ���Ϥ���ޤ��� �����ܤ� Perl �ΰ�����ʸ����ؤΥݥ��󥿡��Ȥ��ư���졢�ѿ� b �� ��������ޤ��� b �� I<��> ��ؿ� foo ���Ϥ���ޤ��� B ����������ؿ� foo �ؤμºݤθƤӽФ��ϼ��Τ褦�ˤʤ�ޤ��� foo(&a, b); =begin original B will parse the following function argument lists identically: =end original B �ϰʲ��δؿ������ꥹ�Ȥ�Ʊ����ΤȲ��Ϥ��ޤ�: char &a char&a char & a =begin original However, to help ease understanding, it is suggested that you place a "&" next to the variable name and away from the variable type), and place a "*" near the variable type, but away from the variable name (as in the call to foo above). By doing so, it is easy to understand exactly what will be passed to the C function -- it will be whatever is in the "last column". =end original �������ʤ���狼��䤹�����뤿��ˡ� "&" ���ѿ�̾��³�����ѿ��η������ Υ���Ƥ������Ȥȡ� "*" ��̾�˶�Ť��뤬�ѿ�̾�����Υ��(���Ҥ��� foo �� �ƤӽФ��Τ褦��)�Ȥ������Ȥ򤪴��ᤷ�ޤ��� �����Ԥ����Ȥǡ��ºݤ� C �δؿ����Ϥ���뤬�ʤ�ʤΤ����ñ�� ����Ǥ��ޤ� -- ����� "last column" �ˤ����ΤǤ��礦�� =begin original You should take great pains to try to pass the function the type of variable it wants, when possible. It will save you a lot of trouble in the long run. =end original ��ǽ�ʤ�С��ؿ���(��ʬ��˾��)�ѿ��η����Ϥ����Ȥ�ĩ�路�ƶ�ϫ�����ۤ��� �ɤ��Ǥ��礦�� Ĺ���ܤǸ���Ф���ˤ�ä�¿���Υȥ�֥���򤱤뤳�Ȥˤʤ�ޤ��� =head2 The Argument Stack (���������å�) =begin original If we look at any of the C code generated by any of the examples except example 1, you will notice a number of references to ST(n), where n is usually 0. "ST" is actually a macro that points to the n'th argument on the argument stack. ST(0) is thus the first argument on the stack and therefore the first argument passed to the XSUB, ST(1) is the second argument, and so on. =end original �� 1 �ʳ��� ���Υ��塼�ȥꥢ��Ǻ������� C �����ɤ򸫤��Τʤ�С� ���������ST(n) (n ���̾�� 0)���Ф��뻲�Ȥ˵����Ĥ������ȤǤ��礦�� "ST" �ϼºݤˤϰ��������å���� n ���ܤΰ�����ؤ������ޥ����Ǥ��� ���ä� ST(0) �ϥ����å���κǽ�ΰ����ʤΤǡ�XSUB ���Ϥ����ǽ�� �����Ǥ��ꡢST(1) �������ܤΰ����Ȥʤ�ޤ��� =begin original When you list the arguments to the XSUB in the .xs file, that tells B which argument corresponds to which of the argument stack (i.e., the first one listed is the first argument, and so on). You invite disaster if you do not list them in the same order as the function expects them. =end original .xs �ե�������� XSUB ���Ф��������ꥹ�ȥ��åפ����Ȥ�������ϰ��� �����å����б���������� B �˻��ꤹ��Ȥ������ȤǤ�(�㤨�С� �ꥹ�Ȥκǽ�ˤ�����������������ܤʤ���������Ȥ�������)�� �ؿ������Ԥ���Τ�Ʊ�����֤ǥꥹ�ȥ��åפ��ʤ���С��פ������ʤ� �ҳ��򾷤����Ȥˤʤ�ޤ��� =begin original The actual values on the argument stack are pointers to the values passed in. When an argument is listed as being an OUTPUT value, its corresponding value on the stack (i.e., ST(0) if it was the first argument) is changed. You can verify this by looking at the C code generated for Example 3. The code for the round() XSUB routine contains lines that look like this: =end original ���������å��μºݤ��ͤ��Ϥ��줿�ͤؤΥݥ��󥿤Ǥ��� ������ OUTPUT ���ͤȤ��Ƶ󤲤��Ƥ����硢�����å�����б������� (�㤨�С��ǽ�ΰ����ʤ� ST(0)) ���ѹ�����ޤ��� �� 3 ���������줿 C �����ɤ򸫤뤳�ȤǤ���򸡾ڤǤ��ޤ��� round() XSUB �롼����Υ����ɤϰʲ��Τ褦�˸�����Ԥ�ޤ�Ǥ��ޤ�: double arg = (double)SvNV(ST(0)); /* Round the contents of the variable arg */ sv_setnv(ST(0), (double)arg); =begin original The arg variable is initially set by taking the value from ST(0), then is stored back into ST(0) at the end of the routine. =end original �����ѿ��ϡ��ޤ� ST(0) ���ͤ��������Τ˥��åȤ��졢���θ�롼����� �Ǹ�� ST(0) �˽��ᤵ��ޤ��� =begin original XSUBs are also allowed to return lists, not just scalars. This must be done by manipulating stack values ST(0), ST(1), etc, in a subtly different way. See L for details. =end original XSUB ��ñ�ʤ륹����ǤϤʤ����ꥹ�Ȥ��֤����Ȥ�Ǥ��ޤ��� ����ϥ����å��� ST(0), ST(1) �ʤɤ���̯�˰㤦��ˡ�����뤳�Ȥˤ�ä� �Ԥ�ɬ�פ�����ޤ��� �ܤ����� L �򻲾Ȥ��Ƥ��������� =begin original XSUBs are also allowed to avoid automatic conversion of Perl function arguments to C function arguments. See L for details. Some people prefer manual conversion by inspecting C even in the cases when automatic conversion will do, arguing that this makes the logic of an XSUB call clearer. Compare with L<"Getting the fat out of XSUBs"> for a similar tradeoff of a complete separation of "Perl glue" and "workhorse" parts of an XSUB. =end original XSUB �Ϥޤ���Perl �ؿ��������� C �ؿ������ؤμ�ư�Ѵ�����򤹤뤳�Ȥ� �Ǥ��ޤ��� �ܤ����� L �򻲾Ȥ��Ƥ��������� ��ư�Ѵ����Ԥ�����Ǥ� C �򸡺����뤳�ȤǤμ�ư�Ѵ��򹥤� �͡��⤤�ơ�����ˤ�� XSUB �ƤӽФ��Υ����å�����꤭�줤�ˤʤ�� ��ĥ���Ƥ��ޤ��� XSUB �ˤ������Perl �θҡפȡּ�Ư����פδ�����ʬΥ�˴ؤ�������褦�� �ȥ졼�ɥ��դ˴ؤ��ơ�L<"Getting the fat out of XSUBs"> �� ��Ӥ��Ƥ��������� =begin original While experts may argue about these idioms, a novice to Perl guts may prefer a way which is as little Perl-guts-specific as possible, meaning automatic conversion and automatic call generation, as in L<"Getting the fat out of XSUBs">. This approach has the additional benefit of protecting the XSUB writer from future changes to the Perl API. =end original ����ȤϤ����δ���ˡ�ˤĤ��Ƽ�ĥ���������Perl �������˴ؤ���鿴�Ԥ� Perl �����˸�ͭ�Τ�Τ��ǽ�ʸ¤꾮����������ˡ�򹥤ߤޤ�; ����ϤĤޤ� L<"Getting the fat out of XSUBs"> �Τ褦�ʡ���ư�Ѵ��ȼ�ư�ƤӽФ� �����Ǥ��� ���μ�ˡ�ˤϡ�XSUB ��Ԥ� Perl API �ξ�����ѹ�������Ȥ����ɲä������� ����ޤ��� =head2 Extending your Extension (�������ƥ󥷥����ĥ����) =begin original Sometimes you might want to provide some extra methods or subroutines to assist in making the interface between Perl and your extension simpler or easier to understand. These routines should live in the .pm file. Whether they are automatically loaded when the extension itself is loaded or only loaded when called depends on where in the .pm file the subroutine definition is placed. You can also consult L for an alternate way to store and load your extra subroutines. =end original Perl�Ȥ��ʤ��Υ������ƥ󥷥��Ȥδ֤Υ��󥿡��ե����������ñ�ˡ� ������򤷤䤹������Τ�����뤿��˥᥽�åɤ䥵�֥롼����� ��ꤿ���Ȼפ��������뤫�⤷��ޤ��� �������ä��롼����� .pm �ե�������֤��Τ��ɤ��Ǥ��� ���줬�������ƥ󥷥�󼫿Ȥ������ɤ��줿�Ȥ������ɤ����ˤ����� ���֥롼����������֤���Ƥ���. pm �ե�����˰�¸����ƤӽФ��ΤȤ��Τ� �����ɤ���ˤ�������ưŪ�˥����ɤ��Ԥ��ޤ��� �ɲäΥ��֥롼������ݴɤ����ɤ߹���⤦��Ĥ���ˡ�Ȥ��Ƥ� L �򻲹ͤˤ��뤳�Ȥ�Ǥ��ޤ��� =head2 Documenting your Extension (�������ƥ󥷥���ʸ�񲽤���) =begin original There is absolutely no excuse for not documenting your extension. Documentation belongs in the .pm file. This file will be fed to pod2man, and the embedded documentation will be converted to the manpage format, then placed in the blib directory. It will be copied to Perl's manpage directory when the extension is installed. =end original ���ʤ��Υ������ƥ󥷥��˴ؤ���ɥ�����Ȥ��ʤ��Ȥ������ȤˤĤ��ơ� ���ʤ��ϲ��θ�������Ǥ��ޤ��� �ɥ�����Ȥ� .pm �ե������°���ޤ��� ���Υե������ pod2man ��������ޤ졢�����ޤ줿�ɥ�����Ȥ� man �ڡ����ե����ޥåȤ��Ѵ����졢���줫�� blib �ǥ��쥯�ȥ���֤���ޤ��� ���Υɥ�����Ȥϡ��������ƥ󥷥�󤬥��󥹥ȡ��뤵���Ȥ��� Perl �� man �ڡ����ǥ��쥯�ȥ�ˤ⥳�ԡ�����ޤ��� =begin original You may intersperse documentation and Perl code within the .pm file. In fact, if you want to use method autoloading, you must do this, as the comment inside the .pm file explains. =end original ���ʤ��� .pm �ե�����ˤ���ɥ�����Ȥ� Perl �����ɤ�ޤ����餹���Ȥ� ���뤫�⤷��ޤ��� ���¡����ʤ����᥽�åɤ� autoload ���褦�Ȥ���ʤ�С�.pm �ե��������� ���륳���Ȥ���������Ƥ���褦�ˤ����Ԥ�ͤФʤ�ޤ��� =begin original See L for more information about the pod format. =end original pod �ե����ޥåȤˤĤ��ƤΤ��ܤ�������� L �򻲾Ȥ��Ƥ��������� =head2 Installing your Extension (�������ƥ󥷥��򥤥󥹥ȡ��뤹��) =begin original Once your extension is complete and passes all its tests, installing it is quite simple: you simply run "make install". You will either need to have write permission into the directories where Perl is installed, or ask your system administrator to run the make for you. =end original ���ʤ��κ�ä��������ƥ󥷥�󤬴����������Ĥ��٤ƤΥƥ��Ȥ˹�ʤ���С� �����¤�ñ��ʤ�꤫���ǥ��󥹥ȡ��뤷�ޤ��� ����ñ�� "make install" �ȼ¹Ԥ�������Ǥ��� Perl �����󥹥ȡ��뤵�줿�ǥ��쥯�ȥ���Ф���񤭹��߸��¤����äƤ��� ɬ�פ�����ޤ��������뤤�ϡ����ʤ��Υ����ƥ�����Ԥˤ��ʤ��� make �� �¹Ԥ���褦���ꤤ����ɬ�פ�����Ǥ��礦�� =begin original Alternately, you can specify the exact directory to place the extension's files by placing a "PREFIX=/destination/directory" after the make install. (or in between the make and install if you have a brain-dead version of make). This can be very useful if you are building an extension that will eventually be distributed to multiple systems. You can then just archive the files in the destination directory and distribute them to your destination systems. =end original �̤���ˡ�Ȥ��ơ�"make install" �θ���� (���뤤�Ϥ������� make ��ȤäƤ��� ���� "make" �� "install" �δ֤�)"PREFIX=/destination/directory" �� �񤯤��Ȥǡ��������ƥ󥷥��Υե�������֤����Τʥǥ��쥯�ȥ�� ����Ǥ��ޤ��� ����ϡ��ǽ�Ū��ʣ���Υ����ƥ�����ۤ���륨�����ƥ󥷥���ӥ�ɤ��Ƥ��� ���ˤȤƤ�ͭ�ѤǤ��� ���θ�ñ�˽�����Υǥ��쥯�ȥ�Υե�����򥢡������֤��ơ���Ū�Υ����ƥ�� ���ۤ��ޤ��� =head2 EXAMPLE 5 (�� 5) =begin original In this example, we'll do some more work with the argument stack. The previous examples have all returned only a single value. We'll now create an extension that returns an array. =end original ������Ǥϡ����������å��ˤ���ʤ��Ȥ򤷤ޤ��� ������Ǥ����ơ�ñ����ͤ��֤���Τ����Ǥ��� �����Ǥϡ�������֤��������ƥ󥷥�����ޤ��� =begin original This extension is very Unix-oriented (struct statfs and the statfs system call). If you are not running on a Unix system, you can substitute for statfs any other function that returns multiple values, you can hard-code values to be returned to the caller (although this will be a bit harder to test the error case), or you can simply not do this example. If you change the XSUB, be sure to fix the test cases to match the changes. =end original ���Υ������ƥ󥷥��ϤȤƤ� Unix �ظ��Ǥ�(struct statfs �� statfs �����ƥॳ����)�� �⤷ Unix �����ƥ�ʳ��Ǽ¹Ԥ���ʤ顢statfs ��ʣ�����ͤ��֤��̤δؿ��� �֤������뤫���ƤӽФ������֤��ͤ�ϡ��ɥ����ǥ��󥰤���(����������� ���顼�ξ���ƥ��Ȥ���Τ������񤷤��ʤ�ޤ�)�������뤤��ñ�� �������¹Ԥ��ʤ��Ȥ������Ȥ�Ǥ��ޤ��� �⤷ XSUB ���ѹ������ʤ顢�ƥ��ȥ��������ѹ��ˤ��碌�Ƴμ¤� �������Ƥ��������� =begin original Return to the Mytest directory and add the following code to the end of Mytest.xs: =end original Mytest �ǥ��쥯�ȥ����äơ�Mytest.xs �������˰ʲ��Υ����ɤ��ɲä��ޤ�: void statfs(path) char * path INIT: int i; struct statfs buf; PPCODE: i = statfs(path, &buf); if (i == 0) { XPUSHs(sv_2mortal(newSVnv(buf.f_bavail))); XPUSHs(sv_2mortal(newSVnv(buf.f_bfree))); XPUSHs(sv_2mortal(newSVnv(buf.f_blocks))); XPUSHs(sv_2mortal(newSVnv(buf.f_bsize))); XPUSHs(sv_2mortal(newSVnv(buf.f_ffree))); XPUSHs(sv_2mortal(newSVnv(buf.f_files))); XPUSHs(sv_2mortal(newSVnv(buf.f_type))); } else { XPUSHs(sv_2mortal(newSVnv(errno))); } =begin original You'll also need to add the following code to the top of the .xs file, just after the include of "XSUB.h": =end original �ޤ���.xs �ե��������Ƭ��"XSUB.h" �򥤥󥯥롼�ɤ���ľ��ˡ��ʲ��Υ����ɤ� �ɲä���ɬ�פ�����ޤ�: #include =begin original Also add the following code segment to Mytest.t while incrementing the "9" tests to "11": =end original �ޤ����ʲ��Υ����ɤ� Mytest.t ���ɲä��������"9" �Υƥ��Ȥ� "11" �� ���䤷�ޤ�: @a = &Mytest::statfs("/blech"); ok( scalar(@a) == 1 && $a[0] == 2 ); @a = &Mytest::statfs("/"); is( scalar(@a), 7 ); =head2 New Things in this Example (������Ǥο���������) =begin original This example added quite a few new concepts. We'll take them one at a time. =end original ������Ϥ����Ĥ���������������ǰ���ɲä��ޤ��� ���˰�Ĥ��Ĥ��ޤ��� =over 4 =item * =begin original The INIT: directive contains code that will be placed immediately after the argument stack is decoded. C does not allow variable declarations at arbitrary locations inside a function, so this is usually the best way to declare local variables needed by the XSUB. (Alternatively, one could put the whole C section into braces, and put these declarations on top.) =end original INIT: �ؼ��Ҥϡ����������å����ǥ����ɤ��줿ľ����֤���륳���ɤ�ޤߤޤ��� C �Ǥϴؿ����Ǥ�λԤǤ��ѿ����������Ƥ��ʤ��Τǡ� ��������� XSUB �ˤ�ä�ɬ�פʥ��������ѿ���������뤿��κ�������ˡ�Ǥ��� (���ذƤȤ��Ƥϡ�C ������������Τ��椫�ä��ǰϤäơ������� �������Ƭ���֤����Ȥ�Ǥ��ޤ���) =item * =begin original This routine also returns a different number of arguments depending on the success or failure of the call to statfs. If there is an error, the error number is returned as a single-element array. If the call is successful, then a 9-element array is returned. Since only one argument is passed into this function, we need room on the stack to hold the 9 values which may be returned. =end original ���Υ롼����Ϥޤ���statfs �θƤӽФ������������Ԥ��˰�¸���ơ��ۤʤä� �����ͤ��֤��ޤ��� ���顼�ξ��ϡ����顼�ֹ椬ñ������Ǥ�����Ȥ����֤���ޤ��� �ƤӽФ��������������ϡ�9 ���Ǥ������֤���ޤ��� ���δؿ��ˤϰ����� 1 �Ĥ����Ϥ����Τǡ��֤� 9 �Ĥ��ͤ��ݻ����뤿��� �����å��˶�������ɬ�פ�����ޤ��� =begin original We do this by using the PPCODE: directive, rather than the CODE: directive. This tells B that we will be managing the return values that will be put on the argument stack by ourselves. =end original �����CODE: �ؼ��ҤǤϤʤ� PPCODE: �ؼ��Ҥ�Ȥ����ȤǹԤ��ޤ��� ����ϡ����������å����֤�����֤��ͤδ�����ʬ���ȤǹԤ����Ȥ� B ���Τ餻�ޤ��� =item * =begin original When we want to place values to be returned to the caller onto the stack, we use the series of macros that begin with "XPUSH". There are five different versions, for placing integers, unsigned integers, doubles, strings, and Perl scalars on the stack. In our example, we placed a Perl scalar onto the stack. (In fact this is the only macro which can be used to return multiple values.) =end original �ƤӽФ������֤��ͤ��֤�����ξ�꤬�����å���ˤۤ������� "XPUSH" �ǻϤޤ��Ϣ�Υޥ�����Ȥ��ޤ��� ����ˤϡ������å�������������ʤ�������double��ʸ����Perl ������� �֤������ 5 ���ब����ޤ��� ������Ǥϡ������å���� Perl ������򤪤��ޤ��� (�ºݤΤȤ����������ʣ�����ͤ��֤��Ȥ��˻Ȥ���ͣ��Υޥ����Ǥ���) =begin original The XPUSH* macros will automatically extend the return stack to prevent it from being overrun. You push values onto the stack in the order you want them seen by the calling program. =end original XPUSH* �ޥ����ϡ��֤��ͥ����å��������С���󤹤뤳�Ȥ��ɤ�����ˡ���ưŪ�� �֤��ͥ����å����ĥ���ޤ��� �ƤӽФ��ץ�����फ�鸫���Ƥۤ������֤˥����å����ͤ�ץå��夷�ޤ��� =item * =begin original The values pushed onto the return stack of the XSUB are actually mortal SV's. They are made mortal so that once the values are copied by the calling program, the SV's that held the returned values can be deallocated. If they were not mortal, then they would continue to exist after the XSUB routine returned, but would not be accessible. This is a memory leak. =end original XSUB ���֤��ͥ����å��˥ץå��夵�줿�ͤϼºݤˤϴ�ȯ���� SV �Ǥ��� ����ϴ�ȯ���ʤΤǡ���ö�ƤӽФ��ץ������ˤ�ä��ͤ����ԡ������С� �֤��ͤ��ݻ����Ƥ��� SV �ϳ����Ǥ��ޤ��� �⤷����餬��ȯ���Ǥʤ��ʤ顢XSUB �롼���󤫤��֤ä����¸�ߤ�³���ޤ����� ���������Ǥ��ʤ��ʤ�ޤ��� ����ϥ���꡼���Ǥ��� =item * =begin original If we were interested in performance, not in code compactness, in the success branch we would not use C macros, but C macros, and would pre-extend the stack before pushing the return values: =end original �⤷�����ɥ������ǤϤʤ���ǽ�˶�̣������Τʤ顢�������ˤ� C �ǤϤʤ� C ��Ȥäơ��֤��ͤ�ץå��夹������ͽ�᥹���å��� ��ĥ���Ƥ����ޤ�: EXTEND(SP, 7); =begin original The tradeoff is that one needs to calculate the number of return values in advance (though overextending the stack will not typically hurt anything but memory consumption). =end original �ȥ졼�ɥ��դϡ�ͽ���֤��ͤο���׻����Ƥ���ɬ�פ����뤳�ȤǤ� (�����������å���;ʬ�˳�ĥ���Ƥ⡢ŵ��Ū�ˤϥ������ʳ��ˤϤʤ������� ����ޤ���)�� =begin original Similarly, in the failure branch we could use C I extending the stack: the Perl function reference comes to an XSUB on the stack, thus the stack is I large enough to take one return value. =end original Ʊ�ͤˡ����Ի��ˤϥ����å��� I<��ĥ������> C ��Ȥ��ޤ�: Perl �ؿ��Υ�ե���󥹤ϥ����å��� XSUB ���Ϥ����Τǡ� �����å��� I<���> ��Ĥ��֤��ͤ��֤��ˤϽ�ʬ���礭��������ޤ��� =back =head2 EXAMPLE 6 (�� 6) =begin original In this example, we will accept a reference to an array as an input parameter, and return a reference to an array of hashes. This will demonstrate manipulation of complex Perl data types from an XSUB. =end original ������Ǥϡ����ϥѥ�᡼���Ȥ�������ؤΥ�ե���󥹤�����դ����ϥå���� ����ؤΥ�ե���󥹤��֤��ޤ��� �����Ǥϡ�ʣ���� Perl �ǡ������� XSUB ����������ˡ�򼨤��ޤ��� =begin original This extension is somewhat contrived. It is based on the code in the previous example. It calls the statfs function multiple times, accepting a reference to an array of filenames as input, and returning a reference to an array of hashes containing the data for each of the filesystems. =end original ���Υ������ƥ󥷥��Ϥ���Լ����Ǥ��� ����ϰ�������Υ����ɤ���äˤ��Ƥ��ޤ��� ����� statfs �ؿ���ʣ���ƤӽФ������ϤȤ��ƥե�����̾������ؤ� ��ե���󥹤�����դ��ơ����줾��Υե����륷���ƥ�Υǡ�����ޤ� �ϥå��������ؤΥ�ե���󥹤��֤��ޤ��� =begin original Return to the Mytest directory and add the following code to the end of Mytest.xs: =end original Mytest �ǥ��쥯�ȥ����äơ�Mytest.xs �������˰ʲ��Υ����ɤ��ɲä��ޤ�: SV * multi_statfs(paths) SV * paths INIT: AV * results; I32 numpaths = 0; int i, n; struct statfs buf; if ((!SvROK(paths)) || (SvTYPE(SvRV(paths)) != SVt_PVAV) || ((numpaths = av_len((AV *)SvRV(paths))) < 0)) { XSRETURN_UNDEF; } results = (AV *)sv_2mortal((SV *)newAV()); CODE: for (n = 0; n <= numpaths; n++) { HV * rh; STRLEN l; char * fn = SvPV(*av_fetch((AV *)SvRV(paths), n, 0), l); i = statfs(fn, &buf); if (i != 0) { av_push(results, newSVnv(errno)); continue; } rh = (HV *)sv_2mortal((SV *)newHV()); hv_store(rh, "f_bavail", 8, newSVnv(buf.f_bavail), 0); hv_store(rh, "f_bfree", 7, newSVnv(buf.f_bfree), 0); hv_store(rh, "f_blocks", 8, newSVnv(buf.f_blocks), 0); hv_store(rh, "f_bsize", 7, newSVnv(buf.f_bsize), 0); hv_store(rh, "f_ffree", 7, newSVnv(buf.f_ffree), 0); hv_store(rh, "f_files", 7, newSVnv(buf.f_files), 0); hv_store(rh, "f_type", 6, newSVnv(buf.f_type), 0); av_push(results, newRV((SV *)rh)); } RETVAL = newRV((SV *)results); OUTPUT: RETVAL =begin original And add the following code to Mytest.t, while incrementing the "11" tests to "13": =end original �ޤ����ʲ��Υ����ɤ� Mytest.t ���ɲä��������"11" �Υƥ��Ȥ� "13" �����䤷�ޤ�: $results = Mytest::multi_statfs([ '/', '/blech' ]); ok( ref $results->[0]) ); ok( ! ref $results->[1] ); =head2 New Things in this Example (������Ǥο���������) =begin original There are a number of new concepts introduced here, described below: =end original �����Ǥϸ�Ҥ��뤤���Ĥ��ο�������ǰ������ޤ�: =over 4 =item * =begin original This function does not use a typemap. Instead, we declare it as accepting one SV* (scalar) parameter, and returning an SV* value, and we take care of populating these scalars within the code. Because we are only returning one value, we don't need a C directive - instead, we use C and C directives. =end original ���δؿ��� typemap ��Ȥ��ޤ��� ����ˡ���Ĥ� SV* (������) ������������졢��Ĥ� SV* �ͤ��֤��褦�� ������ơ������Υ�����򥳡�����ǰ��������ޤ��� ��Ĥ��ͤ������֤��Τǡ� C �ؼ��Ҥ����פǤ� - ����ˡ� C �� C �λؼ��Ҥ�Ȥ��ޤ��� =item * =begin original When dealing with references, it is important to handle them with caution. The C block first checks that C returns true, which indicates that paths is a valid reference. It then verifies that the object referenced by paths is an array, using C to dereference paths, and C to discover its type. As an added test, it checks that the array referenced by paths is non-empty, using the C function (which returns -1 if the array is empty). The XSRETURN_UNDEF macro is used to abort the XSUB and return the undefined value whenever all three of these conditions are not met. =end original ��ե���󥹤򰷤��Ȥ�����������դ��ư������Ȥ����פǤ��� C �֥��å��Ϥޤ���C �������֤����Ȥ������å����ޤ�; �����ѥ��������ʥ�ե���󥹤Ǥ��뤳�Ȥ򼨤��Ƥ��ޤ��� ���줫�顢C �ǥѥ���ǥ�ե���󥹤��ơ�C �Ǥ��η��� Ĵ�٤뤳�Ȥǡ��ѥ��ǥ�ե���󥹤���Ƥ��륪�֥������Ȥ�����Ǥ��뤳�Ȥ� ���ڤ��ޤ��� �ɲäΥƥ��ȤȤ��ơ�(���󤬶��ξ��� -1 ���֤�)C �ؿ���Ȥäơ� �ѥ��ǥ�ե���󥹤��줿���󤬶��Ǥʤ����Ȥ�����å����ޤ��� XSRETURN_UNDEF �ޥ����ϡ������ 3 �Ĥξ��Τɤ줫����Ω���ʤ��Ȥ��� XSUB �����Ǥ���̤����ͤ��֤�����˻Ȥ��ޤ��� =item * =begin original We manipulate several arrays in this XSUB. Note that an array is represented internally by an AV* pointer. The functions and macros for manipulating arrays are similar to the functions in Perl: C returns the highest index in an AV*, much like $#array; C fetches a single scalar value from an array, given its index; C pushes a scalar value onto the end of the array, automatically extending the array as necessary. =end original ���� XSUB �Ǥ����Ĥ�����������ޤ��� ���������Ū�� AV* �ݥ��󥿤�ɽ������Ƥ��뤳�Ȥ����դ��Ƥ��������� �������Τ���δؿ��ȥޥ����� Perl �δؿ��Ȼ��Ƥ��ޤ�: C �ϡ� $#array ��Ʊ�͡�AV* �κ����ź�������֤��ޤ�; C ��ź������ ��äơ����󤫤� 1 �ĤΥ������ͤ���Ф��ޤ�; C �ϥ������ͤ� ����κǸ�˥������ͤ�ץå��夷���⤷ɬ�פʤ鼫ưŪ��������ĥ���ޤ��� =begin original Specifically, we read pathnames one at a time from the input array, and store the results in an output array (results) in the same order. If statfs fails, the element pushed onto the return array is the value of errno after the failure. If statfs succeeds, though, the value pushed onto the return array is a reference to a hash containing some of the information in the statfs structure. =end original �äˡ��������󤫤���٤� 1 �Ĥ����ѥ�̾���ɤ߹��ߡ���̤��������� Ʊ��������ݴɤ��ޤ��� �⤷ statfs �����Ԥ���ȡ��֤�������˥ץå��夵������Ǥϼ��Ի��� errno �Ǥ��� ��������statfs ����������ȡ��֤�������˥ץå��夵����ͤ� statfs ��¤�Τ� �����ޤ�ϥå���ؤΥ�ե���󥹤Ǥ��� =begin original As with the return stack, it would be possible (and a small performance win) to pre-extend the return array before pushing data into it, since we know how many elements we will return: =end original �֤��ͥ����å��˴ؤ��ơ������Ĥ����Ǥ��֤���뤫��ʬ���äƤ���Τǡ� �ǡ�����ץå��夹�������֤��������ͽ���ĥ���Ƥ������Ȥ���ǽ�Ǥ� (�����ƾ�����ǽ���褯�ʤ�ޤ�): av_extend(results, numpaths); =item * =begin original We are performing only one hash operation in this function, which is storing a new scalar under a key using C. A hash is represented by an HV* pointer. Like arrays, the functions for manipulating hashes from an XSUB mirror the functionality available from Perl. See L and L for details. =end original ���δؿ��Ǥϡ�C ��Ȥäƥ������Ф��ƿ�������������ݴɤ���Ȥ����� 1 �ĤΥϥå������Τߤ�Ԥ��ޤ��� �ϥå���� HV* �ݥ��󥿤�ɽ������ޤ��� �����Ʊ�͡�XSUB ����ϥå��������ؿ��ϡ�Perl �������Ѳ�ǽ�ʵ�ǽ�� ���̤��Ǥ��� �ܤ����� L �� L �򻲾Ȥ��Ƥ��������� =item * =begin original To create a reference, we use the C function. Note that you can cast an AV* or an HV* to type SV* in this case (and many others). This allows you to take references to arrays, hashes and scalars with the same function. Conversely, the C function always returns an SV*, which may need to be cast to the appropriate type if it is something other than a scalar (check with C). =end original ��ե���󥹤���ˤϡ� C �ؿ���Ȥ��ޤ��� ���ξ��(����Ӥ���¾¿���ξ��)��AV* �� HV* �� SV* ���� ���㥹�ȤǤ��뤳�Ȥ����դ��Ƥ��������� ����ˤ�ꡢƱ���ؿ������󡢥ϥå��塢������Υ�ե���󥹤����뤳�Ȥ� �Ǥ��ޤ��� ȿ�Фˡ� C �ؿ��Ͼ�� SV* ���֤��Τǡ��⤷���줬(C �� �����å�����)������ʳ��ξ�硢Ŭ�ڤʷ��˥��㥹�Ȥ���ɬ�פ�����ޤ��� =item * =begin original At this point, xsubpp is doing very little work - the differences between Mytest.xs and Mytest.c are minimal. =end original ���λ����ǡ�xsubpp �ϤۤȤ�ɲ��⤷�Ƥ��ޤ��� - Mytest.xs �� Mytest.c �Ȥ� ���ϺǾ��¤Ǥ��� =back =head2 EXAMPLE 7 (Coming Soon) (�� 7 (��������)) =begin original XPUSH args AND set RETVAL AND assign return value to array =end original ������ XPUSH �� RETVAL �Υ��åȤ��֤��ͤ�����ؤ����� =head2 EXAMPLE 8 (Coming Soon) (�� 8 (��������)) =begin original Setting $! =end original $! �򥻥åȤ��� =head2 EXAMPLE 9 Passing open files to XSes (�� 9 �����ץ󤷤��ե������ XS ���Ϥ�) =begin original You would think passing files to an XS is difficult, with all the typeglobs and stuff. Well, it isn't. =end original �������֤ʤɤǡ�XS �˥ե�������Ϥ��Τ��񤷤��ȹͤ��뤫�⤷��ޤ��� ���äȡ������ǤϤ���ޤ��� =begin original Suppose that for some strange reason we need a wrapper around the standard C library function C. This is all we need: =end original ɸ�� C �饤�֥��ؿ� C �Υ�åѡ���ɬ�ס��Ȥ����Ի׵Ĥ� ��ͳ������Ȥ��ޤ��礦�� ɬ�פʤ�Τϰʲ��Τ�Τ����Ǥ�: #define PERLIO_NOT_STDIO 0 #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include int fputs(s, stream) char * s FILE * stream =begin original The real work is done in the standard typemap. =end original �ºݤκ�Ȥ�ɸ��� typemap �ǹԤ��ޤ��� =begin original B you loose all the fine stuff done by the perlio layers. This calls the stdio function C, which knows nothing about them. =end original B<������>��perlio �ؤǹԤ��������餷����ǽ���Ƥ���������Ȥˤʤ�ޤ��� ����� stdio �ؿ� C ��ƤӽФ��Τǡ������ˤĤ��Ƥϲ��⤷�ޤ��� =begin original The standard typemap offers three variants of PerlIO *: C (T_IN), C (T_INOUT) and C (T_OUT). A bare C is considered a T_INOUT. If it matters in your code (see below for why it might) #define or typedef one of the specific names and use that as the argument or result type in your XS file. =end original ɸ�� typemap �� 3 ����� PerlIO * ���󶡤��ޤ�: C (T_IN), C (T_INOUT), C (T_OUT) �Ǥ��� ���� C �� T_INOUT �Ȥ��ư����ޤ��� ��������Ǥ��줬����ˤʤ���(�ʤ������ʤ����뤫�ϰʲ��� ���Ȥ��Ƥ�������)�������̾���ΰ�Ĥ� #define �� typedef ���ơ� ����� XS �ե�����ǰ������̤η��Ȥ��ƻȤäƤ��������� =begin original The standard typemap does not contain PerlIO * before perl 5.7, but it has the three stream variants. Using a PerlIO * directly is not backwards compatible unless you provide your own typemap. =end original perl 5.7 ������ɸ�� typemap �ˤ� PerlIO * �ϴޤޤ�Ƥ��ޤ��󤬡� 3 ����Υ��ȥ꡼��μ��ब����ޤ��� PerlIO * ��ľ�ܻȤ��ȡ��ȼ��� typemap ���󶡤��ʤ��¤ꡢ �����ߴ��ǤϤ���ޤ��� =begin original For streams coming I perl the main difference is that C will get the output PerlIO * - which may make a difference on a socket. Like in our example... =end original perl I<����> ��륹�ȥ꡼��ˤĤ��ơ���ʰ㤤�� C �� ���� PerlIO * ������Ȥ������ȤǤ� - ����ϥ����åȤξ��˰㤤�Ȥʤ�ޤ��� �䤿�������Ʊ�ͤˡ� =begin original For streams being handed I perl a new file handle is created (i.e. a reference to a new glob) and associated with the PerlIO * provided. If the read/write state of the PerlIO * is not correct then you may get errors or warnings from when the file handle is used. So if you opened the PerlIO * as "w" it should really be an C if open as "r" it should be an C. =end original perl I<��> �Ϥ���륹�ȥ꡼��ˤĤ��ơ��������ե�����ϥ�ɥ� (�Ĥޤꡢ�����������֤ؤΥ�ե����)���������졢�󶡤��줿 PerlIO * �� ����դ����ޤ��� �⤷ PerlIO * ���ɤ߹���/�񤭹��߾��֤��������ʤ���硢�ե�����ϥ�ɥ뤬 �Ȥ�줿�Ȥ��˥��顼��ٹ������뤳�Ȥˤʤ�ޤ��� ���äơ��⤷ PerlIO * �� "w" �Ȥ��ƥ����ץ󤷤��ʤ�ºݤˤ� C �Ǥ���٤��Ǥ�����"r" �Ȥ��ƥ����ץ󤷤��ʤ� C �Ǥ���٤��Ǥ��� =begin original Now, suppose you want to use perlio layers in your XS. We'll use the perlio C function as an example. =end original �����ǡ�XS �� perlio �ؤ�Ȥ������Ȥ��ޤ��礦�� ��Τ褦�ˡ�perlio �� C �ؿ���Ȥ��ޤ��� =begin original In the C part of the XS file (above the first MODULE line) you have =end original XS �ե������ C ����ʬ (�ǽ�� MODULE �Ԥξ�) �˰ʲ��Τ褦�ˤ��ޤ� #define OutputStream PerlIO * or typedef PerlIO * OutputStream; =begin original And this is the XS code: =end original ������ XS �����ɤϤ���Ǥ�: int perlioputs(s, stream) char * s OutputStream stream CODE: RETVAL = PerlIO_puts(stream, s); OUTPUT: RETVAL =begin original We have to use a C section because C has the arguments reversed compared to C, and we want to keep the arguments the same. =end original C �� C ����Ӥ���ͽ�󤵤줿��������äƤ��ơ� ������Ʊ���ޤޤˤ������Τǡ�C ����������Ȥ�ɬ�פ�����ޤ��� =begin original Wanting to explore this thoroughly, we want to use the stdio C on a PerlIO *. This means we have to ask the perlio system for a stdio C: =end original ���������õ�����뤿��ˡ�PerlIO * �� stdio �� C ��Ȥ������Ǥ��� ����ϡ�perlio �����ƥ�� stdio �� C ���䤤��碌��ɬ�פ�����ޤ�: int perliofputs(s, stream) char * s OutputStream stream PREINIT: FILE *fp = PerlIO_findFILE(stream); CODE: if (fp != (FILE*) 0) { RETVAL = fputs(s, fp); } else { RETVAL = -1; } OUTPUT: RETVAL =begin original Note: C will search the layers for a stdio layer. If it can't find one, it will call C to generate a new stdio C. Please only call C if you want a I C. It will generate one on each call and push a new stdio layer. So don't call it repeatedly on the same file. C_findFILE will retrieve the stdio layer once it has been generated by C. =end original ����: C �� stdio �ؤ��ؤ򸡺����ޤ��� �⤷���줬���Ĥ���ʤ���С������� stdio C ���������뤿��� C ��ƤӽФ��ޤ��� I<������> C ��ɬ�פʾ��ˤΤ� C �� �ƤӽФ��褦�ˤ��Ƥ��������� ����ϸƤӽФ���˿�������Τ��������ƿ����� stdio �ؤ˥ץå��夷�ޤ��� ���äơ�Ʊ���ե�������Ф��Ʒ����֤��ƤӽФ��ʤ��Ǥ��������� C_findFILE �ϡ����� C ���������줿 stdio �ؤ� ���Ф��ޤ��� =begin original This applies to the perlio system only. For versions before 5.7, C is equivalent to C. =end original ����� perlio �����ƥ�ˤΤ�Ŭ�Ѥ���ޤ��� 5.7 �����ΥС������Ǥϡ�C �� C �� �����Ǥ��� =head2 Troubleshooting these Examples (��������Υȥ�֥륷�塼�ƥ���) =begin original As mentioned at the top of this document, if you are having problems with these example extensions, you might see if any of these help you. =end original ����ʸ��κǽ�˿��줿�褦�ˡ��⤷��������Υ������ƥ󥷥������꤬ ���ä���硢�ʲ������ͤˤʤ뤫�⤷��ޤ��� =over 4 =item * =begin original In versions of 5.002 prior to the gamma version, the test script in Example 1 will not function properly. You need to change the "use lib" line to read: =end original �åС����������ΥС������ 5.002 �Ǥϡ�L �ˤ���ƥ��� ������ץȤ�������ư��ޤ��� "use lib" �Ȥ����Ԥ�ʲ����ͤ��ѹ�����ɬ�פ�����ޤ��� use lib './blib'; =item * =begin original In versions of 5.002 prior to version 5.002b1h, the test.pl file was not automatically created by h2xs. This means that you cannot say "make test" to run the test script. You will need to add the following line before the "use extension" statement: =end original �С������ 5.002b1h �����ΥС������ 5.002 �Ǥϡ�test.pl �Ȥ����ե����뤬 h2xs �ˤ�äƼ�ư��������ޤ��� ����ϥƥ��ȥ�����ץȤ�¹Ԥ��뤿��ˡ�"make test" �Ȥ��뤳�Ȥ� �Ǥ��ʤ��Ȥ������ȤǤ��� "use extension" �Ȥ���ʸ�����˰ʲ��ιԤ��ɲä���ɬ�פ�����ޤ�: use lib './blib'; =item * =begin original In versions 5.000 and 5.001, instead of using the above line, you will need to use the following line: =end original �С������ 5.000 ����� 5.001 �Ǥϡ���Ǽ������ԤǤϤʤ����ʲ��˼������Ԥ� �Ȥ�ɬ�פ�����Ǥ��礦�� BEGIN { unshift(@INC, "./blib") } =item * =begin original This document assumes that the executable named "perl" is Perl version 5. Some systems may have installed Perl version 5 as "perl5". =end original ���Υɥ�����ȤǤϡ�"perl" �Ȥ���̾���μ¹ԥե����뤬 Perl �ΥС������ 5 �Ǥ��뤳�Ȥ��ꤷ�Ƥ��ޤ��� Perl �ΥС������ 5 �� "perl5" �Ȥ��ƥ��󥹥ȡ��뤵��Ƥ��륷���ƥ�� ���뤫�⤷��ޤ��� =back =head1 See also =begin original For more information, consult L, L, L, L, and L. =end original ���ܤ�������ϡ�L, L, L, L, L �򻲾Ȥ��Ƥ��������� =head1 Author =begin original Jeff Okamoto > =end original Jeff Okamoto > =begin original Reviewed and assisted by Dean Roehrich, Ilya Zakharevich, Andreas Koenig, and Tim Bunce. =end original Dean Roehrich, Ilya Zakharevich, Andreas Koenig, Tim Bunce �ˤ���ӥ塼�� ���Ϥ�����ޤ����� =begin original PerlIO material contributed by Lupe Christoph, with some clarification by Nick Ing-Simmons. =end original PerlIO ���Ǻ�� Lupe Christoph �ˤ�ä��󶡤��졢Nick Ing-Simmons �ˤ�ä� ���β����줿��ΤǤ��� =begin original Changes for h2xs as of Perl 5.8.x by Renee Baecker =end original Perl 5.8.x �Ǥ� h2xs �˴ؤ��Ƥ� Renee Baecker ���ѹ����ޤ����� =head2 Last Changed 2007/10/11 =begin meta Translate: KIMURA Koichi Update: Kentaro Shirakata (5.8.8-) Status: completed =end meta