=encoding euc-jp =head1 NAME X X X X X =begin original perlref - Perl references and nested data structures =end original perlref - Perl�Υ�ե���󥹤ȥͥ��Ȥ����ǡ�����¤ =head1 NOTE (����) =begin original This is complete documentation about all aspects of references. For a shorter, tutorial introduction to just the essential features, see L. =end original �ܥɥ�����Ȥϥ�ե���󥹤˴ؤ����ΤǤ��� ���塼�ȥꥢ��˴ؤ��Ƥ� L �򻲾Ȥ��Ƥ��������� =head1 DESCRIPTION =begin original Before release 5 of Perl it was difficult to represent complex data structures, because all references had to be symbolic--and even then it was difficult to refer to a variable instead of a symbol table entry. Perl now not only makes it easier to use symbolic references to variables, but also lets you have "hard" references to any piece of data or code. Any scalar may hold a hard reference. Because arrays and hashes contain scalars, you can now easily build arrays of arrays, arrays of hashes, hashes of arrays, arrays of hashes of functions, and so on. =end original ��꡼�� 5 ������ Perl �ǤϤ��٤ƤΥ�ե���󥹤ϥ���ܥ�å��Ǥʤ���� �ʤ�ʤ��ä�����ˡ�ʣ���ʥǡ�����¤��ɽ�����뤳�Ȥ�����Ǥ����� �����ƥ���ܥ�ơ��֥�Υ���ȥ꡼�ǤϤʤ��ѿ��򻲾Ȥ�������˾����Ȥ��� ����Ǥ��ä��ΤǤ��� Perl �ϥ���ܥ�å���ե���󥹤��ѿ��Ȥ��ƻȤ����Ȥ��ñ�ˤ��������Ǥʤ��� Ǥ�դΥǡ����ˤĤ��ơ֥ϡ��ɡץ�ե���󥹤���Ĥ��Ȥ��ǽ�Ȥ����ΤǤ��� Ǥ�դΥ�����ϥϡ��ɥ�ե���󥹤��ݻ����뤳�Ȥ��Ǥ��ޤ��� ����ȥϥå���ϥ����餫�鹽������Ƥ���Τǡ����ʤ��Ϥ��ޤ���������� �ϥå������������Υϥå��塢�ؿ��Υϥå���������������ñ�� �Ȥ�Ω�Ƥ뤳�Ȥ��Ǥ���ΤǤ��� =begin original Hard references are smart--they keep track of reference counts for you, automatically freeing the thing referred to when its reference count goes to zero. (Reference counts for values in self-referential or cyclic data structures may not go to zero without a little help; see L for a detailed explanation.) If that thing happens to be an object, the object is destructed. See L for more about objects. (In a sense, everything in Perl is an object, but we usually reserve the word for references to objects that have been officially "blessed" into a class package.) =end original �ϡ��ɥ�ե���󥹤ϸ����ºݤ˻��Ȥ���Ƥ������������Ƥ��ơ����ο��� 0 �ˤʤ�м�ưŪ�˲�����Ԥʤ��ޤ��� (���ʻ��Ȥ�۴ĥǡ���(cyclic data)�ˤ����ͤ��Ф��뻲�ȥ�����Ȥϡ� ����äȤ���������ʤ��ˤϥ����ˤϤʤ�ޤ��� �ܤ����� L �򻲾Ȥ��Ƥ���������) ���Τ�Τ����֥������ȤǤ���Ȥ��ˤϡ��ǥ��ȥ饯����ư��ޤ��� ���֥������ȤˤĤ��Ƥ��ܤ����ϡ�L �򻲾Ȥ��Ƥ��������� (�����̣�Ǥϡ�Perl �Τ��٤Ƥ����֥������ȤǤ������̾ ���饹�ѥå�������Ǹ��� "bless" ����Ƥ����ΤؤΥ�ե���󥹤��Ф��ơ� �����Ѹ���Ѥ��ޤ���) =begin original Symbolic references are names of variables or other objects, just as a symbolic link in a Unix filesystem contains merely the name of a file. The C<*glob> notation is something of a symbolic reference. (Symbolic references are sometimes called "soft references", but please don't call them that; references are confusing enough without useless synonyms.) X X X X =end original UNIX �ե����륷���ƥ��ǤΥ���ܥ�å���󥯤�ñ�˥ե������̾���� ���äƤ�������Ǥ���Τ�Ʊ�ͤˡ�����ܥ�å���ե���󥹤��ѿ��� ̾�����ݻ����ޤ��� C<*glob> �ε�ˡ�ϡ�����ܥ�å���ե���󥹤ΰ��Ǥ� (����ܥ�å���ե���󥹤ϻ��ޡȥ��եȥ�ե���󥹡ɤ� �ƤФ�ޤ��������������ƤӤ����򤷤ʤ��Ǥ��������� ��ե���󥹤�Ʊ�����Ȥ�ʤ��Ǥ��Ƥ�������򾷤��ΤǤ�����)�� X X X X =begin original In contrast, hard references are more like hard links in a Unix file system: They are used to access an underlying object without concern for what its (other) name is. When the word "reference" is used without an adjective, as in the following paragraph, it is usually talking about a hard reference. X X =end original �о�Ū�ˡ� �ϡ��ɥ�ե���󥹤�UNIX�Υե����륷���ƥ��Υϡ��ɥ�󥯤� ���Ƥ��ޤ��� �ϡ��ɥ�󥯤Ϥ��줬�ʤ��̾���Ǥ���Τ��򵤤ˤ����� ���ä�ʤ����֥������Ȥ˥����������뤿��˻Ȥ��ޤ��� �ʲ�����ˤ���褦�ˡȥ�ե���󥹡ɤȸ������դ���Ƥʤ��˻Ȥä� ��硢�̾�ϥϡ��ɥ�ե���󥹤Τ��Ȥ�ؤ��ޤ��� X X =begin original References are easy to use in Perl. There is just one overriding principle: Perl does no implicit referencing or dereferencing. When a scalar is holding a reference, it always behaves as a simple scalar. It doesn't magically start being an array or hash or subroutine; you have to tell it explicitly to do so, by dereferencing it. =end original Perl�ǥ�ե���󥹤�Ȥ��Τ����˴�ñ�Ǥ��� ��§�Υ����С��饤�ɤ���Ĥ�������Ǥ��� Perl �ϥ�ե����(referencing)��ǥ�ե����(defreferencing)�� ���ۤ˹Ԥ����ȤϤ���ޤ��� �����餬��ե���󥹤��ݻ����Ƥ���Ȥ�������Ͼ��ñ��ʥ�����Ȥ��� �����񤤤ޤ��� ���������䡢�ϥå��塢���֥롼����Ȥ��ƿ����񤦤褦�ʤ��ȤϤ���ޤ��� �ǥ�ե���󥹤򤹤뤳�Ȥˤ�äơ���ʬ������Ū�� Perl �� ������ɬ�פ�����ޤ��� =head2 Making References X X (��ե���󥹤���) =begin original References can be created in several ways. =end original ��ե���󥹤ϴ��Ĥ��Τ�����ǹ��ۤ��뤳�Ȥ��Ǥ��ޤ��� =over 4 =item 1. X<\> X =begin original By using the backslash operator on a variable, subroutine, or value. (This works much like the & (address-of) operator in C.) This typically creates I reference to a variable, because there's already a reference to the variable in the symbol table. But the symbol table reference might go away, and you'll still have the reference that the backslash returned. Here are some examples: =end original �ѿ������֥롼�����ͤ˥Хå�����å���黻�Ҥ�Ȥ����Ȥˤ�� (����� C �Ǥ� & (���ɥ쥹) �黻�Ҥ˻���Ư���򤷤ޤ�)�� ���˥���ܥ�ơ��֥�ˤ��ѿ��ؤΥ�ե���󥹤�¸�ߤ��Ƥ��ޤ����顢 ������ˡ�� I<�̤�> ��ե���󥹤����ΤǤ��� ������������ܥ�ơ��֥�Υ�ե���󥹤��ʤ��ʤä��Ȥ��Ƥ⡢ �Хå�����å��夬�֤�����ե���󥹤�¸�ߤ��뤳�Ȥˤʤ�ޤ��� ��򤤤��Ĥ������ޤ�: $scalarref = \$foo; $arrayref = \@ARGV; $hashref = \%ENV; $coderef = \&handler; $globref = \*foo; =begin original It isn't possible to create a true reference to an IO handle (filehandle or dirhandle) using the backslash operator. The most you can get is a reference to a typeglob, which is actually a complete symbol table entry. But see the explanation of the C<*foo{THING}> syntax below. However, you can still use type globs and globrefs as though they were IO handles. =end original �Хå�����å���黻�Ҥ�Ȥä� IO �ϥ�ɥ�(�ե�����ϥ�ɥ�ޤ��� �ǥ��쥯�ȥ�ϥ�ɥ�)�ؤΥ�ե���󥹤��������뤳�ȤϤǤ��ޤ��� ��äȤ��ɤ�������ե���󥹤Ϸ������֤��Ф����ե����(�ºݤˤ� ����ܥ�ơ��֥�Υ���ȥ�Ǥ���)�Ǥ��礦�� ��Ҥ��� *foo{THING} ��ʸ�������򻲾Ȥ��Ƥ��������� �������ʤ��顢IO �ϥ�ɥ�Ǥ��ä���ΤȤ��Ʒ������֤䷿�����֤� ��ե����(grobref)��Ȥ����Ȥ�Ǥ��ޤ��� =item 2. X X<[> X<[]> X X X X X =begin original A reference to an anonymous array can be created using square brackets: =end original ̾����̵������ؤΥ�ե���󥹤ϡ��礫�ä���Ȥäƺ�뤳�Ȥ��Ǥ��ޤ�: $arrayref = [1, 2, ['a', 'b', 'c']]; =begin original Here we've created a reference to an anonymous array of three elements whose final element is itself a reference to another anonymous array of three elements. (The multidimensional syntax described later can be used to access this. For example, after the above, C<< $arrayref->[2][1] >> would have the value "b".) =end original �����Ǥϡ��ء�̾����̵�� 3 �Ĥ����Ǥ��������פ�Ǹ�����ǤȤ��ƻ��� 3 �����Ǥ�̾����̵������٤ؤΥ�ե���󥹤��äƤ��ޤ��� (���ȤǽҤ٤�¿������ʸ��Ȥäơ�����򥢥��������뤳�Ȥ��Ǥ��ޤ��� ���Ȥ��С��嵭�Τ褦�ˤ�����Ǥϡ� C<< $arrayref->[2][1] >> �� "b" �Ȥʤ�ޤ���) =begin original Taking a reference to an enumerated list is not the same as using square brackets--instead it's the same as creating a list of references! =end original ���ꥹ�Ȥ��Ф����ե���󥹤���Ȥ������Ȥϡ��礫�ä���Ȥ����Ȥ� Ʊ���ǤϤ���ޤ��� ����ϥ�ե���󥹤Υꥹ�Ȥ��������뤳�Ȥ�Ʊ���ʤΤǤ�! @list = (\$a, \@b, \%c); @list = \($a, @b, %c); # same thing! =begin original As a special case, C<\(@foo)> returns a list of references to the contents of C<@foo>, not a reference to C<@foo> itself. Likewise for C<%foo>, except that the key references are to copies (since the keys are just strings rather than full-fledged scalars). =end original �ü�ʾ��Ȥ��ơ�C<\(@foo)> �� C<@foo> ���ȤؤΥ�ե���󥹤ǤϤʤ��� C<@foo> �����ƤΤ��줾��ؤΥ�ե���󥹤Υꥹ�Ȥ��֤��ޤ��� C<%foo> ��Ʊ�ͤǤ����������Υ��ԡ����Ф����ե���󥹤Ǥ������� �ۤʤ�ޤ�(����ϥ����������ʥ�����ǤϤʤ�ñ�ʤ�ʸ����Ǥ��뤿��Ǥ�)�� =item 3. X X<{> X<{}> X X X X X X =begin original A reference to an anonymous hash can be created using curly brackets: =end original ̾����̵���ϥå���ؤΥ�ե���󥹤ϡ��椫�ä���Ȥäƺ�뤳�Ȥ��Ǥ��ޤ�: $hashref = { 'Adam' => 'Eve', 'Clyde' => 'Bonnie', }; =begin original Anonymous hash and array composers like these can be intermixed freely to produce as complicated a structure as you want. The multidimensional syntax described below works for these too. The values above are literals, but variables and expressions would work just as well, because assignment operators in Perl (even within local() or my()) are executable statements, not compile-time declarations. =end original ̵̾�ϥå��������Υ��󥹥ȥ饯���ϡ�ʣ���ʹ�¤���뤿��˼�ͳ�� �Ȥ߹�碌�뤳�Ȥ��Ǥ��ޤ��� ��Ҥ���¿������ʸ�ϡ����Τ褦�ʤ�Τ��Ф��Ƥ�Ȥ����Ȥ��Ǥ��ޤ��� �嵭���ͤϥ�ƥ��Ǥ������ѿ��估�Ǥ��äƤ⤫�ޤ��ޤ��� Perl �Ǥ������黻�ϡ�(���Ȥ���local() �� my() ����Ǥ�) ����ѥ������ ���ʸ�ǤϤʤ����¹�ʸ������Ǥ��� =begin original Because curly brackets (braces) are used for several other things including BLOCKs, you may occasionally have to disambiguate braces at the beginning of a statement by putting a C<+> or a C in front so that Perl realizes the opening brace isn't starting a BLOCK. The economy and mnemonic value of using curlies is deemed worth this occasional extra hassle. =end original �椫�ä��ϡ�BLOCK ��Ϥ�Ȥ���¾�Τ��Ȥˤ�Ȥ��ޤ����顢 �������椫�ä��� BLOCK �γ��ϤǤʤ����Ȥ� Perl �˶����� �����ʸ�κǽ���椫�ä������� C<+> �� C ��Ĥ��ơ� ۣ�椵��ʤ����褦�ˤ���ɬ�פ������礬����ޤ��� ���Τ���äȤ���;ʬ�ʺ�Ȥϡ��椫�ä��λ��Ѥ˴�Ϣ���� �ºݤ˻ȤäƤߤ���ͤ�Ф��Ƥ��������β��ͤ�����Ǥ��礦�� =begin original For example, if you wanted a function to make a new hash and return a reference to it, you have these options: =end original ���Ȥ��С��������ϥå�����äơ�����ؤΥ�ե���󥹤��֤��ؿ����ߤ�����С� �ʲ��Τ褦������褬����ޤ�: sub hashem { { @_ } } # silently wrong sub hashem { +{ @_ } } # ok sub hashem { return { @_ } } # ok =begin original On the other hand, if you want the other meaning, you can do this: =end original ���ΰ����ǡ��ʲ��Τ褦�ˤ��뤳�Ȥ�Ǥ��ޤ�: sub showem { { @_ } } # ambiguous (currently ok, but may change) sub showem { {; @_ } } # ok sub showem { { return @_ } } # ok =begin original The leading C<+{> and C<{;> always serve to disambiguate the expression to mean either the HASH reference, or the BLOCK. =end original ��Ƭ�ˤ��� C<+{> �� C<{;> �������μ����ºݤˤ� �ϥå���Υ�ե���󥹤ʤΤ��֥��å��ʤΤ��� ۣ�椵�����뤿��ˤ���ޤ��� =item 4. X X X X X X X =begin original A reference to an anonymous subroutine can be created by using C without a subname: =end original ̵̾���֥롼����ΤؤΥ�ե���󥹤ϡ����֥롼����̾��̵�� C ��Ȥä� ��뤳�Ȥ��Ǥ��ޤ�: $coderef = sub { print "Boink!\n" }; =begin original Note the semicolon. Except for the code inside not being immediately executed, a C is not so much a declaration as it is an operator, like C or C. (However, no matter how many times you execute that particular line (unless you're in an C), $coderef will still have a reference to the I anonymous subroutine.) =end original ���ߥ���������դ��Ƥ��������� �����Υ����ɤ�¨�¤˼¹Ԥ����ΤǤϤʤ��Ȥ������Ȥ�����С�C �ϡ� ����Ȥ�������ष����C �� C �Τ褦�ʱ黻�ҤǤ�(�������� C ����Ǥʤ���С����󤽤ιԤ�¹Ԥ��褦�Ȥ� $coderef �ϡ�I<Ʊ���> ̵̾���֥롼������ե���󥹤��뤳�Ȥˤʤ�ޤ�)�� =begin original Anonymous subroutines act as closures with respect to my() variables, that is, variables lexically visible within the current scope. Closure is a notion out of the Lisp world that says if you define an anonymous function in a particular lexical context, it pretends to run in that context even when it's called outside the context. =end original ̵̾���֥롼����� my() �ѿ��˴�Ϣ���륯��������Τ褦�˿����񤤤ޤ��� �Ĥޤꡢ�ѿ��ϥ����ȤΥ������פ���ˤ����ƥ쥭������˸����ޤ��� ����������ϡ�̵̾�ؿ��򤢤�����Υ쥭�����륳��ƥ����Ȥ� ����������ˤ��Ȥ����Υ���ƥ����Ȥγ�¦�ǸƤӽФ��줿�Ȥ��Ƥ� ������줿�Ȥ��Υ���ƥ����ȤǼ¹Ԥ���뤳�Ȥ��׵᤹��Ȥ��� Lisp �������γ�ǰ�Ǥ��� =begin original In human terms, it's a funny way of passing arguments to a subroutine when you define it as well as when you call it. It's useful for setting up little bits of code to run later, such as callbacks. You can even do object-oriented stuff with it, though Perl already provides a different mechanism to do that--see L. =end original �狼��䤹�������С����ʤ������֥롼�����ƤӽФ��Ȥ���Ʊ���褦�ˡ� ���֥롼������������Ȥ��˰������Ϥ��Ȥ�����̯�ʤ�����Ȥ������ȤǤ��� ���ʤ��Ϥ���ǥ��֥������Ȼظ�Ū������¸����뤳�Ȥ� �Ǥ��ޤ��� ����Perl �Ϥ��Ǥˤ���Ȥϰۤʤ���Ȥߤ��󶡤��Ƥ��ޤ� -- L �򻲾Ȥ��Ƥ��������� =begin original You might also think of closure as a way to write a subroutine template without using eval(). Here's a small example of how closures work: =end original ����������� eval �ʤ��˥��֥롼����Υƥ�ץ졼�Ȥ򵭽Ҥ�����ˡ���Ȥ��� �ͤ�����Ǥ��ޤ��� �ʲ��˼����Τϡ����������㤬�ɤΤ褦��ư���Τ�����Ǥ��� sub newprint { my $x = shift; return sub { my $y = shift; print "$x, $y!\n"; }; } $h = newprint("Howdy"); $g = newprint("Greetings"); # Time passes... &$h("world"); &$g("earthlings"); =begin original This prints =end original ���ϤϤ����ʤ�ޤ��� Howdy, world! Greetings, earthlings! =begin original Note particularly that $x continues to refer to the value passed into newprint() I "my $x" having gone out of scope by the time the anonymous subroutine runs. That's what a closure is all about. =end original �������äˡ�̵̾���֥롼����μ¹Ի��ˤϡ�my $x�ɤ��������פγ��ˤ���ˤ� B<������餺>��$x �� newprint() ���Ϥ��줿�ͤ򻲾Ȥ��ĤŤ��Ƥ��뤳�Ȥ� ���դ��Ƥ��������� ���줬���������㤬���������㤿��ͳ��Ǥ��� =begin original This applies only to lexical variables, by the way. Dynamic variables continue to work as they have always worked. Closure is not something that most Perl programmers need trouble themselves about to begin with. =end original ����ϥ쥭�������ѿ��ˤΤ�Ŭ�Ѥ���ޤ��� ưŪ�ѿ��Ϥ���ޤǤ�Ʊ���褦��ư��ޤ��� ����������ϡ�����ʬ�� Perl �ץ�����ޡ��������Ȥ��Ϥ���뤿��� �ȥ�֥�򤷤礤����Ǥ��ޤ��褦����ʪ�ǤϤ���ޤ��� =item 5. X X =begin original References are often returned by special subroutines called constructors. Perl objects are just references to a special type of object that happens to know which package it's associated with. Constructors are just special subroutines that know how to create that association. They do so by starting with an ordinary reference, and it remains an ordinary reference even while it's also being an object. Constructors are often named C. You I call them indirectly: =end original ��ե���󥹤ϥ��󥹥ȥ饯���ȸƤФ�����̤ʥ��֥롼�����֤��褦�ˤ��� ���뤳�Ȥ�¿���ʤäƤ��ޤ��� Perl�Υ��֥������Ȥϡ���ʬ���ɤΥѥå������ȴط�����äƤ��뤫�� �ΤäƤ����ΤؤΥ�ե���󥹤Ǥ�������ޤ��� ���󥹥ȥ饯���ϡ����δط��դ���ɤΤ褦�˹Ԥʤ������ΤäƤ���Ȥ�����̣�ǡ� ���̤ʥ��֥롼����ˤ����ޤ��� ñ�ʤ��ե���󥹤Ȥ��ƥ������Ȥ������֥������ȤȤ����Ƥ���֤Ǥ� �̾�Υ�ե���󥹤Ǥ��뤳�Ȥ��Ѥ��Ϥ���ޤ��� ���󥹥ȥ饯���ϡ�����Ū�� C ��̾�����դ����ޤ��� ����Ū�˸ƤӽФ����Ȥ� I<��ǽ�Ǥ�>: $objref = new Doggie( Tail => 'short', Ears => 'long' ); =begin original But that can produce ambiguous syntax in certain cases, so it's often better to use the direct method invocation approach: =end original ������������Ͼ��ˤ�äƤϤ����ޤ���ʸˡ�Ȥʤ��礬����Τǡ� �����Ƥ���ľ�ܥ᥽�åɵ�ư��ˡ��Ȥ������褤�Ǥ�: $objref = Doggie->new(Tail => 'short', Ears => 'long'); use Term::Cap; $terminal = Term::Cap->Tgetent( { OSPEED => 9600 }); use Tk; $main = MainWindow->new(); $menubar = $main->Frame(-relief => "raised", -borderwidth => 2) =item 6. X =begin original References of the appropriate type can spring into existence if you dereference them in a context that assumes they exist. Because we haven't talked about dereferencing yet, we can't show you any examples yet. =end original Ŭ�ڤʷ��Υ�ե���󥹤ϡ��������ä���ե���󥹤�¸�ߤ��뤫�Τ褦��ʸ̮�� �ǥ�ե���󥹤�Ԥ��ȡ��ºݤ�¸�ߤ���褦�ˤʤ�ޤ��� �ǥ�ե���󥹤ˤĤ��ƤϤޤ��ä��Ƥ��ʤ��Τǡ��ޤ���򼨤����Ȥ��Ǥ��ޤ��� =item 7. X<*foo{THING}> X<*> =begin original A reference can be created by using a special syntax, lovingly known as the *foo{THING} syntax. *foo{THING} returns a reference to the THING slot in *foo (which is the symbol table entry which holds everything known as foo). =end original ��ե���󥹤ϡ�*foo{THING} ��ʸ�Ȥ����Τ��Ƥ��� �ü�ʹ�ʸ��Ȥä��������뤳�Ȥ��Ǥ��ޤ��� *foo{THING} �ϥ�ե���󥹤� *foo �� THING �����å�(����� foo �Ȥ����Τ������ƤΤ�Τ��ݻ����륷��ܥ�ơ��֥�Υ���ȥ�Ǥ�)�� �֤��ޤ��� $scalarref = *foo{SCALAR}; $arrayref = *ARGV{ARRAY}; $hashref = *ENV{HASH}; $coderef = *handler{CODE}; $ioref = *STDIN{IO}; $globref = *foo{GLOB}; $formatref = *foo{FORMAT}; =begin original All of these are self-explanatory except for C<*foo{IO}>. It returns the IO handle, used for file handles (L), sockets (L and L), and directory handles (L). For compatibility with previous versions of Perl, C<*foo{FILEHANDLE}> is a synonym for C<*foo{IO}>, though it is deprecated as of 5.8.0. If deprecation warnings are in effect, it will warn of its use. =end original ��������Ƥϡ�C<*foo{IO}> ������Ƽ�������Ū�Ǥ��� �ե�����ϥ�ɥ�(L)�������å�(L �� L)���ǥ��쥯�ȥ�ϥ�ɥ�(L) �Ȥ��ƻȤ��� IO �ϥ�ɥ���֤��ޤ��� ���ΥС������� Perl ���Ф���ߴ����Τ���ˡ� C<*foo{FILEHANDLE}> �� C<*foo{IO}> ��Ʊ����Ǥ�����5.8.0 ����� ��侩�Ǥ��� ��侩�ٹ�ͭ���ˤʤäƤ���С����Ѥ���ȷٹ𤬽Фޤ��� =begin original C<*foo{THING}> returns undef if that particular THING hasn't been used yet, except in the case of scalars. C<*foo{SCALAR}> returns a reference to an anonymous scalar if $foo hasn't been used yet. This might change in a future release. =end original C<*foo{THING}> �ϡ�������ξ������������� THING ���ޤ� �Ȥ��Ƥ��ʤ����ˤ� undef ���֤��ޤ��� C<*foo{SCALAR}> �ϡ�$foo ���ޤ��Ȥ��Ƥ��ʤ��ä����ˤ� ̵̾������ؤΥ�ե���󥹤��֤��ޤ��� ����Ͼ���Υ�꡼���Ǥ��Ѥ���ǽ��������ޤ��� =begin original C<*foo{IO}> is an alternative to the C<*HANDLE> mechanism given in L for passing filehandles into or out of subroutines, or storing into larger data structures. Its disadvantage is that it won't create a new filehandle for you. Its advantage is that you have less risk of clobbering more than you want to with a typeglob assignment. (It still conflates file and directory handles, though.) However, if you assign the incoming value to a scalar instead of a typeglob as we do in the examples below, there's no risk of that happening. =end original C<*foo{IO}> �ϡ�L �ˤ��� �ե�����ϥ�ɥ�򥵥֥롼������Ϥ����ꡢ���뤤�ϵդ� ���֥롼���󤫤��֤�����ˡ����뤤���礭�ʥǡ�����¤���Ǽ���� ������Ѥ����� C<*HANDLE> ���������ؤǤ��� �������������Ͽ������ϥ�ɥ�򤢤ʤ��Τ���˺��Ф����ȤϤ��ʤ��� �������ȤǤ��� ͭ�������ϡ��������֤�������Ȥ����ⵤ��Ĥ���ɬ�פ����ޤ�ʤ��Ȥ������ȤǤ��� (������������Ϥޤ��ե�����ϥ�ɥ�ȥǥ��쥯�ȥ�ϥ�ɥ��ͻ�礷�ޤ���) ���������⤷�ʲ�����ǹԤäƤ���褦�ˡ��ͤ򷿥����֤ǤϤʤ�������� ��������ȡ������뤳�Ȥ��Ф���ꥹ���Ϥ���ޤ��� splutter(*STDOUT); # pass the whole glob splutter(*STDOUT{IO}); # pass both file and dir handles sub splutter { my $fh = shift; print $fh "her um well a hmmm\n"; } $rec = get_rec(*STDIN); # pass the whole glob $rec = get_rec(*STDIN{IO}); # pass both file and dir handles sub get_rec { my $fh = shift; return scalar <$fh>; } =back =head2 Using References X X X (��ե���󥹤�Ȥ�) =begin original That's it for creating references. By now you're probably dying to know how to use references to get back to your long-lost data. There are several basic methods. =end original ��ե���󥹤κ�����Ϥ�������Ǥ��� �����ޤǤ���ȡ��ɤ������ȤΤ��ʤ��عԤäƤ��ޤä��ǡ��������᤹����ˡ� �ɤ���äƥ�ե���󥹤�Ȥ������Τꤿ�����ȤǤ��礦�� ����Ū����ˡ�������Ĥ�����ޤ��� =over 4 =item 1. =begin original Anywhere you'd put an identifier (or chain of identifiers) as part of a variable or subroutine name, you can replace the identifier with a simple scalar variable containing a reference of the correct type: =end original �ѿ�̾�䥵�֥롼����̾�ΰ����Ȥ��Ƽ��̻Ҥ��֤��Ȥ����Ǥϡ�Ŭ�ڤ� ���Υ�ե���󥹤���ä�ñ�㥹�����ѿ��Ǥ��μ��̻Ҥ� �֤������뤳�Ȥ��Ǥ��ޤ�: $bar = $$scalarref; push(@$arrayref, $filename); $$arrayref[0] = "January"; $$hashref{"KEY"} = "VALUE"; &$coderef(1,2,3); print $globref "output\n"; =begin original It's important to understand that we are specifically I dereferencing C<$arrayref[0]> or C<$hashref{"KEY"}> there. The dereference of the scalar variable happens I it does any key lookups. Anything more complicated than a simple scalar variable must use methods 2 or 3 below. However, a "simple scalar" includes an identifier that itself uses method 1 recursively. Therefore, the following prints "howdy". =end original $arrayref[0] �� $hashref{"KEY"} �Ȥ������Ǥ��Ƥ���� I<�ǤϤʤ�> ���Ȥ� ���ڤǤ��� �������ѿ��Υǥ�ե���󥹤ϡ������ʤ� key �θ������� I<����> �Ԥʤ��ޤ��� ñ�㥹�����ѿ����������ä���ΤϤ��٤ơ� �ʲ��� 2 �֤� 3 �֤���ˡ���Τ��ޤ��� �������ʤ��顢��ñ�㥹����פˤϡ����� 1 ���ܤ���ˡ��Ƶ�Ū�˻ȤäƤ��� ���̻Ҥ�ޤޤ�ޤ��� �������äơ��ʲ��Ϥϡ�"howdy" �Ƚ��Ϥ��ޤ��� $refrefref = \\\"howdy"; print $$$$refrefref; =item 2. =begin original Anywhere you'd put an identifier (or chain of identifiers) as part of a variable or subroutine name, you can replace the identifier with a BLOCK returning a reference of the correct type. In other words, the previous examples could be written like this: =end original �ѿ�̾�䥵�֥롼����̾�ΰ����Ȥ��Ƽ��̻Ҥ��֤��Ȥ����Ǥϡ�Ŭ�ڤʷ��� ��ե���󥹤��֤� BLOCK ��ȼ�����̻Ҥ��֤������뤳�Ȥ��Ǥ��ޤ��� ����������ȡ������ϰʲ��Τ褦�˽񤯤��Ȥ��Ǥ��ޤ�: $bar = ${$scalarref}; push(@{$arrayref}, $filename); ${$arrayref}[0] = "January"; ${$hashref}{"KEY"} = "VALUE"; &{$coderef}(1,2,3); $globref->print("output\n"); # iff IO::Handle is loaded =begin original Admittedly, it's a little silly to use the curlies in this case, but the BLOCK can contain any arbitrary expression, in particular, subscripted expressions: =end original �Τ��ˤ��ξ��ˤ��椫�ä����դ���ΤϤФ��������Ȥ��⤷��ޤ��󤬡� BLOCK �ˤ�Ǥ�դμ����ä�ź�����դ�����������뤳�Ȥ��Ǥ��ޤ�: &{ $dispatch{$index} }(1,2,3); # call correct routine =begin original Because of being able to omit the curlies for the simple case of C<$$x>, people often make the mistake of viewing the dereferencing symbols as proper operators, and wonder about their precedence. If they were, though, you could use parentheses instead of braces. That's not the case. Consider the difference below; case 0 is a short-hand version of case 1, I case 2: =end original ñ��� <$$x> �ξ��ˤ��椫�ä�����ά�Ǥ���Τǡ�����ܥ�� �ǥ�ե���󥹤�Ŭ���ʱ黻�ҤΤ褦�˼�����äơ�����ͥ���̤ϤɤΤ��餤���� Ǻ��ͤ�����ޤ��� ���������⤷�黻�ҤǤ���С��椫�ä�������ˡ� ���̤γ�̤��Ȥ��뤳�ȤǤ��礦�� �����ǤϤ���ޤ��� �ʲ��ΰ㤤��ͤ��ƤߤƤ��������� CASE 0 �ϡ�CASE 1 ��û��������ΤǤ��ꡢCASE 2 �� û��������ΤǤ� B<����ޤ���>: $$hashref{"KEY"} = "VALUE"; # CASE 0 ${$hashref}{"KEY"} = "VALUE"; # CASE 1 ${$hashref{"KEY"}} = "VALUE"; # CASE 2 ${$hashref->{"KEY"}} = "VALUE"; # CASE 3 =begin original Case 2 is also deceptive in that you're accessing a variable called %hashref, not dereferencing through $hashref to the hash it's presumably referencing. That would be case 3. =end original CASE 2 ��ޤ��ְ㤨�䤹����Τǡ�%hashref �Ȥ����ѿ��򥢥����������ΤǤ��� $hashref ����𤷤ơ����줬�ؤ����ȤˤʤäƤ���ϥå���� �ǥ�ե���󥹤��Ƥ����ΤǤϤ���ޤ��� ����ϡ�CASE 3 �Ǥ��� =item 3. =begin original Subroutine calls and lookups of individual array elements arise often enough that it gets cumbersome to use method 2. As a form of syntactic sugar, the examples for method 2 may be written: =end original ����θġ������Ǥ�Ȥ���礬������ȡ�2 �֤���ˡ��Ȥ��Τ� �Ѥ路���ʤäƤ��ޤ��� ��ʸ����dz����Ȥ��ƾ嵭�� 2 �Ԥϡ��ʲ��Τ褦�˽񤱤ޤ�: $arrayref->[0] = "January"; # Array element $hashref->{"KEY"} = "VALUE"; # Hash element $coderef->(1,2,3); # Subroutine call =begin original The left side of the arrow can be any expression returning a reference, including a previous dereference. Note that C<$array[$x]> is I the same thing as C<< $array->[$x] >> here: =end original ����κ�¦�ˤ���Ԥ���ǥ�ե���󥹤�ޤ�ơ���ե���󥹤��֤�Ǥ�դμ��� �񤱤ޤ�(C<$array[$x]> �ϡ�C<< $array->[$x] >> ��Ʊ���Ǥ� I<����ޤ���>)�� $array[$x]->{"foo"}->[0] = "January"; =begin original This is one of the cases we mentioned earlier in which references could spring into existence when in an lvalue context. Before this statement, C<$array[$x]> may have been undefined. If so, it's automatically defined with a hash reference so that we can look up C<{"foo"}> in it. Likewise C<< $array[$x]->{"foo"} >> will automatically get defined with an array reference so that we can look up C<[0]> in it. This process is called I. =end original ���줬����κ����ͥ���ƥ����Ȥ��Ѥ���ȥ�ե���󥹤� ¸�ߤ���褦�ˤʤ�Ȥ����������ΰ�ĤǤ��� ����ʸ�����ˤϡ�C<$array[$x]> ��̤������⤷��ޤ��� ���ξ�缫ưŪ�˥ϥå����ե���󥹤��������ơ�C<{"foo"}> �� �����Ǥ���褦�ˤʤ�ޤ��� Ʊ���褦�� C<< $array[$x]->{"foo"} >> �������ե���󥹤���������Τǡ� C<[0]> �򤽤���õ�����Ȥ��Ǥ��ޤ��� ���Υץ������� I<��ưͭ����> (autovivification) �ȸƤФ�ޤ��� =begin original One more thing here. The arrow is optional I brackets subscripts, so you can shrink the above down to =end original �⤦��ġ� ����ϡ�����դ���ź���� I<��> �Ǥϡ���ά���뤳�Ȥ��Ǥ���Τǡ� �����ϡ��ʲ��Τ褦�˽񤱤ޤ�: $array[$x]{"foo"}[0] = "January"; =begin original Which, in the degenerate case of using only ordinary arrays, gives you multidimensional arrays just like C's: =end original �̾�����������Ȥ��褦�˸��ꤹ��С����礦�� C ��¿��������Τ褦�� �Ȥ����Ȥ��Ǥ��ޤ�: $score[$x][$y][$z] += 42; =begin original Well, okay, not entirely like C's arrays, actually. C doesn't know how to grow its arrays on demand. Perl does. =end original ���������������ºݤˤ����� C �������Ʊ���Ȥ������ǤϤ���ޤ��� C �Ǥϡ�ɬ�פ˱�����������礭������ʤɤȤ������ȤϤǤ��ޤ��� Perl �Ǥϡ����줬�Ǥ��ޤ��� =item 4. =begin original If a reference happens to be a reference to an object, then there are probably methods to access the things referred to, and you should probably stick to those methods unless you're in the class package that defines the object's methods. In other words, be nice, and don't violate the object's encapsulation without a very good reason. Perl does not enforce encapsulation. We are not totalitarians here. We do expect some basic civility though. =end original ��ե���󥹤������֥������Ȥ��Ф����ΤǤ�����ˤϡ����Ȥ�����Τ� �����������뤿��Υ᥽�åɤ�����Ϥ��ǡ����֥������ȤΥ᥽�åɤ� ������륯�饹�ѥå�������Ǥʤ���С����Υ᥽�åɤ�Ȥ��褦�� ���������ɤ��Ǥ��礦�� ����������ȡ��ɼ����äơ�������������ͳ���ʤ��¤ꡢ ���֥������ȤΥ��ץ��벽��ȿ�Ťˤ��ƤϤ����ʤ��Ȥ������ȤǤ��� Perl �ϡ����ץ��벽���פ�����Ϥ��ޤ��� ��ã�ϡ����μ���ԤǤϤ���ޤ��� �������ʤˤ������δ���Ū�����٤���Ԥ��Ƥ��ޤ��� =back =begin original Using a string or number as a reference produces a symbolic reference, as explained above. Using a reference as a number produces an integer representing its storage location in memory. The only useful thing to be done with this is to compare two references numerically to see whether they refer to the same location. X =end original ʸ�������ͤ��ե���󥹤Ȥ��ƻȤ��ȡ���Ҥ����褦�˥���ܥ�å� ��ե���󥹤��������ޤ��� ��ե���󥹤���ͤȤ��ƻȤ��ȡ�������Υ��ȥ졼���ΰ��֤� ����ɽ�����������ޤ��� ��������Ѥ���������ͣ��ξ����ϡ���ĤΥ�ե���󥹤���ͤȤ��� ��Ӥ��뤳�Ȥǡ�Ʊ�����򻲾Ȥ��Ƥ��뤫�ɤ�����Ĵ�٤���Ǥ��� X if ($ref1 == $ref2) { # cheap numeric compare of references print "refs 1 and 2 refer to the same thing\n"; } =begin original Using a reference as a string produces both its referent's type, including any package blessing as described in L, as well as the numeric address expressed in hex. The ref() operator returns just the type of thing the reference is pointing to, without the address. See L for details and examples of its use. X =end original ��ե���󥹤�ʸ����Ȥ��ƻȤ��ȡ����Ȥ��Ƥ����Τη� (L �ǵ��Ҥ��Ƥ���褦�ˡ�bless ���Ƥ���ѥå�������ޤߤޤ�)�ȡ� 16 �ʿ���ɽ�����줿���ͥ��ɥ쥹��ξ�����������ޤ��� ref() �黻�Ҥϡ����ɥ쥹ȴ���ǡ���ե���󥹤������Ƥ����Τη������� �֤��ޤ��� ���λȤ����ξܺ٤���ˤĤ��Ƥ� L �򻲾Ȥ��Ƥ��������� X =begin original The bless() operator may be used to associate the object a reference points to with a package functioning as an object class. See L. =end original bless() �黻�Ҥϡ����֥������Ȥ򥪥֥������ȥ��饹�Ȥ��Ƶ�ǽ���� �ѥå������ȷ���դ��뤿��˻Ȥ����Ȥ��Ǥ��ޤ��� L �򻲾Ȥ��Ƥ��������� =begin original A typeglob may be dereferenced the same way a reference can, because the dereference syntax always indicates the type of reference desired. So C<${*foo}> and C<${\$foo}> both indicate the same scalar variable. =end original �ǥ�ե���󥹤ι�ʸ�ǤϾ����Ū�Ȥ����ե���󥹤η��� �������Ȥ��Ǥ���Τǡ��������֤��ե���󥹤�Ʊ���褦�� �ǥ�ե���󥹤��뤳�Ȥ��Ǥ��ޤ��� �ĤޤꡢC<${*foo}> �� C<${\$foo}> �ϡ��ɤ����Ʊ���������ѿ��� �������Ȥˤʤ�ޤ��� =begin original Here's a trick for interpolating a subroutine call into a string: =end original ������˼����Τϡ�ʸ����˥��֥롼����ƤӽФ���������ųݤ��Ǥ�: print "My sub returned @{[mysub(1,2,3)]} that time.\n"; =begin original The way it works is that when the C<@{...}> is seen in the double-quoted string, it's evaluated as a block. The block creates a reference to an anonymous array containing the results of the call to C. So the whole block returns a reference to an array, which is then dereferenced by C<@{...}> and stuck into the double-quoted string. This chicanery is also useful for arbitrary expressions: =end original ���֥륯�����ȤǰϤޤ줿ʸ������� C<@{...}> �����Ĥ���ȡ����� ʸ����ϥ֥��å��Ȥ���ɾ������ޤ��� �֥��å��Ǥϡ�C �θƤӽФ���¹Ԥ������η�̤��Ф��� ̵̾����ؤΥ�ե���󥹤�����ޤ��� �Ĥޤꡢ�֥��å����ΤǤϡ�����ؤΥ�ե���󥹤��֤����ȤȤʤꡢ C<@{...}> �ǥǥ�ե���󥹤��줿�塢���֥륯�����ȤǰϤޤ줿ʸ�������ˡ� �����ޤ�뤳�Ȥˤʤ�ޤ��� ���Τ��ޤ�����Ǥ�դμ��ˤ�ͭ�ѤǤ�: print "That yields @{[$n + 5]} widgets\n"; =begin original Similarly, an expression that returns a reference to a scalar can be dereferenced via C<${...}>. Thus, the above expression may be written as: =end original Ʊ�ͤˡ�������ؤΥ�ե���󥹤��֤����� C<${...}> �� �ǥ�ե���󥹤Ǥ��ޤ��� ���äơ���Ҥμ��ϰʲ��Τ褦�˽񤱤ޤ�: print "That yields ${\($n + 5)} widgets\n"; =head2 Symbolic references X X X X (����ܥ�å���ե����) =begin original We said that references spring into existence as necessary if they are undefined, but we didn't say what happens if a value used as a reference is already defined, but I a hard reference. If you use it as a reference, it'll be treated as a symbolic reference. That is, the value of the scalar is taken to be the I of a variable, rather than a direct link to a (possibly) anonymous value. =end original ��ե���󥹤Ϥ⤷̤����Ǥ����ɬ�פ˱�����¸�ߤ���褦�ˤʤ�ȸ����ޤ������� �⤷��ե���󥹤Ȥ��ƻȤ�줿�ͤ������������Ƥ����Ȥ��ˤϤɤΤ褦�� �ʤ�Τ������Ƥ��ޤ���Ǥ����� ����ϥϡ��ɥ�ե���󥹤Ǥ� I<����ޤ���>�� ��ե���󥹤Ȥ��ƻȤä��ʤ餽��ϥ���ܥ�å���ե���󥹤Ȥ��ư����ޤ��� �Ĥޤꡢ��������ͤ�(�����餯)̵̾���ͤؤ�ľ�ܤΥ�󥯤ǤϤʤ��� �ѿ��� I<̾��> �Ȥ��ư����ޤ��� =begin original People frequently expect it to work like this. So it does. =end original ���Τ褦��Ư���Ȼפ��Ƥ��뤳�Ȥ�¿����ΤǤ��� �Ǥ����餽��ư���ΤǤ��� $name = "foo"; $$name = 1; # Sets $foo ${$name} = 2; # Sets $foo ${$name x 2} = 3; # Sets $foofoo $name->[0] = 4; # Sets $foo[0] @$name = (); # Clears @foo &$name(); # Calls &foo() (as in Perl 4) $pack = "THAT"; ${"${pack}::$name"} = 5; # Sets $THAT::foo without eval =begin original This is powerful, and slightly dangerous, in that it's possible to intend (with the utmost sincerity) to use a hard reference, and accidentally use a symbolic reference instead. To protect against that, you can say =end original ����϶��Ϥǡ�����¿�������Ǥ⤢��ޤ��� (����¤����դ�Ϥ�ä�) �ϡ��ɥ�ե���󥹤�Ȥ����Ȥ������ˤ⡢ ���äƥ���ܥ�å���ե���󥹤�ȤäƤ��ޤ��褦�ʾ�礬���뤫��Ǥ��� ������ɻߤ���ˤϡ��ʲ��Τ褦�˽񤤤� use strict 'refs'; =begin original and then only hard references will be allowed for the rest of the enclosing block. An inner block may countermand that with =end original �ϤäƤ���֥��å���λĤ����ʬ�Ǥϥϡ��ɥ�ե���󥹤Τߤ� �������褦�ˤ��뤳�Ȥ��Ǥ��ޤ��� ��¦�Υ֥��å��Ǥϡ��ʲ��Τ褦�˽񤤤��Ǥ��ä����Ȥ�Ǥ��ޤ�: no strict 'refs'; =begin original Only package variables (globals, even if localized) are visible to symbolic references. Lexical variables (declared with my()) aren't in a symbol table, and thus are invisible to this mechanism. For example: =end original ����ܥ�å���ե���󥹤Ǥϡ��ѥå������ѿ�(�������벽����Ƥ����Ȥ��Ƥ� �������Х�)�����򸫤뤳�Ȥ��Ǥ��ޤ��� (my() ���������) �쥭�������ѿ��ϡ�����ܥ�ơ��֥�� ¸�ߤ��ʤ��Τǥ���ܥ�å���ե���󥹤Ǥϻ��Ȥ��뤳�Ȥ��Ǥ��ޤ��� ���Ȥ���: local $value = 10; $ref = "value"; { my $value = 20; print $$ref; } =begin original This will still print 10, not 20. Remember that local() affects package variables, which are all "global" to the package. =end original ����ϡ�20 �ǤϤʤ���10 �Ƚ��Ϥ��ޤ��� local() �ϡ��ѥå������ǡ֥������Х�ʡס��ѥå������ѿ��� �ƶ������ΤǤ��� =head2 Not-so-symbolic references (���ޤꥷ��ܥ�å��ǤϤʤ���ե����) =begin original A new feature contributing to readability in perl version 5.001 is that the brackets around a symbolic reference behave more like quotes, just as they always have within a string. That is, =end original perl �ΥС������ 5.001 �ǡ��椫�ä��˰Ϥޤ줿����ܥ�å���ե���󥹤� ��ꥯ�����ȤΤ褦�ˡ����礦�ɤ��줬ʸ�������ˤ��뤫�Τ褦�� �����񤦤Ȥ��������ʵ�ǽ���ɤߤ䤹���Τ�����ɲä���ޤ����� ����ϡ� $push = "pop on "; print "${push}over"; =begin original has always meant to print "pop on over", even though push is a reserved word. This has been generalized to work the same outside of quotes, so that =end original �Ȥ���� push ��ͽ���Ǥ���ˤ�ؤ餺��ˡ�pop on over�ɤȤʤ�ޤ��� ����ϥ������Ȥγ��Ǥ��äƤ�Ʊ�ͤǤ��� �Ǥ����顢 print ${push} . "over"; =begin original and even =end original �� print ${ push } . "over"; =begin original will have the same effect. (This would have been a syntax error in Perl 5.000, though Perl 4 allowed it in the spaceless form.) This construct is I considered to be a symbolic reference when you're using strict refs: =end original �Ǥ�����Ʊ�����̤Ȥʤ�ޤ��� (����� Perl 4 �� ����̵���η���������Ƥ���ˤ�ؤ餺�� Perl 5.000 �ǤϹ�ʸ���顼�Ȥʤ�ޤ�)�� ���ι�¤�Ϥ��ʤ��� strict refs ��ȤäƤ���Ȥ��ˡ� ����ܥ�å���ե���󥹤Ǥ��뤳�Ȥ��θ I<���Ƥ��ޤ���>�� use strict 'refs'; ${ bareword }; # Okay, means $bareword. ${ "bareword" }; # Error, symbolic reference. =begin original Similarly, because of all the subscripting that is done using single words, we've applied the same rule to any bareword that is used for subscripting a hash. So now, instead of writing =end original Ʊ�ͤˡ�ź�����դ��Τ��٤Ƥ�ñ��θ�ǹԤ��Ƥ���Τǡ� Ʊ���롼���ϥå�����Ф���ź�����դ��˻Ȥ��� Ǥ�դ����ñ���Ŭ�Ѥ��ޤ��� �Ǥ����顢 $array{ "aaa" }{ "bbb" }{ "ccc" } =begin original you can write just =end original ������˰ʲ��Τ褦�˽񤱤�: $array{ aaa }{ bbb }{ ccc } =begin original and not worry about whether the subscripts are reserved words. In the rare event that you do wish to do something like =end original ������ź������ͽ���Ǥ��뤫�ɤ������ۤ��뤳�ȤϤ���ޤ��� �ʲ��Τ褦�ʤ��Ȥ��������Ȥ������������ˤ�: $array{ shift } =begin original you can force interpretation as a reserved word by adding anything that makes it more than a bareword: =end original ���ñ��Ǥʤ��褦�ˤ����벿�����ɲä��뤳�Ȥǡ�ͽ���Ǥ���褦�� ����Ū�˲�ᤵ���뤳�Ȥ��Ǥ��ޤ�: $array{ shift() } $array{ +shift } $array{ shift @_ } =begin original The C pragma or the B<-w> switch will warn you if it interprets a reserved word as a string. But it will no longer warn you about using lowercase words, because the string is effectively quoted. =end original C �ץ饰�ޤ� B<-w> �����å���ʸ����ͽ���Ȥ��� ��ᤵ�줿�Ȥ��ˤϷٹ��ȯ���ޤ��� �����������ʤ�����ʸ����������ʤ�ñ���Ȥä����ˤϤ�Ϥ�ٹ𤵤�ޤ��� �ʤ��ʤ�ʸ����ϻ��¾奯�����Ȥ���Ƥ��뤫��Ǥ��� =head2 Pseudo-hashes: Using an array as a hash X X X (�����ϥå���: �����ϥå���Ȥ��ƻȤ�) =begin original Pseudo-hashes have been removed from Perl. The 'fields' pragma remains available. =end original �����ϥå���� Perl ����������ޤ����� 'fields' �ץ饰�ޤϤޤ����Ѳ�ǽ�Ǥ��� =head2 Function Templates X X X X X X X X (�ؿ��ƥ�ץ졼��) =begin original As explained above, an anonymous function with access to the lexical variables visible when that function was compiled, creates a closure. It retains access to those variables even though it doesn't get run until later, such as in a signal handler or a Tk callback. =end original ������������褦�ˡ��ؿ�������ѥ��뤵�줿�Ȥ��˲Ļ�ʥ쥭�������ѿ��� ������������̵̾�ؿ��ϥ������������ޤ��� �������ä��ѿ����Ф��륢�������ϡ������ʥ�ϥ�ɥ�� Tk �� ������Хå��Τ褦�ˡ����ȤǼ¹Ԥ����Ȥ��ޤ��ݻ�����Ƥ����ͤ� ���Ф��ޤ��� =begin original Using a closure as a function template allows us to generate many functions that act similarly. Suppose you wanted functions named after the colors that generated HTML font changes for the various colors: =end original �ؿ��ƥ�ץ졼�ȤȤ��ƥ����������Ȥ����Ȥˤ�äơ�Ʊ���褦�ʴؿ��� ���������뤳�Ȥ���ǽ�ˤʤ�ޤ��� �����ǡ����ʤ����͡��ʿ��Τ���˥ե���Ȥ��ѹ����� HTML ����������褦�� ����̾���ΤĤ����ؿ���ɬ�פȤ��Ƥ���Ȥ��ޤ��礦: print "Be ", red("careful"), "with that ", green("light"); =begin original The red() and green() functions would be similar. To create these, we'll assign a closure to a typeglob of the name of the function we're trying to build. =end original red() �� green() �Ȥ��ä��ؿ��ϻ��̤ä���Τˤʤ�ޤ��� ��������Ф�����ˡ�������Ȥ��Ƥ���ؿ���̾���η������֤� ������������������ޤ��� @colors = qw(red blue green yellow orange purple violet); for my $name (@colors) { no strict 'refs'; # allow symbol table manipulation *$name = *{uc $name} = sub { "@_" }; } =begin original Now all those different functions appear to exist independently. You can call red(), RED(), blue(), BLUE(), green(), etc. This technique saves on both compile time and memory use, and is less error-prone as well, since syntax checks happen at compile time. It's critical that any variables in the anonymous subroutine be lexicals in order to create a proper closure. That's the reasons for the C on the loop iteration variable. =end original ����ǡ����줾��δؿ����̸Ĥ˺�������ޤ��� red(), RED(), blue(), BLUE(), green() �Τ褦�˸ƤӽФ����Ȥ���ǽ�Ǥ��� ���Υƥ��˥å��ϥ���ѥ�����֤ȥ��꡼�λ����̤��ޤ��� �ޤ����顼�򾯤ʤ����ޤ�; �ʤ��ʤ顢��ʸ�����å��ϥ���ѥ�����˹Ԥ��뤫��Ǥ��� Ŭ�ڤʥ����������������뤿��ˡ�̵̾���֥롼����ˤ����ѿ��Ϥ��٤� �쥭������Ǥ��뤳�Ȥ����פǤ��� ����ϥ롼�פη����֤��ѿ����Ф��� C ����ͳ��Ʊ���Ǥ��� =begin original This is one of the only places where giving a prototype to a closure makes much sense. If you wanted to impose scalar context on the arguments of these functions (probably not a wise idea for this particular example), you could have written it this way instead: =end original ����ϥ���������˥ץ��ȥ����פ�Ϳ���뤿��ξ��ΰ�ĤǤ⤢��ޤ��� ��������ؿ��ΰ����˥����饳��ƥ����Ȥ�Ϳ���褦�Ȥ���ʤ� (�����餯�Ϥ�����˴ؤ��ƤϤ��ޤ긭��������ǤϤʤ��Ǥ��礦)�� ����˰ʲ��Τ褦�˵��ҤǤ��ޤ�: *$name = sub ($) { "$_[0]" }; =begin original However, since prototype checking happens at compile time, the assignment above happens too late to be of much use. You could address this by putting the whole loop of assignments within a BEGIN block, forcing it to occur during compilation. =end original �������ʤ��顢�ץ��ȥ����פθ����ϥ���ѥ�����˹Ԥ���Τǡ� ��������ʸ�ϥץ��ȥ����פ�Ȥ��ˤ��٤�����ΤǤ��� ����ʸ�Υ롼�����Τ� BEGIN �֥��å����֤��Ƥ��٤Ƥ� ����ѥ�����˹Ԥ��褦�ˤ��뤳�ȤǤ�����н褹�뤳�Ȥ��Ǥ��ޤ��� =begin original Access to lexicals that change over time--like those in the C loop above, basically aliases to elements from the surrounding lexical scopes-- only works with anonymous subs, not with named subroutines. Generally said, named subroutines do not nest properly and should only be declared in the main package scope. =end original ���֤ˤ�ä��Ѥ��褦�ʥ쥭�������ѿ����Ф��륢������ -- �㤨�������� C �롼�פΤ褦�ˡ�����Ū�˼��Ϥ�쥭�����륹�����פ���� ���Ǥؤ���̾ -- ��̵̾���֥롼����ˤ����ƤΤ�ư��� ̾���դ����֥롼����Ǥ�ư��ޤ��� ����Ū�˸����ȡ�̾���դ����֥롼�����Ŭ�ڤ˥ͥ��Ȥ��뤳�ȤϤʤ��� main �ѥå���������������ǤΤ���������٤��Ǥ��� =begin original This is because named subroutines are created at compile time so their lexical variables get assigned to the parent lexicals from the first execution of the parent block. If a parent scope is entered a second time, its lexicals are created again, while the nested subs still reference the old ones. =end original ����ϡ�̾���Ĥ��Υ��֥롼����ϥ���ѥ�����˰��٤������������Τǡ� ���Υ쥭�������ѿ��ϡ��ƥ֥��å��κǽ�μ¹Ի�����ƥ쥭������� ��������뤫��Ǥ��� �ƥ������פ� 2 ���ܤ�����ȡ��쥭������Ϻƺ������������� �ͥ��Ȥ������֥롼����ϸŤ���Τ򻲾Ȥ����ޤޤǤ��� =begin original Anonymous subroutines get to capture each time you execute the C operator, as they are created on the fly. If you are accustomed to using nested subroutines in other programming languages with their own private variables, you'll have to work at it a bit in Perl. The intuitive coding of this type of thing incurs mysterious warnings about "will not stay shared" due to the reasons explained above. For example, this won't work: =end original ̵̾���֥롼����ϡ�'sub' �黻�Ҥ��¹Ԥ��졢���ξ�Ǻ����������� ��ª����ޤ��� ¾�Υץ�����ߥ󥰸���ˤ���褦�ʡ���ͭ�Υץ饤�١����ѿ�����ä� �ͥ��Ȥ������֥롼�����Ȥ������ΤǤ���С�Perl �ǤϤ���ä� ��֤�������ޤ��� ľ��Ū�ˤ������Ȼפ��褦�ʥ����ǥ��󥰤ϡ���Ҥ���ͳ�ˤ���ԲĻ׵Ĥʷٹ� "will not stay shared" �Ȥʤ�Ǥ��礦�� ���󤲤�ȡ��ʲ�����Ϥ��ޤ�ư��ޤ���: sub outer { my $x = $_[0] + 35; sub inner { return $x * 19 } # WRONG return $x + inner(); } =begin original A work-around is the following: =end original �ʲ��Τ褦�ˤ���Ȥ��ޤ������ޤ�: sub outer { my $x = $_[0] + 35; local *inner = sub { return $x * 19 }; return $x + inner(); } =begin original Now inner() can only be called from within outer(), because of the temporary assignments of the anonymous subroutine. But when it does, it has normal access to the lexical variable $x from the scope of outer() at the time outer is invoked. =end original ����ǡ�inner() �� outer() ���椫��Τ߸ƤӽФ���褦�ˤʤ�ޤ��� �ʤ��ʤ顢̵̾���֥롼����ΰ���ѿ����Ф������������뤫��Ǥ��� �����������Ԥä��Ȥ���outer() ����ư����Ȥ��� outer() �Υ������פ��� �쥭�������ѿ� $x �Υ����������̾��̤�Ǥ��� =begin original This has the interesting effect of creating a function local to another function, something not normally supported in Perl. =end original ����ϡ��̾� Perl �Ǥϥ��ݡ��Ȥ���Ƥ��ʤ��褦�ʡ� ����ؿ��˥�������ʴؿ�����������Ȥ��ζ�̣�������̤Ǥ��� =head1 WARNING X X (�ٹ�) =begin original You may not (usefully) use a reference as the key to a hash. It will be converted into a string: =end original ��ե���󥹤�ϥå�����Ф��륭���Ȥ��ƻȤ����Ȥ�(���̤�)�Ǥ��ޤ��� �����ʸ������Ѵ�����Ƥ��ޤ��ޤ�: $x{ \$a } = $a; =begin original If you try to dereference the key, it won't do a hard dereference, and you won't accomplish what you're attempting. You might want to do something more like =end original ������ǥ�ե���󥹤��褦�Ȥ���ΤǤ���С������ �ϡ��ɥ�ե���󥹤ˤ�Ư������ �����ơ����ʤ������褦�Ȥ������ȤϤǤ��ʤ��Ǥ��礦�� ���Τ褦�ʽ����򤷤����Ȼפ����⤷��ޤ��� $r = \@a; $x{ $r } = $r; =begin original And then at least you can use the values(), which will be real refs, instead of the keys(), which won't. =end original ư��ʤ� keys() ������ˡ����ʤ��Ȥ������Υ�ե���󥹤Ȥʤ� values() ��Ȥ����Ȥ��Ǥ��ޤ��� =begin original The standard Tie::RefHash module provides a convenient workaround to this. =end original ɸ��⥸�塼�� Tie::RefHash �Ϥ���򰷤��䤹�����뤿��� ���ʤ��󶡤��Ƥ��ޤ��� =head1 SEE ALSO =begin original Besides the obvious documents, source code can be instructive. Some pathological examples of the use of references can be found in the F regression test in the Perl source directory. =end original �ɥ�����Ȥ�¾�ˡ������������ɤ⤿��ˤʤ�ޤ��� ���Ĥ��Υ�ե���󥹤�ȤäƤ���������Ū�ʥ���ץ�� Perl �� �������ǥ��쥯�ȥ�ˤ��� F �Ȥ�����ԥƥ��Ȥˤ���ޤ��� =begin original See also L and L for how to use references to create complex data structures, and L, L, and L for how to use them to create objects. =end original ʣ���ʥǡ�����¤���������뤿��Υ�ե���󥹤λȤ����� L �� L �򻲾Ȥ��Ƥ��������� ���֥������Ȥ��������뤿��Υ�ե���󥹤λȤ����� L��L��L �򻲾Ȥ��Ƥ��������� =begin meta Translate: KIMURA Koichi (5.005) Update: SHIRAKATA Kentaro (5.8.8-) Status: completed =end meta