=encoding euc-jp =head1 NAME =begin original perlfilter - Source Filters =end original perlfilter - �������ե��륿 =head1 DESCRIPTION =begin original This article is about a little-known feature of Perl called I. Source filters alter the program text of a module before Perl sees it, much as a C preprocessor alters the source text of a C program before the compiler sees it. This article tells you more about what source filters are, how they work, and how to write your own. =end original ���ε����ϡ��ۤȤ���Τ��Ƥ��ʤ� Perl �ε�ǽ�Ǥ��� I<�������ե��륿> �� �ؤ����ΤǤ��� C �ץ�ץ����å��� C �ץ������Υ������ƥ����Ȥ򥳥�ѥ��餬�������� �ѹ�����褦�ˡ��������ե��륿�ϥ⥸�塼��Υץ������ʸ�� Perl �� ���������ѹ����ޤ��� ���ε����ϡ��������ե��륿�Ȥϲ������ɤΤ褦��ư���Τ�����ʬ���Ȥ� �񤯤ˤϤɤ�����Ф������ˤĤ��Ƥξ�����󶡤��ޤ��� =begin original The original purpose of source filters was to let you encrypt your program source to prevent casual piracy. This isn't all they can do, as you'll soon learn. But first, the basics. =end original �������ե��륿���������Ū�ϡ������奢�����߸����ɤ�����˥ץ������ ��������Ź沽���뤿��Ǥ����� ���줫��ؤ֤褦�ˡ�����뤳�ȤϤ�������ǤϤ���ޤ��� �������ޤ��ϴ��ܤ���Ǥ��� =head1 CONCEPTS (���󥻥ץ�) =begin original Before the Perl interpreter can execute a Perl script, it must first read it from a file into memory for parsing and compilation. If that script itself includes other scripts with a C or C statement, then each of those scripts will have to be read from their respective files as well. =end original Perl ���󥿥ץ꥿�� Perl ������ץȤ�¹ԤǤ���褦�ˤ������ˡ� �ѡ����ȥ���ѥ���Τ���ˤޤ��ե�����������ɤ߹��ޤʤ���Фʤ�ޤ��� ���Υ�����ץȼ��Ȥ� C ʸ�� C ʸ��¾�Υ�����ץȤ� ���󥯥롼�ɤ��Ƥ���ʤ顢�����Υ�����ץȤ�Ʊ�ͤ˥ե����뤫���ɤ߹��� ɬ�פ�����ޤ��� =begin original Now think of each logical connection between the Perl parser and an individual file as a I. A source stream is created when the Perl parser opens a file, it continues to exist as the source code is read into memory, and it is destroyed when Perl is finished parsing the file. If the parser encounters a C or C statement in a source stream, a new and distinct stream is created just for that file. =end original �����ǡ�Perl �ѡ����ȸġ��Υե�����Ȥ�����Ū����³�� I<���������ȥ꡼��> (source stream) �Ȥ��ƹͤ��ޤ��� ���������ȥ꡼��� Perl �ѡ������ե�����򳫤����Ȥ��˺������졢 �����������ɤ�������ɤ߹��ޤ�Ƥ����¸�ߤ���Perl ���ե������ �ѡ������������Ȥ����˲�����ޤ��� �ѡ��������������ȥ꡼����� C ʸ�� C ʸ�˽в񤦤ȡ� �������ۤʤä����ȥ꡼�ब���Υե�����Τ���˺�������ޤ��� =begin original The diagram below represents a single source stream, with the flow of source from a Perl script file on the left into the Perl parser on the right. This is how Perl normally operates. =end original �ʲ��οޤ�ñ��Υ��������ȥ꡼���ɽ�����Ƥ��ơ���¦�� Perl ������ץ� �ե����뤫�鱦¦�� Perl �ѡ����ؤΥ�������ή��Ǥ��� ����� Perl �����̽���������ˡ�Ǥ��� file -------> parser =begin original There are two important points to remember: =end original �Ф��Ƥ����٤����פʥݥ���Ȥ���Ĥ���ޤ�: =over 5 =item 1. =begin original Although there can be any number of source streams in existence at any given time, only one will be active. =end original Ʊ����Ǥ�դο��Υ��������ȥ꡼�ब¸�ߤǤ��ޤ�������Ĥ����� ͭ���Ȥʤ�ޤ��� =item 2. =begin original Every source stream is associated with only one file. =end original �ƥ��������ȥ꡼��Ϥ�����ĤΥե�����ȴ�Ϣ�Ť����ޤ��� =back =begin original A source filter is a special kind of Perl module that intercepts and modifies a source stream before it reaches the parser. A source filter changes our diagram like this: =end original �������ե��륿�ϡ����������ȥ꡼�ब�ѡ������Ϥ�������ޤ��ƽ������롢 ���̤ʼ���� Perl �⥸�塼��Ǥ��� �������ե��륿�ϰʲ��Τ褦�˥������������ѹ����ޤ�: file ----> filter ----> parser =begin original If that doesn't make much sense, consider the analogy of a command pipeline. Say you have a shell script stored in the compressed file I. The simple pipeline command below runs the script without needing to create a temporary file to hold the uncompressed file. =end original ����ˤ��ޤ�Ǽ��������ʤ��ʤ顢���ޥ�ɥѥ��ץ饤����㤨�� �ͤ��ƤߤƤ��������� ���̤��줿�ե����� I ���䴰���줿�����륹����ץȤ� �ͤ��ƤߤƤ��������� ��Ҥ�ñ��ʥѥ��ץ饤�󥳥ޥ�ɤ�Ÿ�����줿�ե�������ݴɤ��뤿��� ����ե�������뤳�Ȥʤ�������ץȤ�¹Ԥ��ޤ��� gunzip -c trial.gz | sh =begin original In this case, the data flow from the pipeline can be represented as follows: =end original ���ξ�硢�ѥ��ץ饤�󤫤�Υǡ����ե����ϰʲ��Τ褦��ɽ���Ǥ��ޤ�: trial.gz ----> gunzip ----> sh =begin original With source filters, you can store the text of your script compressed and use a source filter to uncompress it for Perl's parser: =end original �������ե��륿������ȡ�������ץȤΥƥ����Ȥ򰵽̤��ơ�Perl �ѡ����Τ���� Ÿ�����륽�����ե��륿��Ȥ��ޤ�: compressed gunzip Perl program ---> source filter ---> parser =head1 USING FILTERS (�ե��륿��Ȥ�) =begin original So how do you use a source filter in a Perl script? Above, I said that a source filter is just a special kind of module. Like all Perl modules, a source filter is invoked with a use statement. =end original ����ǡ��ɤ���ä� Perl ������ץȤǥ������ե��륿��Ȥ��ΤǤ��礦? ��ˡ��������ե��륿��ñ�����̤ʼ���Υ⥸�塼��Ǥ���ȸ����ޤ����� ����¾���Ƥ� Perl �⥸�塼���Ʊ�͡��������ե��륿�� use ʸ�� ��ư����ޤ��� =begin original Say you want to pass your Perl source through the C preprocessor before execution. As it happens, the source filters distribution comes with a C preprocessor filter module called Filter::cpp. =end original Perl �Υ�������¹����� C �Υץ�ץ����å����̤������Ȥ��ޤ��� ���ޤ��ޥ������ե��륿���ۤˤ� Filter::cpp �ȸƤФ�� C �ץ�ץ����å� �ե��륿�⥸�塼�뤬�ޤޤ�Ƥ��ޤ��� =begin original Below is an example program, C, which makes use of this filter. Line numbers have been added to allow specific lines to be referenced easily. =end original �ʲ��ϡ����Υե��륿��Ȥ�����Υ���ץ�ץ������Ǥ��� C �Ǥ��� ���ֹ�ϡ�����ιԤ򻲾Ȥ��䤹�����뤿����ɲä���Ƥ��ޤ��� 1: use Filter::cpp; 2: #define TRUE 1 3: $a = TRUE; 4: print "a = $a\n"; =begin original When you execute this script, Perl creates a source stream for the file. Before the parser processes any of the lines from the file, the source stream looks like this: =end original ���Υ�����ץȤ�¹Ԥ���ȡ�Perl �Ϥ��Υե�����Τ���Υ��������ȥ꡼��� �������ޤ��� �ѡ������ե����뤫��Ԥ���������������������ȥ꡼��ϰʲ��Τ褦�� �ʤ�ޤ�: cpp_test ---------> parser =begin original Line 1, C, includes and installs the C filter module. All source filters work this way. The use statement is compiled and executed at compile time, before any more of the file is read, and it attaches the cpp filter to the source stream behind the scenes. Now the data flow looks like this: =end original 1 ���ܤ� C �ǡ�C �⥸�塼��򥤥󥯥롼�ɤ��� ���󥹥ȡ��뤷�ޤ��� ���ƤΥ������ե��륿�Ϥ��Τ褦�ˤ���ư��ޤ��� use ʸ�ϥ���ѥ��뤵��ƥ���ѥ�����ˡ��ե�����λĤ����ʬ���ɤ߹��ޤ�� ���˼¹Ԥ��졢�ظ�ǥ������ե��륿�� cpp �ե��륿�򤯤äĤ��ޤ��� �����ǥǡ����ե����ϰʲ��Τ褦�ˤʤ�ޤ�: cpp_test ----> cpp filter ----> parser =begin original As the parser reads the second and subsequent lines from the source stream, it feeds those lines through the C source filter before processing them. The C filter simply passes each line through the real C preprocessor. The output from the C preprocessor is then inserted back into the source stream by the filter. =end original �ѡ��������������ȥ꡼�फ�� 2 ���ܰʹߤ��ɤ�ˤĤ�ơ������������� C �������ե��륿���̤��ƹԤ����뤵��ޤ��� C �ե��륿��ñ�˳ƹԤ�ºݤ� C �ץ�ץ����å����̤��ޤ��� C �ץ�ץ����å�����ν��ϤϤ��줫��ե��륿�ˤ�äƥ��������ȥ꡼��� ����������ޤ��� .-> cpp --. | | | | | <-' cpp_test ----> cpp filter ----> parser =begin original The parser then sees the following code: =end original ���줫��ѡ����ϰʲ��Υ����ɤ򸫤ޤ�: use Filter::cpp; $a = 1; print "a = $a\n"; =begin original Let's consider what happens when the filtered code includes another module with use: =end original �ե��륿���줿�����ɤ� use ��Ȥä��⤦��ĤΥ⥸�塼���ޤ�Ǥ��� ���˲��������뤫��ͤ��Ƥߤޤ��礦: 1: use Filter::cpp; 2: #define TRUE 1 3: use Fred; 4: $a = TRUE; 5: print "a = $a\n"; =begin original The C filter does not apply to the text of the Fred module, only to the text of the file that used it (C). Although the use statement on line 3 will pass through the cpp filter, the module that gets included (C) will not. The source streams look like this after line 3 has been parsed and before line 4 is parsed: =end original C �ե��륿�� Fred �⥸�塼��Υƥ����Ȥˤ�Ŭ�Ѥ��줺�� �ե��륿���Ȥ��Ƥ���ե����� (C) �Υƥ����ȤˤΤ� Ŭ�Ѥ���ޤ��� 3 ���ܤ� use ʸ�� cpp �ե��륿���Ϥ���ޤ��������󥯥롼�ɤ���� �⥸�塼�� (C) ���Ϥ���ޤ��� 3 ���ܤ��ѡ������졢4 ���ܤ��ѡ�����������Υ��������ȥ꡼��� �ʲ��Τ褦�ˤʤ�ޤ�: cpp_test ---> cpp filter ---> parser (INACTIVE) Fred.pm ----> parser =begin original As you can see, a new stream has been created for reading the source from C. This stream will remain active until all of C has been parsed. The source stream for C will still exist, but is inactive. Once the parser has finished reading Fred.pm, the source stream associated with it will be destroyed. The source stream for C then becomes active again and the parser reads line 4 and subsequent lines from C. =end original ����ʬ����褦�ˡ�C ���饽�������ɤ߹��ि��ο��������ȥ꡼�ब ��������ޤ��� ���Υ��ȥ꡼��� C �������ɤ߹���ޤ�ͭ���ΤޤޤǤ��� C �Τ���Υ��������ȥ꡼���¸�ߤ����ޤޤǤ�����̵���� �ʤäƤ��ޤ��� �ѡ����� Fred.pm ������ɤ߹��ߤ�λ����ȡ�����˴�Ϣ�Ť���줿 ���������ȥ꡼����˲�����ޤ��� ���줫�� C �Τ���Υ��������ȥ꡼�ब�Ƥ�ͭ���ˤʤꡢ �ѡ����� C ���� 4 ���ܰʹߤ��ɤ߹��ߤޤ��� =begin original You can use more than one source filter on a single file. Similarly, you can reuse the same filter in as many files as you like. =end original ��ĤΥե������ʣ���Υ������ե��륿��Ȥ����Ȥ��Ǥ��ޤ��� Ʊ�ͤˡ������ʤ���¿���Υե�������Ф���Ʊ���ե��륿�� �ƻ��Ѥ��뤳�Ȥ��Ǥ��ޤ��� =begin original For example, if you have a uuencoded and compressed source file, it is possible to stack a uudecode filter and an uncompression filter like this: =end original �㤨�С�uuencode ����ư��̤���Ƥ��륽�����ե����뤬�����硢 ���Τ褦�ˤ��� uudecode �ե��륿�� uncompress �ե��륿�� �����å������뤳�Ȥ��Ǥ��ޤ�: use Filter::uudecode; use Filter::uncompress; M'XL(".H7/;1I;_>_I3=&E=%:F*I"T?22Q/ M6]9* ... =begin original Once the first line has been processed, the flow will look like this: =end original �ǽ�ιԤ����������ȡ��ե����ϰʲ��Τ褦�ˤʤ�ޤ�: file ---> uudecode ---> uncompress ---> parser filter filter =begin original Data flows through filters in the same order they appear in the source file. The uudecode filter appeared before the uncompress filter, so the source file will be uudecoded before it's uncompressed. =end original �ǡ����ϥ������ե�����˸��줿�Τ�Ʊ����ǥե��륿��ή��ޤ��� uudecode �ե��륿�� uncompress �ե��륿�����˸����Τǡ��������ե������ Ÿ����������� uudecode ����ޤ��� =head1 WRITING A SOURCE FILTER (�������ե��륿���) =begin original There are three ways to write your own source filter. You can write it in C, use an external program as a filter, or write the filter in Perl. I won't cover the first two in any great detail, so I'll get them out of the way first. Writing the filter in Perl is most convenient, so I'll devote the most space to it. =end original �ȼ��Υ������ե��륿��񤯤ˤϻ��Ĥ���ˡ������ޤ��� C �ǽ񤯤����ե��륿�Ȥ��Ƴ����ץ�������Ȥ�����Perl �ǥե��륿�� �񤯤��Ǥ��� �ǽ����ĤˤĤ��ƤϤ��ޤ�ܤ����ϵ��Ҥ��ʤ��Τǡ���ˤ����ˤĤ��� ����ޤ��� Perl �ǥե��륿��񤯤Τ����������ʤΤǡ�����˺���Υ��ڡ�����䤭�ޤ��� =head1 WRITING A SOURCE FILTER IN C (C �ǥ������ե��륿���) =begin original The first of the three available techniques is to write the filter completely in C. The external module you create interfaces directly with the source filter hooks provided by Perl. =end original ���Ѳ�ǽ�ʻ��ĤΥƥ��˥å��Τ����ΰ���ܤϡ��ե��륿������ C �� �񤯤��ȤǤ��� �������������⥸�塼��� Perl �ˤ�ä��󶡤���륽�����ե��륿�եå��� ľ����³����ޤ��� =begin original The advantage of this technique is that you have complete control over the implementation of your filter. The big disadvantage is the increased complexity required to write the filter - not only do you need to understand the source filter hooks, but you also need a reasonable knowledge of Perl guts. One of the few times it is worth going to this trouble is when writing a source scrambler. The C filter (which unscrambles the source before Perl parses it) included with the source filter distribution is an example of a C source filter (see Decryption Filters, below). =end original ���Υƥ��˥å��������ϡ��ե��륿�μ�������������Ǥ��뤳�ȤǤ��� �礭�ʼ����ϡ��ե��륿��񤯤����ɬ�פ�ʣ�������������ȤǤ� - �������ե��륿�եå��ˤĤ������򤹤�����Ǥʤ���Perl �������˴ؤ��� �������٤��μ���ɬ�פǤ��� ���κ���˸��������ͤΤ������˰��ϥ������Υ������֥벽�� �񤯤Ȥ��Ǥ��� (Perl ���ѡ����������˥������Υ������֥��������) C �ե��륿�� C �������ե��륿����Ǥ� (�ʲ��� Decryption Filters �򻲾Ȥ��Ƥ�������)�� =over 5 =item B (B<����ե��륿>) =begin original All decryption filters work on the principle of "security through obscurity." Regardless of how well you write a decryption filter and how strong your encryption algorithm is, anyone determined enough can retrieve the original source code. The reason is quite simple - once the decryption filter has decrypted the source back to its original form, fragments of it will be stored in the computer's memory as Perl parses it. The source might only be in memory for a short period of time, but anyone possessing a debugger, skill, and lots of patience can eventually reconstruct your program. =end original ���Ƥ�����ե��륿�ϡ������Ƥ��ˤ�륻�����ƥ��פθ�§��§�äƤ��ޤ��� �ɤ�������ޤ�����ե��륿��񤤤ơ��ɤ�ʤ˶����Ź沽���르�ꥺ��� �ȤäƤ⡢��ʬ�ʷ�դ�����и��Υ����������ɤ�����Ǥ��ޤ��� ������ͳ�ϤȤƤ�ñ��Ǥ� - ��ö����ե��륿���������򸵤η����᤹�ȡ����� ������ Perl ���ѡ������뤿��˥���ԥ塼���Υ�����ݴɤ���ޤ��� ��������û�����֤δ֤�����������ˤʤ����⤷��ޤ��󤬡��ǥХå��������롢 ¿���Ǧ�Ѥ�����ͤʤ�ǽ�Ū�ˤϥץ�������ƹ����Ǥ��ޤ��� =begin original That said, there are a number of steps that can be taken to make life difficult for the potential cracker. The most important: Write your decryption filter in C and statically link the decryption module into the Perl binary. For further tips to make life difficult for the potential cracker, see the file I in the source filters distribution. =end original ����Ū�ʥ���å������Ф���ʪ�����񤷤����뤿��˼�뤤���Ĥ��Υ��ƥåפ� ����ޤ��� �Ǥ���פʤΤ�: ����ե��륿�� C �ǽ񤤤ơ�����⥸�塼��� Perl �Х��ʥ�� ��Ū�˥�󥯤��뤳�ȤǤ��� ����Ū�ʥ���å������Ф���ʪ�����񤷤����뤿��˼�뤵��ʤ륹�ƥåפϡ� �������ե��륿���ۤ� I �ե�����򻲾Ȥ��Ƥ��������� =back =head1 CREATING A SOURCE FILTER AS A SEPARATE EXECUTABLE (��Ω�����¹ԥե�����Ȥ��ƥ������ե��륿�Ȥ��ƺ�������) =begin original An alternative to writing the filter in C is to create a separate executable in the language of your choice. The separate executable reads from standard input, does whatever processing is necessary, and writes the filtered data to standard output. C is an example of a source filter implemented as a separate executable - the executable is the C preprocessor bundled with your C compiler. =end original C �ǥե��륿��񤯤�������ذƤϡ����ߤθ������Ω�����¹ԥե������ ��뤳�ȤǤ��� ��Ω�����¹ԥե������ɸ����Ϥ����ɤ߹��ߡ�����ɬ�פʽ�����Ԥ��� �ե��륿���줿�ǡ�����ɸ����Ϥ˽񤭹��ߤޤ��� C �ϡ���Ω�����¹ԥե�����Ȥ��Ƽ¹Ԥ��줿�������ե��륿�� ��Ǥ� - �¹ԥե������ C ����ѥ�����դ��Ƥ��� C �ץ�ץ����å��Ǥ��� =begin original The source filter distribution includes two modules that simplify this task: C and C. Both allow you to run any external executable. Both use a coprocess to control the flow of data into and out of the external executable. (For details on coprocesses, see Stephens, W.R., "Advanced Programming in the UNIX Environment." Addison-Wesley, ISBN 0-210-56317-7, pages 441-445.) The difference between them is that C spawns the external command directly, while C spawns a shell to execute the external command. (Unix uses the Bourne shell; NT uses the cmd shell.) Spawning a shell allows you to make use of the shell metacharacters and redirection facilities. =end original �������ե��륿���ۤˤϤ��Υ��������ñ�ˤ��뤿�����ĤΥ⥸�塼�뤬 ����ޤ�: C �� C �Ǥ��� �ɤ���⳰���¹ԥե������¹Ԥ��ޤ��� �ɤ���⳰���¹ԥե�����ȤΥǡ����Τ��Ȥ�����椹��Τ˥��ץ������� �Ȥ��ޤ��� (���ץ������ξܺ٤ˤĤ��Ƥϡ�Stephens, W.R. �ˤ�� "Advanced Programming in the UNIX Environment." Addison-Wesley, ISBN 0-210-56317-7, 441-445 �ڡ��� �򻲾Ȥ��Ƥ���������) ��Ĥΰ㤤�ϡ�C �ϳ������ޥ�ɤ�ľ�ܵ�ư���ޤ����� C �ϳ������ޥ�ɤ�ư���뤿��Υ������ư���ޤ��� (Unix �� Bourne �������Ȥ��ޤ�; NT �� cmd �������Ȥ��ޤ���) �������ư���뤳�Ȥˤ�ꡢ������Υ᥿ʸ���ȥ�����쥯�ȵ����� �Ȥ���褦�ˤʤ�ޤ��� =begin original Here is an example script that uses C: =end original �ʲ��� C ��Ȥä�������ץȤ���Ǥ�: use Filter::sh 'tr XYZ PQR'; $a = 1; print "XYZ a = $a\n"; =begin original The output you'll get when the script is executed: =end original ������ץȤ��¹Ԥ��줿�Ȥ�����������Ϥ�: PQR a = 1 =begin original Writing a source filter as a separate executable works fine, but a small performance penalty is incurred. For example, if you execute the small example above, a separate subprocess will be created to run the Unix C command. Each use of the filter requires its own subprocess. If creating subprocesses is expensive on your system, you might want to consider one of the other options for creating source filters. =end original ��Ω�����¹ԥե�����Ȥ��ƥ������ե��륿��񤯤Ȥ��ޤ�ư��ޤ����� ��������ǽ��Υڥʥ�ƥ�������ޤ��� �㤨�С���Ҥξ��������¹Ԥ���ȡ�Unix �� C ���ޥ�ɤ�¹Ԥ��뤿��� �̡��Υ��֥ץ�����������ޤ��� ���줾��Υե��륿�λ��Ѥˤ��ȼ��Υ��֥ץ�������ɬ�פǤ��� ���֥����ƥ�κ����Υ����Ȥ��⤤�����ƥ�Ǥϡ��������ե��륿���뤿��� ����¾��������ͤ��������⤷��ޤ��� =head1 WRITING A SOURCE FILTER IN PERL (Perl �ǥ������ե��륿���) =begin original The easiest and most portable option available for creating your own source filter is to write it completely in Perl. To distinguish this from the previous two techniques, I'll call it a Perl source filter. =end original �ȼ��Υ������ե��륿��������뤿��Τ�äȤ��ñ�Ǥ�äȤ�ܿ����Τ��� �����ϡ������� Perl �ǽ񤯤��ȤǤ��� ��������Ҥ���ĤΥƥ��˥å��ȶ��̤��뤿��ˡ������ǤϤ���� Perl �������ե��륿�ȸƤӤޤ��� =begin original To help understand how to write a Perl source filter we need an example to study. Here is a complete source filter that performs rot13 decoding. (Rot13 is a very simple encryption scheme used in Usenet postings to hide the contents of offensive posts. It moves every letter forward thirteen places, so that A becomes N, B becomes O, and Z becomes M.) =end original Perl �������ե��륿�ν��������򤹤�Τ�����뤿��ˡ��ؽ����뤿��� �㤬ɬ�פǤ��� �ʲ��� rot13 �����Ԥ������ʥ������ե��륿�Ǥ��� (rot13 �ϡ�����Ū����Ƥ򱣤������ Usenet ��ƤǻȤ�줿�ȤƤ��ñ�� �Ź楹������Ǥ��� ����Ϥ��줾��α�ʸ���� 13 ���餷�ޤ�; ���ä� A �� N �ˡ�B �� O �ˡ� Z �� M �ˤʤ�ޤ���) package Rot13; use Filter::Util::Call; sub import { my ($type) = @_; my ($ref) = []; filter_add(bless $ref); } sub filter { my ($self) = @_; my ($status); tr/n-za-mN-ZA-M/a-zA-Z/ if ($status = filter_read()) > 0; $status; } 1; =for apidoc filter_add =for apidoc filter_read =begin original All Perl source filters are implemented as Perl classes and have the same basic structure as the example above. =end original ���Ƥ� Perl �������ե��륿�� Perl ���饹�Ȥ��Ƽ������졢��Ҥ���� Ʊ�����ܹ�¤������ޤ��� =begin original First, we include the C module, which exports a number of functions into your filter's namespace. The filter shown above uses two of these functions, C and C. =end original �ޤ���C �򥤥󥯥롼�ɤ��ơ�¿���δؿ���ե��륿�� ̾�����֤˥������ݡ��Ȥ��ޤ��� ��ҤΥե��륿�Ϥ����δؿ����� C �� C �� ��Ĥδؿ���Ȥ��ޤ��� =begin original Next, we create the filter object and associate it with the source stream by defining the C function. If you know Perl well enough, you know that C is called automatically every time a module is included with a use statement. This makes C the ideal place to both create and install a filter object. =end original ���ˡ��ե��륿���֥������Ȥ��äơ�C �ؿ���������뤳�Ȥˤ�ä� ����򥽡������ȥ꡼��ȷ�ӤĤ��ޤ��� Perl �Τ��Ȥ�ʬ�ΤäƤ���ʤ顢 C �� use ʸ�ǥ⥸�塼�뤬���󥯥롼�ɤ����ι�˼�ưŪ�� �ƤӽФ���뤳�Ȥ��ΤäƤ���Ǥ��礦�� ����ˤ�ꡢC �ϥե��륿���֥������Ȥκ����ȥ��󥹥ȡ���� ��Ŭ�ξ��ȸ����ޤ��� =begin original In the example filter, the object (C<$ref>) is blessed just like any other Perl object. Our example uses an anonymous array, but this isn't a requirement. Because this example doesn't need to store any context information, we could have used a scalar or hash reference just as well. The next section demonstrates context data. =end original ��Υե��륿�Ǥϡ����֥������� (C<$ref>) �Ϥ���¾�� Perl ���֥������Ȥ� Ʊ���褦�� bless ����ޤ��� ������Ǥ�̵̾�����ȤäƤ��ޤ����������ɬ�ܤǤϤ���ޤ��� ������Ǥ����Ƥξ�����䴰����ɬ�פ��ʤ��Τǡ��������ե���󥹤� �ϥå����ե���󥹤Ǥ�Ȥ����Ȥ�Ǥ��ޤ��� ������Ǥϥ���ƥ����ȥǡ�����޼����ޤ��� =begin original The association between the filter object and the source stream is made with the C function. This takes a filter object as a parameter (C<$ref> in this case) and installs it in the source stream. =end original �ե��륿���֥������Ȥȥ��������ȥ꡼��δ�Ϣ�դ��� C �ؿ��� �Ԥ��ޤ��� ����ϥե��륿���֥������� (����ξ��Ǥ� C<$ref>) ������˼�äơ� ����򥽡������ȥ꡼��˼���դ��ޤ��� =begin original Finally, there is the code that actually does the filtering. For this type of Perl source filter, all the filtering is done in a method called C. (It is also possible to write a Perl source filter using a closure. See the C manual page for more details.) It's called every time the Perl parser needs another line of source to process. The C method, in turn, reads lines from the source stream using the C function. =end original �Ǹ�ˡ��ºݤ˥ե��륿��󥰤�Ԥ������ɤ�����ޤ��� ���μ�� Perl �������ե��륿�Τ���ˡ��ե��륿��󥰤����Ƥ� C �ȸƤФ��᥽�åɤǹԤ��ޤ��� (�����������Ȥä� Perl �������ե��륿��񤯤��Ȥ��ǽ�Ǥ��� ����ʤ�ܺ٤ˤĤ��Ƥ� C �ޥ˥奢��ڡ����� ���Ȥ��Ƥ���������) ����� Perl �ѡ������������륽�����ιԤ�ɬ�פˤʤ��٤����ƤӽФ���ޤ��� C �᥽�åɤϡ�C �ؿ���Ȥäƥ��������ȥ꡼�फ�� ���֤˹Ԥ��ɤ߹��ߤޤ��� =begin original If a line was available from the source stream, C returns a status value greater than zero and appends the line to C<$_>. A status value of zero indicates end-of-file, less than zero means an error. The filter function itself is expected to return its status in the same way, and put the filtered line it wants written to the source stream in C<$_>. The use of C<$_> accounts for the brevity of most Perl source filters. =end original ���������ȥ꡼�फ�� 1 �Ԥ����Ѳ�ǽ�ˤʤä��顢C �� 0 ����礭�����ơ������ͤ��֤��ơ�C<$_> �˹Ԥ��ɲä��ޤ��� ���ơ������ͤ� 0 �ξ��ϥե����������򼨤���0 ��꾮�������� ���顼���̣���ޤ��� filter �ؿ����Ȥϥ��ơ�������Ʊ����ˡ���֤������������ȥ꡼��� �񤭹��ߤ����ե��륿��󥰤��줿�Ԥ� C<$_> ������뤳�Ȥ� ���ꤵ��Ƥ��ޤ��� C<$_> �λȤ����ϤۤȤ�ɤ� Perl �������ե��륿�δʷ餵���θ�� ����Ƥ��ޤ��� =begin original In order to make use of the rot13 filter we need some way of encoding the source file in rot13 format. The script below, C, does just that. =end original rot13 �ե��륿��Ȥ��ˤϡ��������ե������ rot13 ��������沽���� ��ˡ��ɬ�פǤ��� �ʲ��Υ�����ץ� C �Ϥ����Ԥ��ޤ��� die "usage mkrot13 filename\n" unless @ARGV; my $in = $ARGV[0]; my $out = "$in.tmp"; open(IN, "<$in") or die "Cannot open file $in: $!\n"; open(OUT, ">$out") or die "Cannot open file $out: $!\n"; print OUT "use Rot13;\n"; while () { tr/a-zA-Z/n-za-mN-ZA-M/; print OUT; } close IN; close OUT; unlink $in; rename $out, $in; =begin original If we encrypt this with C: =end original ����� C �ǰŹ沽�����: print " hello fred \n"; =begin original the result will be this: =end original ��̤ϰʲ��Τ褦�ˤʤ�ޤ�: use Rot13; cevag "uryyb serq\a"; =begin original Running it produces this output: =end original �����¹Ԥ���Ȱʲ��ν��Ϥ��������ޤ�: hello fred =head1 USING CONTEXT: THE DEBUG FILTER (����ƥ����Ȥ�Ȥ�: �ǥХå��ե��륿) =begin original The rot13 example was a trivial example. Here's another demonstration that shows off a few more features. =end original rot13 ����ϤĤޤ�ʤ���Ǥ����� �ʲ��ϡ��⤦�����Ĥ��ε�ǽ�򸫤��뤿�����Ǥ��� =begin original Say you wanted to include a lot of debugging code in your Perl script during development, but you didn't want it available in the released product. Source filters offer a solution. In order to keep the example simple, let's say you wanted the debugging output to be controlled by an environment variable, C. Debugging code is enabled if the variable exists, otherwise it is disabled. =end original ��ȯ��� Perl ������ץȤ����̤ΥǥХå������ɤ�ޤ�Ƥ�����������ɤ⡢ ��꡼�����ʤǤ����Ѳ�ǽ�ˤ������ʤ��Ȥ��ޤ��� �������ե��륿�����ˡ���󶡤��ޤ��� ���ñ��ʤޤޤˤ��뤿��ˡ��Ķ��ѿ� C �����椵���ǥХå����Ϥ� �ۤ����Ȥ��ޤ��� �ǥХå������ɤϡ��Ķ��ѿ���¸�ߤ����ͭ���ˤʤꡢ����ʤ���� ̵���ˤʤ�ޤ��� =begin original Two special marker lines will bracket debugging code, like this: =end original ���Τ褦�ˡ���Ĥ��ü�ʥޡ������ԤǥǥХå����륳���ɤ�Ϥߤޤ�: ## DEBUG_BEGIN if ($year > 1999) { warn "Debug: millennium bug in year $year\n"; } ## DEBUG_END =begin original The filter ensures that Perl parses the code between the and C markers only when the C environment variable exists. That means that when C does exist, the code above should be passed through the filter unchanged. The marker lines can also be passed through as-is, because the Perl parser will see them as comment lines. When C isn't set, we need a way to disable the debug code. A simple way to achieve that is to convert the lines between the two markers into comments: =end original C �Ķ��ѿ���¸�ߤ���Ȥ����ե��륿�� Perl �� C �� C �Υޡ������δ֤Υ����ɤ�����ѡ�������褦�ˤ��ޤ��� ����ˤ�ꡢC ��¸�ߤ���ȡ���ҤΥ����ɤϥե��륿���ѹ��ʤ��� �̲ᤷ���Ϥ���ޤ��� �ޡ������Ԥ⤽�Τޤ��Ϥ���ޤ�; Perl �ѡ����Ϥ����򥳥��ȹԤȤ��� ��������Ǥ��� C �����ꤵ��Ƥ��ʤ��Ȥ����ǥХå������ɤ�̵���ˤ�����ˡ�� ɬ�פˤʤ�ޤ��� �����ã�������ñ����ˡ�ϡ���ĤΥޡ������δ֤ιԤ򥳥��Ȥ� �Ѵ����뤳�ȤǤ�: ## DEBUG_BEGIN #if ($year > 1999) { # warn "Debug: millennium bug in year $year\n"; #} ## DEBUG_END =begin original Here is the complete Debug filter: =end original �ʲ��ϴ����� Debug �ե��륿�Ǥ�: package Debug; use strict; use warnings; use Filter::Util::Call; use constant TRUE => 1; use constant FALSE => 0; sub import { my ($type) = @_; my (%context) = ( Enabled => defined $ENV{DEBUG}, InTraceBlock => FALSE, Filename => (caller)[1], LineNo => 0, LastBegin => 0, ); filter_add(bless \%context); } sub Die { my ($self) = shift; my ($message) = shift; my ($line_no) = shift || $self->{LastBegin}; die "$message at $self->{Filename} line $line_no.\n" } sub filter { my ($self) = @_; my ($status); $status = filter_read(); ++ $self->{LineNo}; # deal with EOF/error first if ($status <= 0) { $self->Die("DEBUG_BEGIN has no DEBUG_END") if $self->{InTraceBlock}; return $status; } if ($self->{InTraceBlock}) { if (/^\s*##\s*DEBUG_BEGIN/ ) { $self->Die("Nested DEBUG_BEGIN", $self->{LineNo}) } elsif (/^\s*##\s*DEBUG_END/) { $self->{InTraceBlock} = FALSE; } # comment out the debug lines when the filter is disabled s/^/#/ if ! $self->{Enabled}; } elsif ( /^\s*##\s*DEBUG_BEGIN/ ) { $self->{InTraceBlock} = TRUE; $self->{LastBegin} = $self->{LineNo}; } elsif ( /^\s*##\s*DEBUG_END/ ) { $self->Die("DEBUG_END has no DEBUG_BEGIN", $self->{LineNo}); } return $status; } 1; =begin original The big difference between this filter and the previous example is the use of context data in the filter object. The filter object is based on a hash reference, and is used to keep various pieces of context information between calls to the filter function. All but two of the hash fields are used for error reporting. The first of those two, Enabled, is used by the filter to determine whether the debugging code should be given to the Perl parser. The second, InTraceBlock, is true when the filter has encountered a C line, but has not yet encountered the following C line. =end original ���Υե��륿�Ȱ�������Ȥ��礭�ʰ㤤�ϡ��ե��륿���֥������Ȥ� ����ƥ����ȥǡ����λ��ѤǤ��� �ե��륿���֥������Ȥϥϥå����ե���󥹤���äȤ��Ƥ��ơ��ե��륿�ؿ��� �ƤӽФ��֤Υ���ƥ����Ⱦ�����͡������Ҥ��ݻ����뤿��˻Ȥ��ޤ��� ��Ĥ���������ƤΥϥå���ե�����ɤϥ��顼���Τ���˻Ȥ��ޤ��� ��Ĥ�������ܤǤ��� Enabled �ϡ��ǥХå������ɤ� Perl �ѡ����� Ϳ������٤����ɤ�������ꤹ�뤿��˻Ȥ��ޤ��� �����ܤǤ��� InTraceBlock �ϡ��ե��륿�� C ��������������ɤ� �ޤ�����³�� C �Ԥ˽в�äƤ��ʤ��Ȥ��˿��Ȥʤ�ޤ��� =begin original If you ignore all the error checking that most of the code does, the essence of the filter is as follows: =end original �����ɤΤۤȤ�ɤ��ԤäƤ��륨�顼�����å������Ƥ�̵�뤹��ȡ��ե��륿�� �ܼ��ϰʲ��Τ褦�ˤʤ�ޤ�: sub filter { my ($self) = @_; my ($status); $status = filter_read(); # deal with EOF/error first return $status if $status <= 0; if ($self->{InTraceBlock}) { if (/^\s*##\s*DEBUG_END/) { $self->{InTraceBlock} = FALSE } # comment out debug lines when the filter is disabled s/^/#/ if ! $self->{Enabled}; } elsif ( /^\s*##\s*DEBUG_BEGIN/ ) { $self->{InTraceBlock} = TRUE; } return $status; } =begin original Be warned: just as the C-preprocessor doesn't know C, the Debug filter doesn't know Perl. It can be fooled quite easily: =end original �ٹ�: C �ץ�ץ����å��� C �Τ��Ȥ��Τ�ʤ��Τ�Ʊ�͡�Debug �ե��륿�� Perl �Τ��Ȥ��Τ�ޤ��� ��ñ�ˤ��ޤ��ޤ�: print < environment variable can then be used to control which blocks get included. =end original �ޤ��ϴ�ñ�ʤ�ΤǤ��� �ǥХå������ɤ򥪡��륪���ʥå��󥰤ˤ���ΤǤϤʤ����ɤΥ֥��å��� �ǥХå������ɤȤ��ƻȤ���������Ǥ���褦�ˤ���Ф�ä������Ǥ��� ���줾��ΥǥХå��֥��å����̤Ǥ���褦��ʸˡ��ʸˡ�� ��ĥ���ƤߤƤ��������� C �Ķ��ѿ������ƤϤɤΥ֥��å���Ȥ��������椹��Τ˻Ȥ��ޤ��� =begin original Once you can identify individual blocks, try allowing them to be nested. That isn't difficult either. =end original �ġ��Υ֥��å����̤Ǥ���褦�ˤʤä��顢�ͥ��ȤǤ���褦�� ���ƤߤƤ��������� ������񤷤��Ϥ���ޤ��� =begin original Here is an interesting idea that doesn't involve the Debug filter. Currently Perl subroutines have fairly limited support for formal parameter lists. You can specify the number of parameters and their type, but you still have to manually take them out of the C<@_> array yourself. Write a source filter that allows you to have a named parameter list. Such a filter would turn this: =end original ����ϥǥХå��ե��륿�˴ط��ʤ����򤤥����ǥ��Ǥ��� ���ΤȤ��� Perl ���֥롼����ϸ����ʰ����ꥹ�Ȥ˸���Ū���б����Ƥ��ޤ��� �ѥ�᡼���ο��Ȥ��η��ϻ���Ǥ��ޤ��������Ϥ� C<@_> ���󤫤���Ф� ɬ�פ�����ޤ��� ̾���դ������ꥹ�Ȥ�Ȥ���褦�ʥ������ե��륿��񤭤ޤ��礦�� ���Τ褦�ʥե��륿�ϰʲ��Τ褦�ʤ�Τ�: sub MySub ($first, $second, @rest) { ... } =begin original into this: =end original ���Τ褦���ѹ����ޤ�: sub MySub($$@) { my ($first) = shift; my ($second) = shift; my (@rest) = @_; ... } =begin original Finally, if you feel like a real challenge, have a go at writing a full-blown Perl macro preprocessor as a source filter. Borrow the useful features from the C preprocessor and any other macro processors you know. The tricky bit will be choosing how much knowledge of Perl's syntax you want your filter to have. =end original �Ǹ�ˡ�������ĩ��򹥤�ʤ顢�ܳ�Ū�� Perl �ޥ����ץ�ץ����å��� �������ե��륿�Ȥ��ƽ񤤤ƤߤƤ��������� C �ץ�ץ����å��䤽��¾�Υޥ����ץ����å����������ʵ�ǽ�� �ڤ�Ƥ��Ƥ��������� �ȥ�å����ʤȤ����ϡ�Perl ��ʸˡ�Τɤ줯�餤���μ���ե��륿�˻������뤫�� ���֤Ȥ����Ǥ��� =head1 LIMITATIONS (����) =begin original Source filters only work on the string level, thus are highly limited in its ability to change source code on the fly. It cannot detect comments, quoted strings, heredocs, it is no replacement for a real parser. The only stable usage for source filters are encryption, compression, or the byteloader, to translate binary code back to source code. =end original �������ե��륿��ʸ�����٥�Ǥ���ư��ʤ��Τǡ����ξ�ǥ����������ɤ� �Ѵ�����ǽ�ϤϤȤƤ����¤���Ƥ��ޤ��� �����ȡ��������Ȥ��줿ʸ���󡢥ҥ�ɥ�����Ȥϸ��ФǤ������ºݤ� �ѡ���������ˤϤʤ�ޤ��� �������ե��륿��ͣ��ΰ��ꤷ������ˡ�ϡ��Ź沽�����̡��Х��ȥ������Ȥ��� �Х��ʥꥳ���ɤ򥽡��������ɤ��᤹���ȤǤ��� =begin original See for example the limitations in L, which uses source filters, and thus is does not work inside a string eval, the presence of regexes with embedded newlines that are specified with raw C delimiters and don't have a modifier C are indistinguishable from code chunks beginning with the division operator C. As a workaround you must use C or C for such patterns. Also, the presence of regexes specified with raw C delimiters may cause mysterious errors. The workaround is to use C instead. See L =end original �㤨�С�L �Ǥ����¤򻲾Ȥ��Ƥ�������; ����ϡ��������ե��륿��ȤäƤ��뤿��ˡ�ʸ���� eval ����䡢 ���� C ���ڤ�ʸ���ǻ��ꤵ��� C �����Ҥ��ʤ����Ȥ߹��ߤβ��Ԥ�ޤ� ����ɽ����¸�ߤ򡢽����黻�� C �ǻϤޤ륳���ɤβ��ȶ��̤Ǥ��ޤ��� ������Ȥ��ơ����Τ褦�ʥѥ�����Ǥ� C �� C ��Ȥ� ɬ�פ�����ޤ��� �ޤ������� C ���ڤ�ʸ���ǻ��ꤵ�줿����ɽ����¸�ߤ� �Ի׵Ĥʥ��顼����������������Τ�ޤ��� ������ˡ�ϡ������ C ��Ȥ����ȤǤ��� L �򻲾Ȥ��Ƥ��������� =begin original Currently the content of the C<__DATA__> block is not filtered. =end original ���ߤΤȤ��� C<__DATA__> �֥��å���������Ƥϥե��륿��󥰤���ޤ��� =begin original Currently internal buffer lengths are limited to 32-bit only. =end original ���ߤΤȤ��������Хåե���Ĺ���� 32 �ӥåȤΤߤ����¤���Ƥ��ޤ��� =head1 THINGS TO LOOK OUT FOR (���դ���٤�����) =over 5 =item Some Filters Clobber the C Handle (�����Υե��륿�� C �ϥ�ɥ���񤭤��ޤ�) =begin original Some source filters use the C handle to read the calling program. When using these source filters you cannot rely on this handle, nor expect any particular kind of behavior when operating on it. Filters based on Filter::Util::Call (and therefore Filter::Simple) do not alter the C filehandle, but on the other hand totally ignore the text after C<__DATA__>. =end original �����Υ������ե��륿�ϡ��ƤӽФ����ץ��������ɤ߹��ि��� C �ϥ�ɥ��Ȥ��ޤ��� �����Υ������ե��륿��Ȥ��Ȥ��ˤϡ����Υϥ�ɥ�˰�¸�����ꡢ����� �����Ȥ��˲��餫������ο����񤤤�����Ǥ��ޤ��� Filter::Util::Call (���ä� Filter::Simple) ����äȤ����ե��륿�� C �ե�����ϥ�ɥ���ѹ����ޤ��󤬡������� C<__DATA__> �θ�Υƥ����Ȥϴ�����̵�뤵��ޤ��� =back =head1 REQUIREMENTS (ɬ�פʤ��) =begin original The Source Filters distribution is available on CPAN, in =end original �������ե��륿�ǥ����ȥ�ӥ塼������ CPAN �ΰʲ��������Ѳ�ǽ�Ǥ� CPAN/modules/by-module/Filter =begin original Starting from Perl 5.8 Filter::Util::Call (the core part of the Source Filters distribution) is part of the standard Perl distribution. Also included is a friendlier interface called Filter::Simple, by Damian Conway. =end original Perl 5.8 ���顢Filter::Util::Call (�������ե��륿���ۤΥ�������ʬ) �� ɸ�� Perl ���ۤΰ����Ǥ��� �ޤ���Damian Conway �ˤ����Ƥ��ߤ䤹�����󥿡��ե������Ǥ��� Filter::Simple ��ޤޤ�Ƥ��ޤ��� =head1 AUTHOR Paul Marquess EPaul.Marquess@btinternet.comE Reini Urban Erurban@cpan.orgE =head1 Copyrights The first version of this article originally appeared in The Perl Journal #11, and is copyright 1998 The Perl Journal. It appears courtesy of Jon Orwant and The Perl Journal. This document may be distributed under the same terms as Perl itself. =begin meta Translate: SHIRAKATA Kentaro Status: completed =end meta