=encoding euc-jp =head1 NAME =begin original perlintro -- a brief introduction and overview of Perl =end original perlintro -- Perl �γ��� =head1 DESCRIPTION =begin original This document is intended to give you a quick overview of the Perl programming language, along with pointers to further documentation. It is intended as a "bootstrap" guide for those who are new to the language, and provides just enough information for you to be able to read other peoples' Perl and understand roughly what it's doing, or write your own simple scripts. =end original ����ʸ��� Perl �ץ�����ߥ󥰸���δ�ñ�ʳ��פ������ơ����ʤ� ʸ��ؤΥݥ��󥿤򼨤����Ȥ���Ū�Ȥ��Ƥ��ޤ��� ����Ϥ��θ�����Τ�ʤ��ͤΤ���ؤΡּ����ץ����ɤ���Ū�Ȥ��Ƥ��ơ� ¾�οͤ� Perl ���ɤ�Dz��򤷤Ƥ��뤫����ޤ������򤷤��ꡢ ��ʬ���ȤǴ�ñ�ʥ�����ץȤ�񤯤��Ȥ��Ǥ���褦�ˤʤ뤿��� ��ʬ�ʾ�����󶡤��Ƥ��ޤ��� =begin original This introductory document does not aim to be complete. It does not even aim to be entirely accurate. In some cases perfection has been sacrificed in the goal of getting the general idea across. You are I advised to follow this introduction with more information from the full Perl manual, the table of contents to which can be found in L. =end original ����Ƴ��ʸ��ϴ���������Ū�ȤϤ��Ƥ��ޤ��� �������������������Ū�ˤ��Ƥ��ޤ��� ���ˤ�äƤϡ�����Ū�ʹͤ���������Ȥ���������Τ���������������ˤ��Ƥ��ޤ��� ���γ��פ��ɤ���塢������ Perl �ޥ˥奢��(�ܼ��� L �ˤ���ޤ�)���� ����ʤ��������뤳�Ȥ�I<����> ����ޤ��� =begin original Throughout this document you'll see references to other parts of the Perl documentation. You can read that documentation using the C command or whatever method you're using to read this document. =end original ����ʸ��Τ��������ˡ�¾�� Perl ʸ��Τؤλ��Ȥ�����ޤ��� ������ʸ��ϡ�C ���ޥ�ɤ䡢����ʸ����ɤि��˻ȤäƤ��� ��ˡ��Ȥä��ɤळ�Ȥ��Ǥ��ޤ��� =begin original Throughout Perl's documentation, you'll find numerous examples intended to help explain the discussed features. Please keep in mind that many of them are code fragments rather than complete programs. =end original Perl ��ʸ��Τ��������ˡ��������Ƥ��뵡ǽ������������뤳�Ȥ�տޤ��� ����������㤬����ޤ��� ������¿���ϴ����ʥץ������ǤϤʤ����������ҤǤ��뤳�Ȥ� α�դ��Ƥ��������� =begin original These examples often reflect the style and preference of the author of that piece of the documentation, and may be briefer than a corresponding line of code in a real program. Except where otherwise noted, you should assume that C and C statements appear earlier in the "program", and that any variables used have already been declared, even if those declarations have been omitted to make the example easier to read. =end original ��������Ϥ��Ф���ʸ��κ�ԤΥ�������ȹ��ߤ�ȿ�Ǥ��Ƥ��ơ� �ºݤΥץ������Ǥ��б����륳���ɤ�����ʷ餫���Τ�ޤ��� �ä˵��ҤΤʤ��¤ꡢ�֥ץ������פ����� C �� C �� ʸ�����äơ����Ƥ��ѿ��ϴ����������Ƥ���Ȳ��ꤹ��٤��Ǥ�; ���Ȥ�����ɤߤ䤹�����뤿��ˤ������������ά����Ƥ��Ƥ�Ǥ��� =begin original Do note that the examples have been written by many different authors over a period of several decades. Styles and techniques will therefore differ, although some effort has been made to not vary styles too widely in the same sections. Do not consider one style to be better than others - "There's More Than One Way To Do It" is one of Perl's mottos. After all, in your journey as a programmer, you are likely to encounter different styles. =end original ��Ͽ���ǯ�δ��֤ˤ錄�ä�¿���ΰۤʤä���Ԥˤ�äƽ񤫤�Ƥ��뤳�Ȥ� ���դ��Ƥ��������� ���äơ���������ȥƥ��˥å��ϰۤʤ�ޤ�����Ʊ����Ǥ��ޤ�ˤ� �������ۤʤä���������ˤʤ�ʤ��褦�����ϤϤ���Ƥ��ޤ��� ���륹�����뤬¾��ʪ���ͥ��Ƥ���ȹͤ��ʤ��Ǥ������� - �֤�����ϰ�Ĥ���ʤ��פ� Perl �Υ�åȡ��Ǥ��� ��ɤνꡢ�ץ�����ޤȤ��Ƥ�ι������ǡ��ۤʤä���������˽в� ���Ȥˤʤ�ޤ��� =head2 What is Perl? (Perl �äƲ�?) =begin original Perl is a general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including system administration, web development, network programming, GUI development, and more. =end original Perl �ϡ������ƥ��������Τ���˳�ȯ����ޤ����������Ǥ� �����ƥ�����������ֳ�ȯ���ͥåȥ���ץ�����ߥ󥰡�GUI ��ȯ�ʤɤ� �ޤ๭���ϰϤǻȤ��Ƥ������ѥץ�����ߥ󥰸���Ǥ��� =begin original The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). Its major features are that it's easy to use, supports both procedural and object-oriented (OO) programming, has powerful built-in support for text processing, and has one of the world's most impressive collections of third-party modules. =end original ���θ���ϡ���蘆 (�����ϡ����쥬��ȡ��Ǿ�) �ǤϤʤ��� ������ (�Ȥ��פ�����Ψ��������) ���ܻؤ��Ƥ��ޤ��� ��ʵ�ǽ�ϡ���ñ�˻Ȥ��롢��³��Ū�ȥ��֥������Ȼظ� (OO) ��ξ���� �ץ�����ߥ󥰤Ǥ��롢�ƥ����Ƚ����Τ���ζ��Ϥ��Ȥ߹��ߵ�ǽ�����롢 �����Ǥ�äȤ�ᤶ�ޤ��������ɥѡ��ƥ��⥸�塼��Υ��쥯����󤬤��롢�� ���ä����ȤǤ��� =begin original Different definitions of Perl are given in L, L and no doubt other places. From this we can determine that Perl is different things to different people, but that lots of people think it's at least worth writing about. =end original Perl �ΰۤʤä������ L �� L �ˤ��ꡢ���ä�¾�� ���ˤ⤢��ޤ��� �������顢Perl �Ͽ����ʿͤˤ�äƿ�����������Ǥ��ޤ�����¿���ο͡��� ���ʤ��Ȥ�񤯲��ͤ�����ȹͤ��Ƥ��ޤ��� =head2 Running Perl programs (Perl �ץ������μ¹�) =begin original To run a Perl program from the Unix command line: =end original Unix ���ޥ�ɥ饤�󤫤� Perl �ץ�������¹Ԥ���ˤ�: perl progname.pl =begin original Alternatively, put this as the first line of your script: =end original ���뤤�ϡ�������ץȤ���Ƭ�Ԥ˰ʲ��Τ褦�˽񤤤Ƥ�����: #!/usr/bin/env perl =begin original ... and run the script as C. Of course, it'll need to be executable first, so C (under Unix). =end original �Ĥ����ƥ�����ץȤ� C �Ȥ��Ƽ¹Ԥ��ޤ��� ������󡢤��Τ���ˤ���� (Unix �Ǥ�) C �Τ褦�ˤ��ơ� �¹Բ�ǽ�ˤ���ɬ�פ�����ޤ��� =begin original (This start line assumes you have the B program. You can also put directly the path to your perl executable, like in C<#!/usr/bin/perl>). =end original (���γ��ϹԤϡ�B �ץ�����ब���뤳�Ȥ��ꤷ�Ƥ��ޤ��� C<#!/usr/bin/perl> �Τ褦�ˡ�perl �¹ԥե�����ؤΥѥ���ľ�� �񤯤��Ȥ�Ǥ��ޤ�)�� =begin original For more information, including instructions for other platforms such as Windows and Mac OS, read L. =end original Windows �� Mac OS �Τ褦��¾�Υץ�åȥۡ���Ǥ�������ޤࡢ����ʤ� ����ˤĤ��Ƥϡ�L ���ɤ�Ǥ��������� =head2 Safety net (�����եƥ��ͥå�) =begin original Perl by default is very forgiving. In order to make it more robust it is recommended to start every program with the following lines: =end original Perl �ϥǥե���ȤǤ����˴���Ǥ��� ����ϴ�ˤ���ˤϡ����ƤΥץ���������Ƭ�˰ʲ��ιԤ�񤯤��Ȥ� �侩����Ƥ��ޤ�: #!/usr/bin/perl use strict; use warnings; =begin original The two additional lines request from perl to catch various common problems in your code. They check different things so you need both. A potential problem caught by C will cause your code to stop immediately when it is encountered, while C will merely give a warning (like the command-line switch B<-w>) and let your code run. To read more about them check their respective manual pages at L and L. =end original �ɲä��� 2 �Ԥϡ������ɤˤ����͡��ʰ���Ū���������館��褦�� perl �� �׵ᤷ�ޤ��� ���� 2 �Ԥ��̤Τ��Ȥ�����å�����Τǡ�ξ����ɬ�פǤ��� C �ˤ�ä���ª���������Ū������ϡ������ɤ�ȯ�������� ľ������ߤ��������C �� (���ޥ�ɥ饤�󥪥ץ���� B<-w> ��Ʊ��) �ٹ��Ф������ǡ������ɤϼ¹Ԥ���ޤ��� �����˴ؤ��뤵��ʤ����ϡ�L �� L �ˤ��뤽�줾��� �ޥ˥奢��ڡ���������å����Ƥ��������� =head2 Basic syntax overview (����Ū��ʸˡ�γ���) =begin original A Perl script or program consists of one or more statements. These statements are simply written in the script in a straightforward fashion. There is no need to have a C function or anything of that kind. =end original Perl ������ץȤ��뤤�ϥץ������� 1 �԰ʾ��ʸ�ǹ�������ޤ��� ������ʸ��ľ��Ū�ʷ��ǥ�����ץȤ˽񤫤�ޤ��� C �ؿ����뤤�ϻ����褦�ʤ�Τ����פǤ��� =begin original Perl statements end in a semi-colon: =end original Perl ��ʸ�ϥ��ߥ�����ǽ�ü����ޤ�: print "Hello, world"; =begin original Comments start with a hash symbol and run to the end of the line =end original �����Ȥ� # �ޡ����ǻϤޤꡢ�����ޤ�³���ޤ�: =begin original # This is a comment =end original # ����ϥ����� =begin original Whitespace is irrelevant: =end original �����̵�뤵��ޤ�: print "Hello, world" ; =begin original ... except inside quoted strings: =end original =begin original ��â���������Ȥ��줿ʸ���������㳰�Ǥ�: # this would print with a linebreak in the middle print "Hello world"; =end original # ���������˲��Ԥ����� print "Hello world"; =begin original Double quotes or single quotes may be used around literal strings: =end original ���֥륯�����Ȥ䥷�󥰥륯�����Ȥϥ�ƥ��ʸ����򤯤���Τ˻Ȥ��ޤ�: print "Hello, world"; print 'Hello, world'; =begin original However, only double quotes "interpolate" variables and special characters such as newlines (C<\n>): =end original �����������֥륯�����ȤΤߤ��ѿ��Ȳ���(C<\n>)�Τ褦���ü�ʸ���� ��Ÿ���פ��ޤ�: =begin original print "Hello, $name\n"; # works fine print 'Hello, $name\n'; # prints $name\n literally =end original print "Hello, $name\n"; # ���ޤ�ư��� print 'Hello, $name\n'; # $name\n �Ȥ���ʸ���򤽤Τޤ�ɽ������ =begin original Numbers don't need quotes around them: =end original ���ͤϥ������Ȥ���ɬ�פϤ���ޤ���: print 42; =begin original You can use parentheses for functions' arguments or omit them according to your personal taste. They are only required occasionally to clarify issues of precedence. =end original �ؿ��ΰ����Τ��ä��ϡ����ߤ��դ������ά������Ǥ��ޤ��� ɬ�פʤΤϡ�ͥ���̤���������β�����Ȥ������Ǥ��� print("Hello, world\n"); print "Hello, world\n"; =begin original More detailed information about Perl syntax can be found in L. =end original Perl ��ʸˡ�˴ؤ��뤵��˾ܺ٤ʾ���� L �ˤ���ޤ��� =head2 Perl variable types (Perl ���ѿ���) =begin original Perl has three main variable types: scalars, arrays, and hashes. =end original Perl �ˤϻ��Ĥμ���ѿ���������ޤ�: �����顢���󡢥ϥå���Ǥ��� =over 4 =item Scalars (������) =begin original A scalar represents a single value: =end original �������ñ����ͤ�ɽ�����ޤ�: my $animal = "camel"; my $answer = 42; =begin original Scalar values can be strings, integers or floating point numbers, and Perl will automatically convert between them as required. There is no need to pre-declare your variable types, but you have to declare them using the C keyword the first time you use them. (This is one of the requirements of C.) =end original �������ͤˤ�ʸ������������ư�������������ꡢPerl ��ɬ�פʤȤ��˼�ưŪ�� �������ͤ���ߤ��Ѵ����ޤ��� �ѿ�����������������ɬ�פϤ���ޤ��󤬡��ѿ���Ȥ��Ȥ��ˤ� C ������ɤ�Ȥä��������ɬ�פ�����ޤ��� (����� C ���׵᤹�뤳�Ȥΰ�ĤǤ���) =begin original Scalar values can be used in various ways: =end original �������ͤϿ�������ˡ�����ѤǤ��ޤ�: print $animal; print "The animal is $animal\n"; print "The square of $answer is ", $answer * $answer, "\n"; =begin original There are a number of "magic" scalars with names that look like punctuation or line noise. These special variables are used for all kinds of purposes, and are documented in L. The only one you need to know about for now is C<$_> which is the "default variable". It's used as the default argument to a number of functions in Perl, and it's set implicitly by certain looping constructs. =end original ������������Υ����Τ褦�˸�����̾������ä���¿���Ρ֥ޥ��å����ѿ��� ����ޤ��� �������ü��ѿ��Ϥ�������Ū�Τ�������Ѥ��졢L �� ʸ�񲽤���Ƥ��ޤ��� ���ΤȤ����ΤäƤ����٤�������ĤΤ��Ȥϡ��֥ǥե�����ѿ��פǤ��� C<$_> �Ǥ��� ����� Perl ��¿���δؿ��ǥǥե���Ȱ����Ȥ��ƻȤ�졢�����Υ롼�׹�¤�� ���ۤ����ꤵ��ޤ��� =begin original print; # prints contents of $_ by default =end original print; # �ǥե���Ȥ� $_ �����Ƥ�ɽ������ =item Arrays (����) =begin original An array represents a list of values: =end original ������ͤΥꥹ�Ȥ�ɽ�����ޤ�: my @animals = ("camel", "llama", "owl"); my @numbers = (23, 42, 69); my @mixed = ("camel", 42, 1.23); =begin original Arrays are zero-indexed. Here's how you get at elements in an array: =end original �����ź���� 0 ����Ϥޤ�ޤ��� �ʲ�����������Ǥ����������ˡ�Ǥ�: =begin original print $animals[0]; # prints "camel" print $animals[1]; # prints "llama" =end original print $animals[0]; # "camel" ��ɽ������ print $animals[1]; # "llama" ��ɽ������ =begin original The special variable C<$#array> tells you the index of the last element of an array: =end original �ü��ѿ� C<$#array> �ϡ�����κǸ�����Ǥ�ź�������֤��ޤ�: =begin original print $mixed[$#mixed]; # last element, prints 1.23 =end original print $mixed[$#mixed]; # �Ǹ�����ǤǤ��� 1.23 ��ɽ������ =begin original You might be tempted to use C<$#array + 1> to tell you how many items there are in an array. Don't bother. As it happens, using C<@array> where Perl expects to find a scalar value ("in scalar context") will give you the number of elements in the array: =end original ����ˤ������Ǥο����Τ�Τ� C<$#array + 1> ��Ȥ������ʤ뤫���Τ�ޤ��� ���ۤϤ���ޤ��� �����ˤ⡢Perl ���������ͤ����ꤷ�Ƥ���Ȥ���(�֥����饳��ƥ����ȡ�)�� C<@array> ��Ȥ��ȡ���������Ǥο����֤��ޤ�: if (@animals < 5) { ... } =begin original The elements we're getting from the array start with a C<$> because we're getting just a single value out of the array; you ask for a scalar, you get a scalar. =end original ���󤫤��Ĥ��ͤ���Ф����������ʤΤǡ���������Ǥ� C<$> �ǻϤޤ�ޤ�; ����������褦�Ȥ���С�����������ޤ��� =begin original To get multiple values from an array: =end original ���󤫤�ʣ�����ͤ�����ˤ�: =begin original @animals[0,1]; # gives ("camel", "llama"); @animals[0..2]; # gives ("camel", "llama", "owl"); @animals[1..$#animals]; # gives all except the first element =end original @animals[0,1]; # ("camel", "llama") ���֤� @animals[0..2]; # ("camel", "llama", "owl") ���֤� @animals[1..$#animals]; # �ǽ�����ǰʳ����Ƥ��֤� =begin original This is called an "array slice". =end original ����ϡ����󥹥饤���פȸƤФ�ޤ��� =begin original You can do various useful things to lists: =end original �ꥹ�Ȥ��Ф��ƿ��������ʤ��Ȥ�����ޤ�: my @sorted = sort @animals; my @backwards = reverse @numbers; =begin original There are a couple of special arrays too, such as C<@ARGV> (the command line arguments to your script) and C<@_> (the arguments passed to a subroutine). These are documented in L. =end original C<@ARGV> (������ץȤΥ��ޥ�ɥ饤�����) �� C<@_> (���֥롼������Ϥ��줿 ����) �Τ褦�ˡ��������ü�����⤢��ޤ��� ������ L ��ʸ�񲽤���Ƥ��ޤ��� =item Hashes (�ϥå���) =begin original A hash represents a set of key/value pairs: =end original �ϥå���ϥ���/�ͤ��Ȥν����ɽ�����ޤ�: my %fruit_color = ("apple", "red", "banana", "yellow"); =begin original You can use whitespace and the C<< => >> operator to lay them out more nicely: =end original ��긫�䤹�����뤿��ˡ������ C<< => >> �黻�Ҥ��Ȥ��ޤ�: my %fruit_color = ( apple => "red", banana => "yellow", ); =begin original To get at hash elements: =end original �ϥå�������Ǥ�����ˤ�: =begin original $fruit_color{"apple"}; # gives "red" =end original $fruit_color{"apple"}; # "red" ���֤� =begin original You can get at lists of keys and values with C and C. =end original �����Υꥹ�Ȥ��ͤΥꥹ�Ȥ� C �� C �������ޤ��� my @fruits = keys %fruit_colors; my @colors = values %fruit_colors; =begin original Hashes have no particular internal order, though you can sort the keys and loop through them. =end original �ϥå���ˤ��ä˷�ޤä�����Ϥ���ޤ��󤬡������򥽡��Ȥ��ơ� �����Ȥäƥ롼�פǤ��ޤ��� =begin original Just like special scalars and arrays, there are also special hashes. The most well known of these is C<%ENV> which contains environment variables. Read all about it (and other special variables) in L. =end original �ü쥹���餪��������Ʊ�͡��ü�ϥå���⤢��ޤ��� ���Τ����Ǥ�褯�Τ��Ƥ����Τϡ��Ķ��ѿ������äƤ��� C<%ENV> �Ǥ��� ��������� (����Ӥ���¾���ü��ѿ�) �� L �ˤ���ޤ��� =back =begin original Scalars, arrays and hashes are documented more fully in L. =end original �����顢���󡢥ϥå���ˤĤ��Ƥ� L �ˤ�괰���� ʸ�񲽤���Ƥ��ޤ��� =begin original More complex data types can be constructed using references, which allow you to build lists and hashes within lists and hashes. =end original ���ʣ���ʥǡ������ϡ��ꥹ�Ȥ�ϥå������˥ꥹ�Ȥ�ϥå���� ����뤳�Ȥ��Ǥ����ե���󥹤�Ȥäƹ��ۤǤ��ޤ��� =begin original A reference is a scalar value and can refer to any other Perl data type. So by storing a reference as the value of an array or hash element, you can easily create lists and hashes within lists and hashes. The following example shows a 2 level hash of hash structure using anonymous hash references. =end original ��ե���󥹤ϥ������ͤǡ�Ǥ�դ�¾�� Perl �ǡ����򻲾ȤǤ��ޤ��� ���äơ���ե���󥹤������ϥå�����ͤȤ��Ƴ�Ǽ���뤳�Ȥǡ� �ꥹ�Ȥ�ϥå������˴�ñ�˥ꥹ�Ȥ�ϥå��������Ǥ��ޤ��� �ʲ�����ϡ�̵̾�ϥå����ե���󥹤�Ȥä� 2 ��٥�Υϥå���Υϥå���� �������Ƥ��ޤ�: my $variables = { scalar => { description => "single item", sigil => '$', }, array => { description => "ordered list of items", sigil => '@', }, hash => { description => "key/value pairs", sigil => '%', }, }; print "Scalars begin with a $variables->{'scalar'}->{'sigil'}\n"; =begin original Exhaustive information on the topic of references can be found in L, L, L and L. =end original ��ե���󥹤˴ؤ������Ū�ʾ���ϡ� L, L, L, L �ˤ���ޤ��� =head2 Variable scoping (�ѿ��Υ�������) =begin original Throughout the previous section all the examples have used the syntax: =end original ���Ҥ����Ϥ����Τǡ����Ƥ���ϰʲ���ʸˡ��ȤäƤ��ޤ�: my $var = "value"; =begin original The C is actually not required; you could just use: =end original C �ϼºݤˤ�ɬ�פǤϤ���ޤ���; ñ�˰ʲ��Τ褦�ˤ�Ȥ��ޤ�: $var = "value"; =begin original However, the above usage will create global variables throughout your program, which is bad programming practice. C creates lexically scoped variables instead. The variables are scoped to the block (i.e. a bunch of statements surrounded by curly-braces) in which they are defined. =end original ����������Ҥλ���ˡ�ϥץ���������Τǥ������Х���ѿ����뤳�Ȥˤʤꡢ ����ϰ����ץ�����ߥ󥰼�ˡ�Ǥ��� ����ˡ�C �ϥ쥭������ʥ������פ���ä��ѿ�����ޤ��� �ѿ��ϡ�������줿�֥��å� (�椫�ä��ǰϤޤ줿ʸ�Τ����ޤ�) ��� �������פ�����ޤ��� =begin original my $x = "foo"; my $some_condition = 1; if ($some_condition) { my $y = "bar"; print $x; # prints "foo" print $y; # prints "bar" } print $x; # prints "foo" print $y; # prints nothing; $y has fallen out of scope =end original my $x = "foo"; my $some_condition = 1; if ($some_condition) { my $y = "bar"; print $x; # "foo" ��ɽ������ print $y; # "bar" ��ɽ������ } print $x; # "foo" ��ɽ������ print $y; # ����ɽ�����ʤ�; $y �ϥ������׳� =begin original Using C in combination with a C at the top of your Perl scripts means that the interpreter will pick up certain common programming errors. For instance, in the example above, the final C would cause a compile-time error and prevent you from running the program. Using C is highly recommended. =end original Perl ������ץȤ���Ƭ�� C ��񤯤��Ȥȹ�碌�� C �� �Ȥ����Ȥˤ�äơ����󥿥ץ꥿�������Τ褯����ץ�����ߥ󥰥ߥ��� ���ФǤ��ޤ��� �㤨�С���Ҥ���ǡ��Ǹ�� C �ϥ���ѥ��륨�顼�Ȥʤꡢ �ץ������μ¹Ԥ�˸���ޤ��� C ��Ȥ����Ȥ򶯤�����ޤ��� =head2 Conditional and looping constructs (���ȥ롼�׹�¤) =begin original Perl has most of the usual conditional and looping constructs. As of Perl 5.10, it even has a case/switch statement (spelled C/C). See L for more details. =end original Perl �ˤϡ�����Ū�ʾ�ﹽʸ�ȥ롼�׹�ʸ�ΤۤȤ�����Ƥ�����ޤ��� Perl 5.10 ���顢case/switch ʸ�⤢��ޤ� (C/C ���֤�ޤ�)�� ����ʤ�ܺ٤ˤĤ��Ƥ� L �򻲾Ȥ��Ƥ��������� =begin original The conditions can be any Perl expression. See the list of operators in the next section for information on comparison and boolean logic operators, which are commonly used in conditional statements. =end original Ǥ�դ� Perl ���Ͼ��Ȥʤ�ޤ��� ���ʸ�Ǥ褯�Ȥ��롢��ӱ黻�Ҥȿ����������黻�Ҥ˴ؤ������ˤĤ��Ƥϡ� ������ˤ���黻�Ҥΰ����򻲾Ȥ��Ƥ��������� =over 4 =item if if ( condition ) { ... } elsif ( other condition ) { ... } else { ... } =begin original There's also a negated version of it: =end original ����������Ǥ⤢��ޤ�: unless ( condition ) { ... } =begin original This is provided as a more readable version of C)>. =end original ����ϡ�C)> �����ɤߤ䤹��������ΤǤ��� =begin original Note that the braces are required in Perl, even if you've only got one line in the block. However, there is a clever way of making your one-line conditional blocks more English like: =end original ���Ȥ��֥��å���� 1 �Ԥ����ʤ��Ƥ⡢Perl �Ǥ��椫�ä���ɬ�פǤ��뤳�Ȥ� ���դ��Ƥ��������� ��������1 �Ԥξ��֥��å�����Ѹ����ˤ��뵤����������ˡ������ޤ�: =begin original # the traditional way if ($zippy) { print "Yow!"; } =end original # ����Ū����ˡ if ($zippy) { print "Yow!"; } =begin original # the Perlish post-condition way print "Yow!" if $zippy; print "We have no bananas" unless $bananas; =end original # Perl �äݤ����־�� print "Yow!" if $zippy; print "We have no bananas" unless $bananas; =item while while ( condition ) { ... } =begin original There's also a negated version, for the same reason we have C: =end original C ������Τ�Ʊ����ͳ�ǡ�����ˤ������Ǥ�����ޤ��� until ( condition ) { ... } =begin original You can also use C in a post-condition: =end original C ����־��ˤ�Ȥ��ޤ�: =begin original print "LA LA LA\n" while 1; # loops forever =end original print "LA LA LA\n" while 1; # ̵�¥롼�� =item for =begin original Exactly like C: =end original ������ C ��Ʊ�ͤǤ�: for ($i = 0; $i <= $max; $i++) { ... } =begin original The C style for loop is rarely needed in Perl since Perl provides the more friendly list scanning C loop. =end original C �����Υ롼�פ� Perl �ǤϤ�ä���ɬ�פǤϤ���ޤ���; �ʤ��ʤ� Perl �� �����ڤʥꥹ�ȥ������Ǥ��� C �롼�פ����뤫��Ǥ��� =item foreach foreach (@array) { print "This element is $_\n"; } print $list[$_] foreach 0 .. $max; =begin original # you don't have to use the default $_ either... foreach my $key (keys %hash) { print "The value of $key is $hash{$key}\n"; } =end original # �ǥե���Ȥ� $_ ��Ȥ�ɬ�פϤʤ��� foreach my $key (keys %hash) { print "The value of $key is $hash{$key}\n"; } =begin original The C keyword is actually a synonym for the C keyword. See C>. =end original C ������ɤϼºݤˤ� C ������ɤ���̾�Ǥ��� C> �򻲾Ȥ��Ƥ��������� =back =begin original For more detail on looping constructs (and some that weren't mentioned in this overview) see L. =end original �롼�׹�¤�˴ؤ��뤵��ʤ�ܺ� (����Ӥ��γ���ǿ���Ƥ��ʤ�����) �� �Ĥ��Ƥ� L �򻲾Ȥ��Ƥ��������� =head2 Builtin operators and functions (�Ȥ߹��ߤα黻�Ҥȴؿ�) =begin original Perl comes with a wide selection of builtin functions. Some of the ones we've already seen include C, C and C. A list of them is given at the start of L and you can easily read about any given function by using C>. =end original Perl ���������Ȥ߹��ߴؿ�����äƤ��ޤ��� C, C, C �ʤɡ����ΰ����ϴ��˸��Ƥ��ޤ����� ���ΰ����� L ����Ƭ�ˤ��ꡢ�ؿ��������� C> �Ȥ��뤳�ȤǴ�ñ���ɤळ�Ȥ��Ǥ��ޤ��� =begin original Perl operators are documented in full in L, but here are a few of the most common ones: =end original Perl �α黻�Ҥ� L �˴�����ʸ�񲽤���Ƥ��ޤ�������äȤ� ����Ū�ʤ�Τ򤤤��Ĥ��ʲ��˼����ޤ�: =over 4 =item Arithmetic (���ͱ黻) =begin original + addition - subtraction * multiplication / division =end original + �û� - ���� * �軻 / ���� =item Numeric comparison (�������) =begin original == equality != inequality < less than > greater than <= less than or equal >= greater than or equal =end original == ���� != ���� < ��꾮���� > ����礭�� <= �ʲ� >= �ʾ� =item String comparison (ʸ�������) =begin original eq equality ne inequality lt less than gt greater than le less than or equal ge greater than or equal =end original eq ���� ne ���� lt ��꾮���� gt ����礭�� le �ʲ� ge �ʾ� =begin original (Why do we have separate numeric and string comparisons? Because we don't have special variable types, and Perl needs to know whether to sort numerically (where 99 is less than 100) or alphabetically (where 100 comes before 99). =end original (�ɤ����ƿ�����Ӥ�ʸ������Ӥ�ʬ����Ƥ���ΤǤ��礦? �ʤ��ʤ顢���̤��ѿ����Ϥʤ��Τǡ����ͤȤ��ư��� (99 �� 100 ��꾮����) ����ʸ���Ȥ��ư��� (100 �� 99 ����) ���� Perl ���Τ餻��ɬ�פ����뤫��Ǥ�)�� =item Boolean logic (����������) && and || or ! not =begin original (C, C and C aren't just in the above table as descriptions of the operators. They're also supported as operators in their own right. They're more readable than the C-style operators, but have different precedence to C<&&> and friends. Check L for more detail.) =end original (C, C, C �ϱ黻�Ҥε��ҤȤ��ƤϾ�Ҥ�ɽ�ˤϤ���ޤ��� ����������α黻�ҤȤ����б����Ƥ��ޤ��� ������ C �����α黻�Ҥ���ɤߤ䤹���Ǥ�����C<&&> ����Ӥ�����֤Ȥ� �ۤʤä�ͥ���̤�����ޤ��� ����ʤ�ܺ٤ˤĤ��Ƥ� L �򻲾Ȥ��Ƥ���������) =item Miscellaneous (����¾) =begin original = assignment . string concatenation x string multiplication .. range operator (creates a list of numbers) =end original = ���� . ʸ������ x ʸ����軻 .. �ϰϱ黻�� (���ͤΥꥹ�Ȥ���) =back =begin original Many operators can be combined with a C<=> as follows: =end original ¿���α黻�Ҥϡ��ʲ��Τ褦�� C<=> ���Ȥ߹�碌�뤳�Ȥ��Ǥ��ޤ�: =begin original $a += 1; # same as $a = $a + 1 $a -= 1; # same as $a = $a - 1 $a .= "\n"; # same as $a = $a . "\n"; =end original $a += 1; # $a = $a + 1 ��Ʊ�� $a -= 1; # $a = $a - 1 ��Ʊ�� $a .= "\n"; # $a = $a . "\n"; ��Ʊ�� =head2 Files and I/O (�ե������ I/O) =begin original You can open a file for input or output using the C function. It's documented in extravagant detail in L and L, but in short: =end original C �ؿ���Ȥäơ����Ϥ���ϤΤ���˥ե�����򳫤��ޤ��� L �� L �����˾ܺ٤�ʸ�񲽤���Ƥ��ޤ����� ��ñ�ˤ�: open(my $in, "<", "input.txt") or die "Can't open input.txt: $!"; open(my $out, ">", "output.txt") or die "Can't open output.txt: $!"; open(my $log, ">>", "my.log") or die "Can't open my.log: $!"; =begin original You can read from an open filehandle using the C<< <> >> operator. In scalar context it reads a single line from the filehandle, and in list context it reads the whole file in, assigning each line to an element of the list: =end original C<< <> >> �黻�Ҥ�Ȥäơ������Ƥ���ե�����ϥ�ɥ뤫���ɤ߹���ޤ��� �����饳��ƥ����ȤǤϥե�����ϥ�ɥ뤫�� 1 �Ԥ��ɤ߹��ߡ��ꥹ�� ����ƥ����ȤǤϥե��������Τ��ɤ߹���ǡ����줾��ιԤ�ꥹ�Ȥ����Ǥ� �������ޤ�: my $line = <$in>; my @lines = <$in>; =begin original Reading in the whole file at one time is called slurping. It can be useful but it may be a memory hog. Most text file processing can be done a line at a time with Perl's looping constructs. =end original �ե��������Τ���٤��ɤ߹��ळ�Ȥϡֵۤ����ߡ�(slurping) �ȸƤФ�ޤ��� ����������Ǥ������������ͭ���뤫�⤷��ޤ��� �ۤȤ�ɤΥƥ����ȥե���������ϡ�Perl �Υ롼�׹�ʸ��Ȥä� 1 �Ԥ��Ľ����Ǥ��ޤ��� =begin original The C<< <> >> operator is most often seen in a C loop: =end original C<< <> >> �黻�Ҥϡ�C �롼�פ���Ǥ�äȤ�褯�����ޤ�: =begin original while (<$in>) { # assigns each line in turn to $_ print "Just read in this line: $_"; } =end original while (<$in>) { # ���줾��ιԤ� $_ ���������� print "Just read in this line: $_"; } =begin original We've already seen how to print to standard output using C. However, C can also take an optional first argument specifying which filehandle to print to: =end original C ��Ȥä�ɸ����Ϥ˽��Ϥ�����ˡ�Ϥ��Ǥ˸��Ƥ��ޤ����� ��������C �ϥ��ץ����Ȥ��ơ��ɤΥե�����ϥ�ɥ�˽��Ϥ��뤫�� ���ꤹ�����������뤳�Ȥ�Ǥ��ޤ�: print STDERR "This is your final warning.\n"; print $out $record; print $log $logmessage; =begin original When you're done with your filehandles, you should C them (though to be honest, Perl will clean up after you if you forget): =end original �ե�����ϥ�ɥ��Ȥä��顢C ��Ȥ��٤��Ǥ� (��������ľ�˸����ȡ��⤷���ʤ���˺��Ƥ� Perl �Ϥ��ʤ��θ�����򤷤ޤ�): close $in or die "$in: $!"; =head2 Regular expressions (����ɽ��) =begin original Perl's regular expression support is both broad and deep, and is the subject of lengthy documentation in L, L, and elsewhere. However, in short: =end original Perl ������ɽ�����ݡ��ȤϹ���������Ĺ��ʸ��� L, L ����Ӥɤ��ˤǤ⤢��ޤ��� ��������û��������: =over 4 =item Simple matching (ñ��ʥޥå���) =begin original if (/foo/) { ... } # true if $_ contains "foo" if ($a =~ /foo/) { ... } # true if $a contains "foo" =end original if (/foo/) { ... } # $_ �� "foo" ���ޤޤ�Ƥ���п� if ($a =~ /foo/) { ... } # $a �� "foo" ���ޤޤ�Ƥ���п� =begin original The C matching operator is documented in L. It operates on C<$_> by default, or can be bound to another variable using the C<=~> binding operator (also documented in L). =end original C �ޥå��󥰱黻�Ҥ� L ��ʸ�񲽤���Ƥ��ޤ��� ����ϥǥե���ȤǤ� C<$_> �����ޤ�����C<=~> ���黻�� (����� L ��ʸ�񲽤���Ƥ��ޤ�) ��Ȥä�¾���ѿ����Ф��� Ŭ�Ѥ��뤳�Ȥ�Ǥ��ޤ��� =item Simple substitution (ñ����ִ�) =begin original s/foo/bar/; # replaces foo with bar in $_ $a =~ s/foo/bar/; # replaces foo with bar in $a $a =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $a =end original s/foo/bar/; # $_ �ˤ��� foo �� bar ���֤������� $a =~ s/foo/bar/; # $a �ˤ��� foo �� bar ���֤������� $a =~ s/foo/bar/g; # $a �ˤ������Ƥ� foo �� bar ���֤������� =begin original The C substitution operator is documented in L. =end original C �ִ��黻�Ҥ� L ��ʸ�񲽤���Ƥ��ޤ��� =item More complex regular expressions (���ʣ��������ɽ��) =begin original You don't just have to match on fixed strings. In fact, you can match on just about anything you could dream of by using more complex regular expressions. These are documented at great length in L, but for the meantime, here's a quick cheat sheet: =end original ñ�˸��ꤷ��ʸ����˥ޥå��󥰤�����ɬ�פϤ���ޤ��� �ºݤνꡢ���ʣ��������ɽ����Ȥ����Ȥˤ�äơ��פ��դ��¤�ۤȤ�� ���ƤΤ�Τȥޥå��󥰤����뤳�Ȥ�����ޤ��� ������ L ��Ĺʸ��ʸ�񲽤���Ƥ��ޤ��������ΤȤ����ϡ��ʲ��� ����˥󥰥ڡ��ѡ��򼨤��ޤ�: =begin original . a single character \s a whitespace character (space, tab, newline, ...) \S non-whitespace character \d a digit (0-9) \D a non-digit \w a word character (a-z, A-Z, 0-9, _) \W a non-word character [aeiou] matches a single character in the given set [^aeiou] matches a single character outside the given set (foo|bar|baz) matches any of the alternatives specified =end original . ñ���ʸ�� \s ����ʸ��(���򡢥��֡����ԡ�) \S �����ʸ�� \d ���� (0-9) \D ����� \w ñ��ʸ�� (a-z, A-Z, 0-9, _) \W ��ñ��ʸ�� [aeiou] ���ꤵ�줿����˴ޤޤ��ñ���ʸ�� [^aeiou] ���ꤵ�줿����˴ޤޤ�ʤ�ñ���ʸ�� (foo|bar|baz) ���ꤵ�줿���ؤΤ����줫�˥ޥå��� =begin original ^ start of string $ end of string =end original ^ ʸ�������Ƭ $ ʸ��������� =begin original Quantifiers can be used to specify how many of the previous thing you want to match on, where "thing" means either a literal character, one of the metacharacters listed above, or a group of characters or metacharacters in parentheses. =end original �̻���Ҥ�ľ���˻��ꤷ���ޥå��󥰤���������Τ򲿲�ޥå��󥰤����������� ���ꤹ�뤿��˻Ȥ��ޤ�; �����ǡ֤�ΡפȤ����Τϥ�ƥ��� 1 ʸ���� ��Ҥ����᥿ʸ���ΰ�ġ����ä��˰Ϥޤ줿ʸ����᥿ʸ���Υ��롼�פǤ��� =begin original * zero or more of the previous thing + one or more of the previous thing ? zero or one of the previous thing {3} matches exactly 3 of the previous thing {3,6} matches between 3 and 6 of the previous thing {3,} matches 3 or more of the previous thing =end original * �����ˤ����Τ� 0 ��ʾ�ޥå��� + �����ˤ����Τ� 1 ��ʾ�ޥå��� ? �����ˤ����Τ� 1 ��ޤ��� 0 ��ޥå��� {3} �����ˤ����Τˤ��礦�� 3 ��ޥå��� {3,6} �����ˤ����Τ� 3 �󤫤� 6 ��ޥå��� {3,} �����ˤ����Τ� 3 ��ʾ�ޥå��� =begin original Some brief examples: =end original �����Ĥ��δ�ñ����Ǥ�: =begin original /^\d+/ string starts with one or more digits /^$/ nothing in the string (start and end are adjacent) /(\d\s){3}/ a three digits, each followed by a whitespace character (eg "3 4 5 ") /(a.)+/ matches a string in which every odd-numbered letter is a (eg "abacadaf") =end original /^\d+/ ��İʾ�ο����ǻϤޤ� /^$/ ��ʸ���� (��Ƭ�����������ܤ��Ƥ���) /(\d\s){3}/ 3 ��ο���(���줾��˶���ʸ��������³��) (�㤨�� "3 4 5 ") /(a.)+/ ������ܤ����� a �Ǥ���ʸ���� (�㤨�� "abacadaf") =begin original # This loop reads from STDIN, and prints non-blank lines: while (<>) { next if /^$/; print; } =end original # ���Υ롼�פ� STDIN �����ɤ߹��ߡ�����Ԥ�ɽ������: while (<>) { next if /^$/; print; } =item Parentheses for capturing (���ä�����ª) =begin original As well as grouping, parentheses serve a second purpose. They can be used to capture the results of parts of the regexp match for later use. The results end up in C<$1>, C<$2> and so on. =end original ���ä��ϡ����롼�ײ������ǤϤʤ��������Ū�����ܤ�̤����ޤ��� ����ɽ���ޥå��󥰤ΰ����η�̤��ǻȤ��������ª���뤿��˻Ȥ��ޤ��� ��̤� C<$1>, C<$2> �ʤɤ�����ޤ��� =begin original # a cheap and nasty way to break an email address up into parts =end original # e �᡼�륢�ɥ쥹��ѡ��Ĥ�ʬ�򤹤������ǰ����ˤ�����ˡ if ($email =~ /([^@]+)@(.+)/) { print "Username is $1\n"; print "Hostname is $2\n"; } =item Other regexp features (����¾������ɽ���ε�ǽ) =begin original Perl regexps also support backreferences, lookaheads, and all kinds of other complex details. Read all about them in L, L, and L. =end original Perl ������ɽ���ϡ��������ȡ��������Ȥ���Ӥ���¾�Τ���������ʣ���� �ܺ٤��б����Ƥ��ޤ��� L, L, L �Ǥ����˴ؤ������Ƥ��ɤ�Ǥ��������� =back =head2 Writing subroutines (���֥롼������) =begin original Writing subroutines is easy: =end original ���֥롼�����񤯤Τϴ�ñ�Ǥ�: sub logger { my $logmessage = shift; open my $logfile, ">>", "my.log" or die "Could not open my.log: $!"; print $logfile $logmessage; } =begin original Now we can use the subroutine just as any other built-in function: =end original ����ǥ��֥롼����򤽤�¾���Ȥ߹��ߴؿ���Ʊ�ͤ˻Ȥ��ޤ�: logger("We have a logger subroutine!"); =begin original What's that C? Well, the arguments to a subroutine are available to us as a special array called C<@_> (see L for more on that). The default argument to the C function just happens to be C<@_>. So C shifts the first item off the list of arguments and assigns it to C<$logmessage>. =end original C �ȤϤʤ�Ǥ��礦? �����äȡ����֥롼����ΰ����� C<@_> �ȸƤФ���ü�����Ȥ������Ѳ�ǽ�� �ʤ�ޤ� (����˴ؤ���ܺ٤� L �򻲾Ȥ��Ƥ�������)�� C �ؿ��Υǥե���Ȱ����� ���ޤ��� C<@_> �Ǥ��� ����ǡ�C �ϰ����Υꥹ�Ȥκǽ�Τ�Τ���Ф��ơ� C<$logmessage> ���������ޤ��� =begin original We can manipulate C<@_> in other ways too: =end original ����¾����ˡ�Ǥ� C<@_> �����Ǥ��ޤ�: =begin original my ($logmessage, $priority) = @_; # common my $logmessage = $_[0]; # uncommon, and ugly =end original my ($logmessage, $priority) = @_; # ����Ū my $logmessage = $_[0]; # ����Ū�Ǥ�ʤ�����������ʤ� =begin original Subroutines can also return values: =end original ���֥롼������ͤ��֤����Ȥ�Ǥ��ޤ�: sub square { my $num = shift; my $result = $num * $num; return $result; } =begin original Then use it like: =end original ���줫�餳���������˻Ȥ��ޤ�: $sq = square(8); =begin original For more information on writing subroutines, see L. =end original ���֥롼����ν����˴ؤ��뤵��ʤ����ˤĤ��Ƥϡ�L �� ���Ȥ��Ƥ��������� =head2 OO Perl (OO Perl) =begin original OO Perl is relatively simple and is implemented using references which know what sort of object they are based on Perl's concept of packages. However, OO Perl is largely beyond the scope of this document. Read L and L. =end original OO Perl �����Ūñ��ǡ����֥������Ȥ� Perl �γ�ǰ�Ǥ���ѥå������� �ɤ�˴�Ť��Ƥ��뤫���ΤäƤ����ե���󥹤�ȤäƼ�������Ƥ��ޤ��� ��������OO Perl �Ϥ���ʸ����ϰϤ�����礭������ޤ��� L �� L ���ɤ�Ǥ��������� =begin original As a beginning Perl programmer, your most common use of OO Perl will be in using third-party modules, which are documented below. =end original Perl �ץ�����ޤ�Ϥ��������äơ�OO Perl �κǤ����Ū�ʻ���ˡ�� �ʲ��˵��Ҥ��Ƥ��롢�����ɥѡ��ƥ����⥸�塼���Ȥ����ȤǤ��� =head2 Using Perl modules (Perl �⥸�塼���Ȥ�) =begin original Perl modules provide a range of features to help you avoid reinventing the wheel, and can be downloaded from CPAN ( http://www.cpan.org/ ). A number of popular modules are included with the Perl distribution itself. =end original Perl �⥸�塼��ϡ����ؤκ�ȯ�����򤱤뤿����͡��ʵ�ǽ���󶡤��Ƥ��ơ� CPAN ( http://www.cpan.org/ ) �������������ɤǤ��ޤ��� ¿����ͭ̾�ʥ⥸�塼��� Perl ���ۼ��Ȥ˴ޤޤ�Ƥ��ޤ��� =begin original Categories of modules range from text manipulation to network protocols to database integration to graphics. A categorized list of modules is also available from CPAN. =end original �⥸�塼��Υ��ƥ���ϡ��ƥ���������ͥåȥ���ץ��ȥ��롢 �ǡ����١������硢����ե��å��ޤ�¿�����Ϥ�ޤ��� ���ƥ����̥⥸�塼��ꥹ�Ȥ� CPAN �����Ѳ�ǽ�Ǥ��� =begin original To learn how to install modules you download from CPAN, read L. =end original CPAN �������������ɤ����⥸�塼��Υ��󥹥ȡ�����ˡ��ؤ֤ˤϡ� L ���ɤ�Ǥ��������� =begin original To learn how to use a particular module, use C>. Typically you will want to C>, which will then give you access to exported functions or an OO interface to the module. =end original ����Υ⥸�塼��λȤ�����ؤ֤ˤϡ�C> �� �ȤäƤ��������� ŵ��Ū�ˤ� C> �Ȥ��ơ����줫�饨�����ݡ��Ȥ��줿 �ؿ������⥸�塼��ؤ� OO ���󥿡��ե������˥����������ޤ��� =begin original L contains questions and answers related to many common tasks, and often provides suggestions for good CPAN modules to use. =end original L �ˤ�¿���ΰ���Ū�ʥ������˴ؤ����������������ꡢ���Ф��� �褤 CPAN �⥸�塼�����Ƥ��󶡤���Ƥ��ޤ��� =begin original L describes Perl modules in general. L lists the modules which came with your Perl installation. =end original L �ϰ���Ū�� Perl �⥸�塼��ˤĤ��Ƶ��Ҥ��Ƥ��ޤ��� L �� Perl ���󥹥ȡ������Ʊ������Ƥ���⥸�塼��ΰ����Ǥ��� =begin original If you feel the urge to write Perl modules, L will give you good advice. =end original Perl �⥸�塼����ꤿ���Ȼפä��ʤ顢L ���褤������ Ϳ���Ƥ����Ǥ��礦�� =head1 AUTHOR Kirrily "Skud" Robert =begin meta Translate: SHIRAKATA Kentaro Status: completed =end meta