<style> #Content { margin: 0; padding: 0px; padding-top: 10px; text-align: left; border-top: none; float: left; padding-left: 1px; width: 740px; } body { font-family : Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 18px; } h1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 28px; font-weight: bold; color: #FF8817; } h2 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 18px; font-weight: bold; color: #B82619; } h3 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 16px; font-weight: bold; color: green; } h4 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; font-weight: bold; color: green; } h5 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; font-weight: bold; color: #333; } a { color: #0066cc; } a:visited { color: #0066cc; } a:hover { color: #FA6F00; } a.img:hover { background-color:#fff; } li { margin-bottom: 7px } .topbanner { border-bottom:dashed 1px #d6d6d6; } pre { font-family: "Courier New"; padding:10pt; font-size: 9pt; background-color: #f6f6f6; border: solid 1px #d6d6d6; border-left: solid 3px #d6d6d6; } b { color: #FF8817; } td { font-size: 70%; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; }
  Perl6����Ѹ�ٵķ�չ�������﷨Ҳ�����б仯���������еIJ����﷨�������㿴��ƪ���µ�ʱ���Ѿ���ʱ����Ȼ��ˣ����ǻ���һЩ���������Ͽ��Բ鿴���������������µ������Ժ���ֻ��Ҫ���Ѽ�������ʱ���������ı����(�鿴�����ĵ� Perl6��ʾ¼12�Ժ������.����ע:�鿴Perl6���Ҳ�Ƿdz��õľ����ܿ�ϧ�������ڻ�û���������,Perlchina��Fayland��̨���ƹ����ֵ�������Ŭ�����İ�)��

  �����ڿ�ʼװ��ɵBһ�£��������Լ���ʵ��һ��Ҳ����Perl������Լ�ǵ�Perl��һЩ�ر���������֤ʵ����뷨���㿼�ǵ�Larry Wall���Խ����������,�����������������һ����Perl���Ա�̡����������°���齲����6.0.1�档����δ����Perl6����Щ�¹��ܣ����Ǵ����ſ�ʼ���⣬�����㿪ʼΪ��ĺ������Ϳ��Ȿ�飬����С���������ϵõ�����Ҫ�Ѿõ���Զ�������ڣ���ƪ���¾������㿴��δ������Զ����

  �ڱ��µ��Ժ�ʱ�������ǽ�����Perl6�ĸ�����ɫ������������Щ����ϲ������Щ���㲻ϲ���ġ��ã����ڿ�ʼ����һ�����ŵ���Щϸ�ڣ����Ǵ������Ͽ�ʼ����


����

  �������������м�������ԵĻ������֣��������ݿ����뵽�Լ�����ʹ������Χ�Ļ������ڲ�ͬ�ij����б仯������ͻȻ������һЩ�µ�λ�á�������ݿ���ֵ�����ǣ��ַ��������֡������ģ����Ǹ��ӵ����ݽṹ�������������ҵ���Щ���ݵ����֡�Perl6�����ֱ���ģʽ�����ǣ�������Scalar�������飨Array������ϣ��Hash������������ÿ�ֶ��ڱ���������ǰ��һ��ӡ��(����)��������$��������@����ϣ��%�����ӡ�Ǵ��Ӿ��Ͽ�������ĸ���ʹ���߱����ľ�ֹ��Ϊ����ʵ���������ֵIJ��dz�С���ӱ���������ÿ�ֱ�ֻ��һ��װ���ݵĹ��ӣ��������������һ�ּ��ϻ��ǵ�����һ��(������������ݰɣ��������Ϊʲô����Ҫ��ô˵��)��

����

������һ��ȫ�ܵĹ��ӡ����ǿ��Դ洢�ַ���������������������ֶ�������á������� 

$string = "��ӭ�μ�perlchina��Perl���Ļ�����Եõ�����";

$int = 42;

$float = 3.14159;

$arrayref = [ "Perlchina", "Perl���Ļ�", "����" ];

$hashref = { "Perlchina" => 362, "Perl���Ļ�" => 1574, "����" => 28 };

$subref = sub { print $string };

$object = Android.new;

һ���ļ����Ҳ������һ����ͨ����ģʽ����ͨ���� 

$filehandle = open $filename;

����

��������洢�򵥵�һ�ű�����ֵ��ͨ������������������Եõ���Щ������0��ʾ��һ��ֵ��@�����DZ������ֵ�һ�������۱�����ôʹ��@�����ᱻȡ������

@crew = ( "Perlchina", "Perl���Ļ�", "����" );

$second_member = @crew[1]; # Perl���Ļ�

���һ�������ж��ٸ�Ԫ�������ʹ��.elems������.last����������������������һ���������ĸ��� 

$count_elements = @crew.elems;

$last_index = @crew.last;

��

�Զ�

�Զ���Pairs���洢�˵�һ�ļ�/ֵ������û��һ��������ӡ�ǣ���Ϊ���Ǻ��������ʹ�ã��������ǿ��Ա��洢�ڱ��������顢��ϣ�С��Զ��Ľṹʹ��=>�����죬�������Ǽ���=>��������ֵ����

$pair = 'key' => 'value';

�ı�ѡ���﷨Ҳ������������һ���Զ�����һ��ð�ŵĺ�����д�����������ŵ�������д��ֵ���� 

$pair = :key('value');

���ѡ���﷨�����ӳ�������о���ʹ�õ��﷨������Բ鿴�����������ĵı��ʵڶ��汾��(Perl6 and Parrot Essentials,2nd Edition)�ĵ�����5.3.1��

��

��ϣ

��ϣ�д�ŵ�������ı���ֵ��ʹ�á��������������洢����á���������ɵIJ���һ����ϣ�б����洢�����ĶԶ��� 

%hash = ( "Zaphod" => 362, "Ford" => 1574, "Trillian" => 28 );

ÿ������������ʶ��һ��ֵ�����ǿ������ַ����������Ȼ���󡱼��������ܵ�һЩ�������ơ�Ϊ�����ܡ�����������뱻��������ʹ�á��κζ����Ϊ��ϣ�ṹ�ġ�����������һ��.id�ķ��������������һ��һ�ķ��ض���ʵ���ġ�ֵ���������Ա����ϣ�г�ͻ��������������еĻ�������Ĭ�ϵģ������㶨���Լ���.id����ʱֻ��Ҫ����Ψһ�Ծ͹��ˡ� 

$age = %hash{"Zaphod"}; # �ַ���

$age = %hash{$name};    # �ַ�������

$age = %hash{$person};  # ����

�������ַ������Ա߱���Ҫ�����ţ����������һ���ӳ��������һ����ʱ������ӳ������������ַ�����һ��������ȥ�����ţ��� 

$age = %hash{get_key};  # subroutine call

������������������ţ������ڼ������Զ����÷�(˫������)�����泣�����÷�(���˺�)��

$age = %hash«Zaphod»;               # string

$age = %hash<<Zaphod>>;             # ASCII equivalent

���б��������У�һ����ϣ�᷵��һ���м�/ֵ�ĶԶ�(Pairs)���󡣶�.kv������ӹ�ϣ����һ����ƽ���б���������һ����ϣֱ�Ӹ���һ�����飺

����ע��һ��ĵ����(��Perlchina�� =>�����Ļ���)���(��Perlchina��,�����Ļ���)���߾�����ƽ���б���ƽ���б�Ҳ�������д洢���ݵĸ�ʽ��������������ʻ�����֡�
@pairs = %hash;

����������һ��ȫ�ǶԶ������飺

(pair1, pair2, pair3, etc . . . )

Ȼ����ϵ�����ƽ���б��� 

@flat = %hash.kv;

�ͱ����������ӣ� 

(key1, value1, key2, value2, etc . . . )

.keys�����������ڹ�ϣ�е����С��������б���.values�����������ڹ�ϣ�е����С�ֵ����

@keys = %hash.keys;

@values = %hash.values;

��

����

���õĹ�����Perl6����Ҫ�Ը���ȷ�ˡ�����������ͨ����֮����һ����С�����𣬲������Զ��������û������õ�ijЩ��Ҫʱ�Żᱻʹ�õ�������һ��������ϣ�����ò�����Ҫʲô�ر��﷨����ֻ��Ҫ�򵥵İ����Ǹ���һ�������Ϳ����ˣ� 

$arrayref = @array;

$hashref = %hash;

�ںܶ������Ĺ�ϵ�����ý���͵͵�ı��Ӵ�������ʹ�������������ϣ�ġ��������������ǵ���ֵ��ֻ��Ҫ�������׵����� 

$arrayref[1]

$hashref{"Zaphod"}

���������û��ϣ�����е��÷�������һֱ�ڲ�����ϣ������һ����ָ�������ú�����������ͻ���󡱲���������ر���������ĸ���������ʹ�ã���Щ������ʲô�ˣ�����������ÿ���֧���������ӵķ��ʣ� 

$arrayref.elems

$hashref.keys

ͨ�����ÿ��Ժ����׵�ִ�д��в������ӳ��򡣵�Ȼ���������ǿյģ����������DZ����еģ�

$subref($arg);

�����������ϣ�����и��ر��﷨ (@{ . . . } �� %{ . . . }) �������������н�����ǵ����ã�

@array = @{$arrayref};

# or

@array = @$arrayref;

ͨ��һ�����������������������һ�����齫�����ֻ�������һ�������õ������顣�����������е�$arrayref���ݶ�����@array������Ƚ��������á�

��

������������

�ڣ�$��@��%ӡ�ǿ�ͷ�ı�����������һ�����Ե�����$��ͷʹ�ñ��������ģ�@��ͷʹ���б������ģ�%��ͷʹ�ù�ϣ������[1]��

[1]Perl6�ﲻ����ֻ�������������ġ�һ����Ϊ��ϸ��������ο���������Ӣ�ĵı��ʵڶ���4.2.7���ֻ��Ժ���½ڡ���

����������

�κ�һ��������б���������������ֵ���õ�һ���������á�������һ��������˵����仰�ĺ��壺 

@array = ( "Zaphod", "Ford", "Trillian" );

$arrayref = @array;

һ���б��� 

$arrayref = ( "Zaphod", "Ford", "Trillian" );

����һ���ⲿ���������ã� 

$arrayref = [ "Zaphod", "Ford", "Trillian" ];

����������������������ȷ�������ͬ�Ľṹ��һ����������Ҳ��������Ԫ�ء�

�б��Ľṹ�����ŵĹ�����������������ĵ�����ͬ����Ч����С����ֻ��ʾһСȺ����һ��������Ԫ�ر�����С�����ڲ��Ҹ�����һ�����������ģ�������Ϊ���͵ı���ֵ��

$value = (20);

������뽨��һ��ֻ��һ��Ԫ�ص�����������ʹ�÷�����([ . . . ]) ������һ��������������ã�

$arrayref = [20];

һ�����������ϣ�����ݱ�������һ�������������������Ͳ�����һ����������ĶԶ����飬�������ַ������ǣ� 

$pair_list = ( "Zaphod" => 362, "Ford" => 1574, "Trillian" => 28 );

���Ҫʹ�ù�ϣ�����ڱ����������У������ʹ��{...}��ȷ�ı�ʾ��������ṹ�� 

$hashref = { "Zaphod" => 362, "Ford" => 1574, "Trillian" => 28 };

��

�������

����ʹ��@��Ϊӡ�ǽ�������ƽ���б��������ġ������˼������㸳һ�����������һ�����飬��ô��ЩԪ�ػ��˳���Ĵ�ԭ��������һ��һ���ĸ��Ƶ��������С�����������������ͬ�ṹ�����齫������һ����ֵ��

@copy = @original;

һ���б�ͬ��Ҳʹ��ƽ���б������ġ���һ�����鸳��һ��ƽ�����б����Ұ�������ÿ��Ԫ�ذ���ǰ��˳������б�����������Ԫ�ر��б��࣬��ô����IJ��ֽ��Զ��������� 

($first, $second, $third) = @array;

���б��������������һ������ֵ����ô��������һ������Ԫ�ص����飺 

@array = (20);

@array = 20;    # same

��������������ʹ��[ . . . ]�����������ڲ����ƽ���б���Ϊ����ʱ���������ܱ�������Ϊ�б������ģ�������Ϊƽ���б������IJ���ƽ�����á��ڱ����������У�һ����ͨ���б���һ���������ýṹ���Բ���һ���Ľ�����������б��������У�һ��(...)������б�������Ϊһ��ƽ���б��Դ�����[...]����ֻ����Ϊ�б���һԪ�ضԴ������Ԫ����һ���������������õ�[...]�У�

@array =  ( "Zaphod", "Ford", "Trillian" );

@array =  [ "Zaphod", "Ford", "Trillian" ];

�����һ�����ӻ����������Ԫ�ص����顣����ڶ������ӻ������һ��Ԫ�ص����鲢�����Ԫ��������������һ��������Ԫ�ص����顣����ڴ���һ�����ӵ����ݽṹ��˵�����ô��� 

@array =  ( "Marvin", [ "Zaphod", "Ford", "Trillian" ], "Zarniwoop" );

ͬ���ģ���ƽ���б��������е�һȺ��������б�����ƽ����һ���µ��б���һ�����������б�Ҳ���Ա�������Ϊ���б�����ʹ������������б��д洢�����������ã� 

@array = ( @array1, @array2, @array3 ); # single flattened list

@array = ( $arrayref1, $arrayref1, $arrayref3 ); # 3-element list

�����һ�����Ӳ���һ����������������������Ԫ�ص������飬���ڶ������ӽ��������������������õ����顣

һ���Զ�����������ʾ������һ�����б�����������һ��û���κ�Ԫ�ص�����ṹ���ڱ���������б�����������ɣ�

$arrayref = ( ); # 0-element arrayref

@array = ( );    # 0-element array

��

��ϣ�б�������

ʹ��%ӡ�ǵĹ�ϣ�б������ģ������õ��Զ��б���������һ�����͵�ʹ�öԶ���������=>������������Զ��б���

%hash = ( "Zaphod" => 362, "Ford" => 1574, "Trillian" => 28 );

һ���򵥵�ֵ�б��ڹ�ϣ�б��������лᱻ��Ϊ�Զ��б���������ڹ�ϣ�б�������Զ���д���ַ�ʽ�� 

%hash = ( "Zaphod", 362, "Ford", 1574, "Trillian", 28 );

{ . . . }���ڹ���������ϣ���ã��������Dz���ǿ��Ϊ��ϣ�б������ġ�������Ϊ�� { } ��Χһ����ͨ�ṹ����������һ����������������һ�������ӳ����ˣ�

# a sub reference that returns a list

$subref = { "Zaphod", 362, "Ford", 1574, "Trillian", 28 };

��ϣ���ù�����ʵ���ϲ���{...}����{...=>...}�����Ե����һ����ϣ���ø���һ��������ʱ����Ͳ���ʹ�ö��Ź���Զ��ṹ����������=>���������ṹ��һ����ϣ�ṹ������������壬������������ĵ��ұ�ǿ��ָ�����ǹ�ϣ(hash)����һ���ӳ���(sub)�� 

$subref = sub { print "Lost luggage.\n"; }

$hashref = hash { "Zaphod", 362, "Ford", 1574, "Trillian", 28 };

��

����������

����������˵�����䵽������ֵ�ϡ���ͬDamian��Ҫ����һ�������ߺ���ֽ���������дһЩע�ͣ����Ǹɴ����Ҫ����Ϣ��д��ȥ��֮��Ͱ����������ϣ�������ʾ���������������DZ����ϡ����㲻��Ҫ��ʱ��Ͱ��������������Ϳ����ˡ�

һЩ�������ڱ���ʱ�ű����ϵġ����DZ��������ԡ�������Ȼ���ڵ��ߣ�ֻ������һЩ�ر����ѡ������ڱ���������ʱ��ͻᱻ�̶��ڱ����ϣ������Ժ�Ҳ�޷��޸ġ�����ʱ��������ʹ�� is �ؼ��֣�

my $pi is constant = 3.14159;

constant ����ָ�����������ֵ�����Ա��ı䡣

�������Ա�����������ʱ������ֻ�޸ı�����ֵ�����DZ��������ǿ��Ա����ӻ�ɾ���ڴ���ִ�е��κν׶Ρ�����ʱ����ʹ�� but �ؼ��֣�

$true_value = 0 but true;

true ����ָ�����ֵ������ڲ�����Boolean�������ĵ�ʱ�򽫳�Ϊtrue������������ʵֵ��ʲô��������ߵ����Ժ�����Perl6ϵͳ�ĺ��н���һ���򵥵�����ʽ��顣���������Ȼ��������ֵ���ɹ���ʱ����0ʧ�ܵ�ʱ����������ֵ������������ֵ��ǽ��ں��гɹ��󷵻سɹ���ʧ�ܡ�

���ߺ�����ͬ��Ҳ���Դ洢ֵ����constant��ture�����õ�ʱ���������Լ���ֵ��һЩ���ߴ洢ֵ������

my @array is dim(2,5,42); # specify dimensions

����Բ������ŵ�������ô��Ҫ�����һ��и��������﷨���㽫���Ժ�ܶ��½��п���������Ӱ�ӡ����߲������ǶԱ�����ֵ�����ã�ʵ���������Ա��������ӳ��򡢺������ࡢ�ʷ������������Ҵ��ڲ����б��С�

��

����

Perl6������ָ��������������ֵ�Ĺ���Ҫ��Perl5ǿ��������ע��������һ����Ҫָ�����ǡ������ʹ�������㽫���һЩ�����ϵĺô�������֮��Ľӿڡ�����ϵͳ��δȫ����ɣ����ǻ����Ѿ���ɡ�

Perl6��ֵ�������������֮����һ��СС����ֵ ����ָ��������ֵ���ڵı������Դ洢����ֵ����һ�� Int ֵ���͸���һ�����������������ֻ�ܴ洢����ֵ��

my Int $scalar;

��һ�� Int ��ֵ���͸���һ�������������齫ֻ�洢����ֵ��

my Int @array;

ͬ������һ�� Int ֵ���͸���һ����ϣ�����ϣҲ��ֻ�洢����ֵ�����Dz����� �� �����ͣ���

my Int %hash;

���� ����ָ�����������ʲô��������������������ϸ�Perl5��� tie ��ࡣ�������Ͷ��������һ����ʹ�� is �ؼ��֡������ǽ���ʾ��������ϰ���ڣ����Ҫ���������͵ı���ֻ��Ҫ������

my $scalar is Scalar;

my @array is Array;

my %hash is Hash;

��ͬ�����Զ������Լ�������ʵ�ֱ������ͣ�

my $scalar is FileHandle;

my @array is Matrix;

my %hash is BerkeleyDB;

�ּ����ݽṹ����ʹ��һ���ۺ�ֵ���͡�һ����ϣ�洢�������飬���齫ֻ����ֵ���� Array of Int��

my Array of Int %hash;

�����﷨�ǿ��Ըı�ģ���ͬ��Ҳ������ôд��

my %hash is Hash of Array of Int;

# or

my %hash of Array of Int;

����������ݽṹ���������Ľ��ɶ��ԣ��ر����ڶ༶���ݽṹ�

my Array of Hash of Array of Int %hash;

my %hash is Hash of Array of Hash of Array of Int;

��

������

������Ϊֵ�IJ����ṩ��һ���ݾ������ü����ַ��Ϳ������һ���������������������������顣���ֹ۽ǶȽ��������ṩ�˲���˼��ķ��㣬��Ȼ���෴�Ƕ�����������������һ����С�ĵط��Ͱ����dz����ĺ�������ѧϰ����Ҫ����һЩ������Perl6�IJ��������Ǵ����֪�ģ�������Perl5����Ա�ǡ��µIJ���������ҲΪ���Ա����������¹��ܣ�������Perl�IJ�����ͬϵͳ��һ���ˡ�

��ֵ������

���ںţ�=��������ͨ�IJ�����������ֵ�����渴�Ʋ��Ҹ�������ı�����ij�����ݽṹ��

$copy = $original;

@copies = @originals;

$copy��$original����ͬ��ֵ��@copiesӵ��@originals���е�Ԫ�ء�

ð�ŵ��ںţ�:=�����������󶨸�ֵ��ʹ�ô��������ķ�ʽ�����һ��������ṹ���Ƶ������������ڷ��ű��б�ʾ�����ֵ����������һ����ڣ�����ע���������ҿ�һ������һ�������Ӷ���ͬ�������ݣ���

$a := $b;  # $a �� $b �ı���

@c := @d;  # @c �� @d ����
�����ʵ������������޸ı���$a��ô����$bҲ���б仯����֮Ҳ�ǡ�������Ϊ��������ֻ��ͬһ�������IJ�ͬ���ֶ��ѡ��󶨸�ֵҪ������������ͬ������Ԫ�ط���ͻ������
# ($a, $b) := ($c);          # ����

# ($a, $b) := ($c, $d, $e);  # ����

˫ð�ŵ��ںţ�::=����һ����ͬ�İ󶨲����������ڳ������ʱ�ű�ִ�С�

����������

�����������У��ӷ���+����������-�����˷���*����������/����ϵ����%������Ļ��**��������ÿ��������Ӧ�ĸ�ֵ�����﷽ʽ��+=��-=��*=��/=��%=��**=����

$a = 3 + 5;

$a += 5;     # $a = $a + 5

һԪ����������ʹ��ǰ׺�ͺ�׺��������++���Լ���--����ǰ׺���ڸ�ֵǰ��ɼ��㣬��׺�������෴��

$a++;

$a--;

++$a;

--$a;

�ַ���������

���ߣ�~�����������������ַ��������ӷ���~=�����ұߵ��ַ�����������Ľ�β��

$line = "The quick brown " ~ $fox ~ jumps_over( ) ~ " the lazy " ~ $dog;

$line ~= "Belgium"; # ׷�ӵ�$line�Ľ�β

X��������x�������ظ��ַ��������������ķ����ַ������ܲ�����������һ����Ԫ�صı�������һ���б��������ʵ����ֵ"LintillaLintillaLintilla"��

$triplet = "Lintilla" x 3;

��Ӧ��x=�������ظ�ԭ�ַ������ҷ��ظ�ԭ������

$twin = "Lintilla";

$twin x= 2;          # "LintillaLintilla"

�������

XX��������xx�������ظ��б�����������һ���б�������������һ���б�����һ����Ԫ�ء���������ʵ����ֵ��������Ԫ�ص�����@array������ÿ��Ԫ�ض���"Lintilla"��

@array = "Lintilla" xx 3; # ("Lintilla", "Lintilla", "Lintilla")

��Ӧ��xx=��������ָ������Ԫ�ص��б������ҽ��丳��һ�����������

@array = (4, 2);

@array xx= 2;              # ���� (4, 2, 4, 2)

@array = (@array, @array); # �ȼ�

��Χ��������..������һ��ֵ���б���������Ҫָ����ʼ��ͽ����㣺

@range = 3..7;   # 3,4,5,6,7

��Χ�������Ƕ��Եģ�����һ����������ֵ�ķ�Χ������һ�μ������е����ڸ�ֵǰ���෴��������һ���б���������ֻ��������Ҫ��ʱ��ŷ�������Ҫ��Ԫ�ء�

@range = 3..Inf; # ����

ʡ�ԺŲ�������...��ͬ..Inf��һ���ģ�

@range = 3 . . . ;

�Ƚϲ�����

ÿ���Ƚϲ����������������ӣ�һ�������ֱȽ���һ�����ַ����Ƚϡ��Ƚϲ������д��ڣ�>��gt����С�ڣ�<��lt�������ڵ��ڣ�>=��ge����С�ڵ��ڣ�<=��le�������ڣ�==��eq���������ڣ�!=��ne�������ݲ�������=:=�������������������Ƿ���ͬ����ı������������ʽΪ�潫���������Ϊ�ٽ����ؼ١���׼��������<=>��cmp��������������ͬ������·���0�������һ���󷵻�1������ڶ�����-1��

if ($age > 12) { . . . }

�Ƚϲ�����������������һ�𡣱�������һ��ıȽϲ�������ִ�е�ʱ��ÿ��ִֻ��һ�Σ���

if (24 < $age < 42) { . . . } # 24 < $age and $age < 42

�߼�������

��Ԫ�߼�������������������ʵ�Բ�������ֵ��Ȼ�󷵻�����һ��������һ��������Ҳ����Ϊ�����̣�short-circuit����������������Ϊ�ܵ���ʵ�Կ����������ȷ���Ļ����Ҳ�ľͲ��ᱻ���㡣�����������Ϳ����������ĸ�ֵ��ִ�д����ˡ�

��ϵ�������루AND����&&�͵����ȼ���and����ʾ��������������Ϊ�٣���ô�ͷ�������ֵ��������������Ϊ�棬���Ҳ�ͱ����㲢��������ֵ��

$splat = $whale && $petunia;

$splat = ($whale and $petunia);

��ϵ��������OR����||�͵����ȼ���or����ʾ�������Ϊ��ʱ�������ֵ�����������Ҳ෵���Ҳ�ֵ��

$splat = $whale || $petunia;

$splat = ($whale or $petunia);

һ����ͬ�Ļ�OR����ϵ�����������Ƿ����������١���ʹ��//�͵ͼ���err��Ϊ���������������ֵ�ж���ͷ��أ����������Ҳ෵���Ҳ��ֵ��

$splat = $whale // $petunia;

$splat = ($whale err $petunia);

��ϵ���XOR����^^�������͵ͼ���xor��������ʾ��������һ����������aperand��Ϊ��ʱ����Ϊ��IJ����롣���������Ϊ���Ϊ��ʱ���ؼ١�xor��������ͬ�����������̲���������Ϊ����������������������֪����٣�

$splat = $whale ^^ $petunia;

$splat = ($whale xor $petunia);
perl 6 ��ȻҲ�в�ͬ�IJ����߼���������?& (AND), ?| (OR), �� ?^ (XOR)�����Ƿ��ص�ֵ�������١�
$whale = 42;

$petunia = 24;

$value = $whale || $petunia   # $value is 42

$truth = $whale ?| $petunia   # $truth is 1

������ǿ�Ʒ�

�����ı���ʽָ���˽�Ҫ�����ֵ�����͡�һ������ϣ��ͬʱ�������ֵ�������б����������ɱ������������ˡ�һ����������ϣ���õ�һ����һ��ֵ����������һ���������������ˡ�Perl�ı���ʽ�����ʺ���Щ�����ģ�����������Ҫ��ֵ��

�����ľ�֤����Perl5��dz��м�ֵ�Ĺ��ߣ�������Perl6����Ϊ������Ҫ�������˳��롣�������Ļ�����void context����Ȼ���ڣ����������ı�ϸ����������boolean�������ͣ�integer�������֣�numeric�����ַ�����string���Ͷ���object�������ġ��б������ı�ϸ����ƽ���б���flattening-list�������ģ���ƽ���б������ģ������б������ģ���ϣ�б������ġ�

��������

����Ҫ�κη���ֵ��

����������

��Ҫ��һֵ�����һ���ϳ�ֵ���������Լ������á�

����������

��Ҫ���١�������ͳ����Ķ���"0"��"undef"���ַ��������١�Ҳ��������ֵ����������"true"��"false"��

����������

��Ҫ����ֵ���Ѿ���������Ϊ���ֵ��ַ����͸�������floating-point�������е�����IJ��֡�

����������

��Ҫ���֣����������͵Ļ��Ǹ���ġ������Dz���ʮ���Ƶģ����Ƕ����ƣ�ʮ�����ƣ��˽��ƻ�����ʲô���͡�

�ַ���������

��Ҫ�ַ��������������о���������Ϣͳͳ��ʽ����Ϊ�ַ�����

����������

��Ҫ������������ԣ�һ����������á���ͬ����Ҫһ����ϸ���͵Ķ���

�������

��Ҫ�ռ�ֵ���κ�һ����ֵ���б��������ж�����Ϊ����б������ĵ�һ��Ԫ�ء�

ƽ���б�

��Ҫһ���б�������������ƽ����������ϣ��

��ƽ���б�������

��Ҫһ���б����������顢��ϣ���������ֵ��Ϊ��ɢʵ��

�����������

��Ҫһ���б����Ͳ�ƽ���б������ĺ��󣬵��Dz���һ���Ե���Ҫ�б�������Ԫ�ء�

��ϣ�б�������

��Ҫ�Զ��б���ÿ�����͹�ϣ�����Ķ����ԶԶ���Ϊ�����ġ�

��һԪ�����IJ���������������Ӱ��ʱ����ǿ��һ������������ġ�ͨ����Ĭ�ϵ����������ǶԵģ�������Щʱ������Ҫ��������Щ�����Ŀ��ơ�

һԪ���������͵����ȼ��ĵȼ۷�trueǿ�Ʋ��������ġ���ֵһ������������һ������ֻ�Ǽ򵥵����ñ��������ģ�����$number��ֵֻ�Ǽ򵥵ı����ơ����ʹ�����������ţ������ǿ�Ƹ��棨true�����������������$number��ֵ��

$value = $number;

$truth = ?$number;

һԪ���������͵����ȼ��ĵȼ۷�notͬ��Ҳǿ�Ʋ��������ģ��������ǻ�񶨣�����ֵ��ֵ�����dz���������ֻ�з��ǿɼ��IJ��������Ľṹ�С�

$untruth = !$number;

һԪ������+ǿ�����������ģ�����-Ҳǿ������������ֻ��������+�����෴��

$number = +$string;

$negnum = -$string;

һԪ������~ǿ���ַ��������ģ�

$string = ~$number;

��ͬ�����Խ����������б�����ϣ������ʹ��$(...)��@(...)��%(...)��

�����

�������......

����������

���ز�����??::�����ĵڶ���������������룬���������ǵ�һ��������Ϊ����Ǽ١�����������һ��if-then-else�ṹ�ķ��ű���ʽ��

$form = ($heads =  = 2) ?? "Zaphod" :: "ape-descended lifeform";

������

��������hyper operator����������Ϊ�б�����Ƶġ������DZ�׼�ı�����������"�޸İ�"��ÿ�����������г����棬�����û�����ġ�����ͬ�������������ͬ�Ļ����ṹ��������������ʹ�õ����ַ��ñ��» �� «[2]�������ı�ģʽ��>>��<<�����������ʵ���������ӷ�����>>+<<��

��[2]���������UNICODE������U+00BB������U+00AB�ַ���

������ʹ���б���������Ϊ���ǵIJ����룬���ò�������Խ������������Ԫ�ؽ��з��䡣�������ӷ��ӵ�һ���б����ó�ÿһ����Ԫ�ز������ӵڶ����б��ж�Ӧ��Ԫ�أ�

@sums = @first >>+<< @second;

����������ÿһ�Զ�Ԫ�ص��ܺ͡�����ÿ�Զ�Ԫ��ʹ�ñ������������ӵ�һ����

@sums = ( (@first[0] + @second[0]), (@first[1] + @second[1]), etc . . . );
�����������һ����һ���������������������Խ�б���ÿ��Ԫ�أ�
@sums = @numbers >>+<< 5;



@sums ( (@numbers[0] + 5), (@numbers[1] + 5), etc . . .  );
һԪ������ͬ��Ҳ������һ��ʹ�ó�������ֻҪֻ��һ�������룩��
@less = @numbers >>--;

@nums = +<< @strings;

���ӷ�

�������......

����ƥ��

��Ԫ������~~�ṩ������ֵ֮��Ĵ���ƥ�䣨smart match�������ƥ��ɹ����������棬���ƥ��ʧ���������ؼ�[3]������ƥ��!~�������෴��ƥ�䣺������Ϊ�٣���ô���������档����ƥ���ƥ��ģʽ����ƥ����������ġ�����������������Ͳ����ڱ���ʱ�ͱ���������ô��������ʱ����������ƥ��ͨ����ƽ��ģ�����A~~B��B~~A���õ���ͬ�Ľ����

[3]���������ϵġ�ʵ���ϣ�ƥ����ܷ��ظ����ӵ�ֵ��ֻ���ڴ���IJ��������ĵ�ʱ��Ż�ֻ������ͼ١�

����ƥ��

�������������һЩ���룬���Ǵ��뷵�صĽ���DZ���ֵ����ƥ������һ��һ��ȼ۵��ַ��������$string����"Ford"�����ʵ������������棺

$string ~~ "Ford"
�ڱ��������Ҫƥ�����֣���ô��ʹ������ȥƥ�䡣�������ʵ������ý��42�����$number��42�Ļ���
$number ~~ 42
һ������ʽ�Ľ����42Ҳ����棺
( (5 * 8) + 2 ) ~~ 42

��һ��δ�����ֵ��ƥ���һ�������Ƿ��塣���$value������˵����δ���壬������ؼ�˵���������ˡ�

$value ~~ undef

$value ~~ $undefined_value

����ֵ����һ����������������ģʽƥ�䡣���"towel"��$string���κεط����ҵ�����ô�������棺

$string ~~ /towel/

����ƥ��һ���滻ģʽ�����ҳ�ʶ�滻ֵ���ݡ����$string��ֵ�����滻��������񷵻ؼ٣�

$string ~~ s/weapon/towel/

����ֵƥ��һ���������ܼ򵥵ķ�������Ǽ١��������ʵ������Զ�����棬��Ϊ����IJ������Ƿ�����[4]��

[4]��Щʱ���������ʽ�ƺ�ûʲô�á������switch�����˽�̶������ƥ��һ����ʱ�������������������⡣�������Ϣ��鿴�Ժ����ݡ�
$value ~~ (1 =  = 1)

δ��ɲ���......

The Boolean value on the right must be an actual Boolean: the result of a Boolean comparison or operation, the return value of a not or true function, or a value forced into Boolean context by ! or ?. The Boolean value also must be on the right; a Boolean on the left is treated as an ordinary scalar value.

�б�ƥ��

������˳���Ե�ƥ��Ƚ��б����е�ÿ��Ԫ�ء��ڱ���ʽ-��-����ʽ��ƥ��ģʽ�У��������һ��Ԫ��ƥ����Ϊ�棬��ôƥ���Ϊ�档����ı���ʽ���õ��棬���$valueͬ���������ַ�����ͬ��

$value ~~ ( "Zaphod", "Ford", "Trillian" )

����һ������ƥ�䣺��һ��ƥ��ɹ�������ֹͣ��������������ͬ����ֵ������������ƥ��������orһ����

($value ~~ "Zaphod") or ($value ~~ "Ford") or ($value ~~ "Trillian")

һ������ƥ����б����԰������е�Ԫ�أ�����������rules��������������ʽ�����飬��ϣ��������

$value ~~ ( "Zaphod", 5, /petunias/ )
һ���б�����ƥ��ͬ����һ���б�����ƥ�䡣���е�һ���б���ÿ��ֵ����ͬ�ڶ����б���Ӧ��ֵ�ֱ�ƥ�䡣���ȫ��ƥ��ɹ���ô������õ��档��������ʵ���õ��Ľ�����棬��Ϊ���������б���ȫһ����
( "Zaphod", "Ford", "Trillian" ) ~~ ( "Zaphod", "Ford", "Trillian" )

�������б�����ͬ������������һ���ij��ȣ��нӽ���Ԫ�ؽ���ƥ�䣺

( $zaphod, $ford, $trillian ) ~~ ( "Zaphod", /Ford/, /^T/ )

����б�-���б���ƥ��ͬ�������̵ġ������һ��ƥ��ʧ������ֹͣ�����ǽ��õ���ͬ����ֵ����Ϊ����ʹ����and�����𼸸�����ƥ�䣺

($zaphod ~~ "Zaphod") and ($ford ~~ /Ford/) and ($trillian ~~ /^T/)

����ƥ��

һ�������ֵ�ƥ�����ʽƥ������ʱ��ƥ��ֵ�Ƿ�����������С����ֵ�������б��ҵ����õ���ķ���ֵ�����@array�е�������"Zaphod"��"Ford"��"Trillian"��$value�а����������е�һ������ô����ֵҲ�����棺

$value ~~ @array

һ������ֵƥ�����飬����������Ӧ���ֵ����Ԫ���Ƿ���ڡ��������ʵ�����@array[2]���ڽ������棺

2 ~~ @array

һ������ƥ���������ã�ͬ��Ҳ�����������Ԫ���Ƿ���ڣ�

2 ~~ [ "Zaphod", "Ford", "Trillian" ]

���ƥ�佫�����棬��Ϊ������Ԫ�ص��������档

���ʹ��*���������鿴�Ժ���ƽṹ���֣�ƽ���������ȥƥ�䣬�൱����һ�����ֵȥƥ��һ�������ԣ������ʵ�������������а���2��ֵ������������ĵڶ���Ԫ�أ�

2 ~~ *@array

һ������ͨ����������ƥ��һ������ʽ�����������ÿ��Ԫ�ض��ó���ƥ�䡣ֻҪ������һ����ƥ��ɹ���ô�ͷ����档���"Trillian"��"Milliways"��"million"������@array��Ԫ�أ������ƥ��Ϊ�棨�����������ˣ���

@array ~~ /illi/

��������Ҫ����һ��ƥ�䣬��ô��������������е�Ԫ�ض��ó������ж�Ӧ�ģ�

@humans ~~ @vogons

���ƥ�䷵���棬���������������ͬ�ij��Ȳ���@humans[0]ƥ��@vogons[0]��@humans[1]ƥ��@vogons[1]���ȵȡ�

��ϣƥ��

��ϵ������ƥ���ʱ��ƥ��hash��һ��ֵ�Ƿ�Ϊ�棨�Ա�����Ϊ������

$key ~~ %hash

���%hash{$key}���ڣ���ô���Ϊ�档

��һ���������ʽ��ƥ���ϵ�ļ���

%hash ~~ /blue/

ֻҪ��%hash����һ������blue����ô�������ͽ�Ϊ�档

��������ϵ�ṹ�м�����ǹ��еļ���

%vogons ~~ %humans

���ԣ�ֻҪ��һ����%vogons��%humans���У��ͷ����档�������ƥ���Ƿ�����ͬ�ļ��������б���ʽ������

%vogons.keys.sort ~~ %humans.keys.sort

��ϵͬ�������ƥ��ʱ�������鲿�ֵ�ֵȥ����ϵ������������κ�һ��ֵ���ǹ�ϵ�ļ�����ô�������棺

�д����飡����

A hash matched against an array checks a slice of a hash to see if its values are true.

%hash ~~ @array

���@array��һ��Ԫ����"blue"����%hash��һ��key��"blue"��%hash{'blue'}�з���ֵ�ͽ�Ϊ�棬�������û�з���ֵ��Ϊ�١�

����ƥ��

һ������ʽƥ��any���ӷ����ݹ�ֿ���recursive disjunction�����������ʵ��������б����κ�һ��Ԫ�ر�ƥ�佫�����棬����һ������ʽ-��-����ʽ��ƥ�䣺

$value ~~ any("Zaphod", "Ford", "Trillian")

�������ʽƥ��$valueͬ��������һ��ֵƥ�䡣���Ч�����б��Ƚϲ�ֻ࣬��ϧ������֤�Ƚϵ�˳���Ǵ����↑ʼ��

����ƥ��all���ӣ�ֻ������������ֵ��Ϊ�棬���ܵõ��棺

/illi/ ~~ all("Gillian", "million", "Trillian")  # match succeeds

/illi/ ~~ all("Zaphod", "Ford", "Trillian")      # match fails

����ƥ��one���ӷ���ֻ��һ��ֵΪ���ʱ�����ʽ��Ϊ�棺

/illi/ ~~ one("Zaphod", "Ford", "Trillian")      # match succeeds

/illi/ ~~ one("Gillian", "million", "Trillian")  # match fails

����ƥ��none���ӷ���ֻ������ֵ����Ϊ���ʱ�����ʽ��Ϊ�棺

/illi/ ~~ none("Zaphod", "Ford", "Marvin")    # match succeeds

/illi/ ~~ none("Zaphod", "Ford", "Trillian")  # match fails

һ��any���ӷ�ƥ������һ��any���ӷ����������ֵ�ݹ�ֿ���recursive disjunction��������ڵ�һ�����ӷ����κ�һ��ֵ���ڵڶ������ӷ����κ�һ��ֵ������ʽ���õ��棺

any("Ford", "Trillian") ~~ any("Trillian", "Arthur")

�������ʽΪ�棬��Ϊ"Trillian"����������ʽ�ﶼ���ڡ�

����ƥ��

����ƥ��һ�������֣����������������������Ǽ̳�������ࡣ�ӱ�����������������.isa������ࣺ

$ship ~~ Vogon::Constructor   # $ship.isa(Vogon::Constructor)

�ӳ���ƥ��

�κ�һ�ֱ���ʽ������ƥ���ӳ���ķ���ֵ������ӳ���û�в�������ô���ᱻ������Ϊ�����ṹ��

$value ~~ my_true

����ӳ�����һ�������źŲ��Һ��������ʽ�ı������ͼ��ݣ���ô�������ʽ����$value��Ϊ�������к����ӳ���

$value ~~ &value_test   # value_test($value)

@array ~~ &array_test   # array_test(@array)

%hash  ~~ &hash_test    # hash_test(%hash)

�ӳ���ķ���ֵ������ƥ����滹�Ǽ١�

һ����ƥ�������ӳ��������ķ���ֵ������ƥ���Ƿ�Ϊ�档���û�еõ���������������Ϊ�����ṹ�����ڿ���ʹ�������Ϊֵ��ʹ��$_��ռλ�����������鿴���������ĵı��ʵڶ��������5.2.7�ڣ���

$value ~~ { $_ + 5; }    # $_ is $value

%hash  ~~ { $_.keys; }   # $_ is \%hash

@array ~~ { @^a.elems; } # @^a is @array

����(��)

һԪ������\�������ز���������á���Ϊ���������Ŀ����Զ��IJ������飬��ϣ�����������á���������ȻҪʹ����ƽ����ƽ���б��������Ļ������޷��Զ��������õĵط���

@array_of_refs = ( \@a, \@b, \@c );

ͨ����һ�е����鱻����һ���б�����һ�е�����Ԫ��Ҫ��ƽ����Ϊһ����һ���顣���������ò�����������һ��ӵ�������������õ��б�����ֵ��@array_of_refs�ˡ�

һԪ������*��known as the splat operatorδ��ɣ�����������������ƽ��һ���б�ʱ��ͨ��������һ�����á�����ֵ��*ʹ����ת����Ϊһ���򵥵��б���

@combo = (\@array, \%hash);

@a := @combo; # @a is @combo

(@b, %c) := *@combo; # @b is @array, %c is %hash

��Ϊ@combo����Ȱ������������ְ�����ϣ���á�������ͨ������ֵ��@combo��ɵ�һԪ�ز�������@a�С���ƽ��������@combo���鱻ת���Ϊһ���򵥵��б�����������ÿ��Ԫ�ض����ֱ�ķŵ����档@b��������@array��@c��������%hash��

����ֵ��ʱ��*��������Ե����п��ԳԵIJ��������һ����ͨ�Ķ��Զ��󶨣��Ͱ����Ƿֱ����һ����һ��������Ҷ���ʽ����ˣ�

(@a, @b) := (@c, @d); # @a is @c, @b is @d

Ȼ�������ʹ��*����ô�����һ��ֵ����ƽ����������Ԫ�س�Ϊһ���б�Ȼ���ڰ󶨸�ֵ�����ԣ������㿴����@a������@c��@d������Ԫ�أ�

*@a := (@c, @d); # @a contains @c and @d

����*���ӳ���ͷ��������е���;����鿴�Ժ�IJ��֣�����Ժ�дPerl6�ӳ���Ļ����ǵø�����������Ҫ��Ŷ����

��

ѹ��������

ѹ���������������������б��������б������鰡����ϣ�ļ������ȵȣ��Խ�����ʽ��ŵ�һ���ҷ���һ�����б���������ͬһʱ����ʹ��ѭ�������IJ��������б���Ԫ�ء�

@a = (1, 2, 3);

@b = (4, 5, 6);



@c = @a ¬�� @b; # @c is (1, 4, 2, 5, 3, 6)

������Ų���ASCII�룬������û�취�ü������롣�����ʹ��zip��������ʵ��������ܣ�����������������ͬ�ġ���֮��IJ��ֶ����zip����������

��

�ṹ����

��򵥵����̿�����һ�����Եġ�һ������ʽ������һ��ֱ���ڳ��������ʱ��The simplest flow of control is linear��one statement follows the next in a straight line to the end of the program.Since this is far too limiting for most development tasks, languages provide ways to alter the control flow.

δ��ɣ�����

ѡ��

ѡ�������п��ܵĵط�ִ��һ�����������á�ѡ����ƽṹ�У�if��unless��given/when��

IF����

If�������һ����������������Ϊ����������Ϣ��������Ŀ��д��롣����ʽ���Ա�д���κ�������ȡ����Ҫ��ֵ��Բ�����DZ���ʽ�Ŀ�ѡ������

if $blue {

    print "True Blue.";

}

if����ʽ�ڻ��ʧ��֮��ͬ�����������޵� elsif ����鸽�ӵĶ�����ʽ��else��Ϊ����ģʽ������ if �� elsif ����ʽʧ�ܺ�ִ�У�

if $blue {

    print "True Blue.";

} elsif $green {

    print "Green, green, green they say . . . ";

} else {

    print "Colorless green ideas sleep furiously.";

}

��

UNLESS����

unless ����ʽ���߼��� if �����෴����������õ�ʧ�ܵĽ�����еĴ���Żᱻִ�У�

unless $fire {

    print "All's well.";

}

����һ�� elsunless ���������� unless �� else һ����

��

SWITCH����

switch����ʽ���ñȽ� given ����ʽ��ͬ������� switch���������� when ������ͬ������� cases����ִ��ѡ��������һ������ƥ��ѡ����������Ŀ�ͽ�ִ�У�

given $bugblatter {

    when Beast::Trall { close_eyes( );  }

    when 'ravenous'   { toss('steak');   }

    when .feeding     { sneak_past( );  }

    when /grrr+/      { cover_ears( );  }

    when 2            { run_between( ); }

    when (3..10)      { run_away( );    }



}

If these comparisons are starting to look familiar, they should. The set of possible relationships between a given and a when are exactly the same as the left and right side of a smart match operator (~~). The given aliases its argument to $_. $_ is always the current topic (think "topic of conversation"), so the process of aliasing a variable to $_ is known as topicalization. The when is a defaulting construct that does an implicit smart match on $_. The result is the same as if you typed:

δ��ɣ�������

given $bugblatter {

    when $_ ~~ Beast::Trall { close_eyes( );  }

    when $_ ~~ 'ravenous'   { toss('steak'); }

    when $_ ~~ .feeding     { sneak_past( );  }

    when $_ ~~ /grrr+/      { cover_ears( );  }

    when $_ ~~ 2            { run_between( ); }

    when $_ ~~ (3..10)      { run_away( );    }

}

but more convenient. Generally, only one case is ever executed. Each when statement has an implicit break at the end. It is possible to fall through a case and continue comparing, but since falling through is less common, it has to be explicitly specified with a continue:

δ��ɣ�����

given $bugblatter {

    when Beast::Trall { close_eyes( ); continue; }

    when 'ravenous'   { toss('steak'); continue; }

    when 'attacking'  { hurl($spear, $bugblatter); continue; }

    when 'retreating' { toss('towel'); }

}

�����е�������ʧ�ܺ�ִ��default �����飺

given $bugblatter {

    when Beast::Trall { close_eyes( ); }

    when 'ravenous'   { toss('steak'); }

    default           { run('away'); }

}

������ given �еĴ��붼����ִ�У��������һ�� when �ɹ���ֹͣ��given��ʣ��Ĵ���ִ�У�

given $bugblatter {

    print "Slowly I turn . . . ";

    when Beast::Trall { close_eyes( ); }

    print "Step by step . . . ";

    when 'ravenous'   { toss('steak'); }

    print "Inch by inch . . . ";

}

���ʾdefault ���������DZ���ģ���Ϊ��һЩ����д������ when ���� default һ����ִ�С�����һ����ȷ�� default�������Ը���ȷ��ֻ���ղ��쳣��ʱ��������𡣸������Ϣ��鿴�Ժ�IJ��֡�

when����ͬ�����Ա�д��given�����档������ôд�Ժ󣬽���$_ ���м򵥵�����ƥ�䡣when statements also have a statement modifier form that doesn't have an implicit break:

print "Zaphod" when 'two heads';    # if $_ ~~ 'two heads'

��

ѭ��

ѭ���ṹ��������ִ�б���ʽ��Perl6��ѭ������ʽ��while��until��loop�ͳ�����for��

��

WHILEѭ��

һ��whileѭ��������Ϊ�������½���Զѭ�����������Ժܸ��ӣ����ǽ����Զ����һ�������IJ���ֵ����Ϊwhileʹ�ò�����������Ϊ���Լ�������ѡ������

while $improbability > 1 {

    print "$improbability to 1 against and falling.";

    $improbability = drive_status('power_down');

}

until ���� while ѭ��������������Ϊ�ٲ���һֱѭ����ȥ��

until $improbability <= 1 {

    print "$improbability to 1 against and falling.";

    $improbability = drive_status('power_down');

}

��

��ѭ��

loop �ṹ��򵥣�����һֱѭ����ȥ�������ڱ���ʽ����ȷ����ֹѭ������Ȼ������Զ����ȥ������һ����������̫̫һ����

loop {

    print "One more of that Ol' Janx.";

    last if enough( );

}

loopͬ��֧��ѭ����¼������while�Ƶģ�����ִ�п������ݵ�ÿһ�ζ������鷳�IJ��Ա���ʽ���������и��õı���ģʽ���ڳ�ʼ����ִ��ѭ����ʱ�����Ϊѭ������һЩĿ�꣺

loop ($counter = 1; $counter < 20; $counter++) {

    print "Try to count electric sheep . . . ";

}

loop����ʽ�е�Բ���Ų��DZ���ġ�

��

FORѭ��

forѭ�������б�ѭ��������������ɢ���б������ġ�������һ�������б������ʽ�����б�������ÿ��ѭ��ȡ���б���ÿ��Ԫ�ء���ÿһ��ѭ����ʱ��for�ı���$_����Ϊ��ǰѭ��Ԫ�ء������˼�����нṹ��Ĭ�ϵı�����$_�У��� print �� when ����Ĭ��ѭ��ֵһ����

for @useful_things {

    print; # prints $_, the current loop variable

    print " You're one hoopy frood." when 'towel';

}

��ͷ������->���Բ�����ǰԪ�صı�����Ϊ$_��[5]���еı�����������ֻ�ڿ��С�

[5]��ͷ����for�����ƣ���ͬ��Ҳ������given�����������ƽṹ��ʹ�á�
for %people.keys -> $name {

    print; # prints $_ (same as $name)

    print ":", %people{$name}{'age'};

}

��ͷ������ͬ��������ѭ����ÿ��Ԫ�غ����������

for %ages.kv -> $name, $age {

    print "$name is now $age";

}

����԰Ѽ����������б�ʹ��zip������zip�������ϲ�ʹ��һ����ͷ��������������ÿ��ѭ����ʱ��ȡ����ָͬ��������Ԫ�أ��������£�

# one from each array

for zip(@people,@places,@things) -> $person, $place, $thing {

    print "Are you a $person, $place, or $thing?";

}

����ʵ��ͬʱѭ���������飬��ÿ�������л��Ԫ�ز��Ҳ�������Ԫ�صı�����

# two from each array

for zip( @animals, @things, :by(2) ) 

        -> $animal1, $animal2, $thing1, $thing2 {



    print "The animals, they came, they came in by twosies, twosies: ";

    print "$animal1 and $animal2";



    print "Two things. And I call them, $thing1 and $thing2.";



}

���ѭ��ʵ�����������飬ÿ��ѭ���������Ԫ�ز��Ҳ������ǵı�����

# two from the first array and one from the second

for zip(@colors=>2, @textures=>1) -> $color1, $color2, $texture {

    $mix = blend($color1, $color2);

    draw_circle($mix, $texture);

}

���ѭ��ʵ�����������飬ÿ�δӵ�һ������ȡ�ö���Ԫ��ͬʱ�ӵڶ�������ȡ��һ��Ԫ�ز��Ҳ������ǵı�����

���zip��������б��ij��Ȳ�ͬ����±������ˣ���ô����Ϊ�Ǹ��̵IJ���undefֵ������

��

�ƻ�ѭ��

�ؼ���next��redo��last�������ƻ����ڽ����е�ѭ����nextֹͣʣ�ಿ�ֵĴ��벢�ҿ�ʼһ���µ�ѭ����redoֹͣʣ�ಿ�ֵĴ��벢�����¿�ʼ��һ��ѭ�����ڲ����Ӽ��������¼��ѭ�������Ļ����ϣ���lastֹͣʣ�ಿ�ֵĴ��벢����ȫ��ֹѭ����

for @useful_things -> $item {

    next when 'towel';

    redo when .try_again;

    last when 'bomb';

    print "Are you sure you need your $item?";

}

��

��

In Perl 6, every block is a closure, so you get consistent behavior throughout the language, whether the block is a control structure, an argument passed to a subroutine, an anonymous subroutine reference, or the definition of a named element such as a subroutine, method, or class. What is a closure? Closures are chunks of code that are tied to the lexical scope in which they're defined. When they're stored and later executed at some point far removed from their definition, they execute using the variables in their original scope, even if those variables are no longer accessible any other way. It's almost as if they package up their lexical scope to make it portable. This example creates a closure that prints a lexical variable. When the closure is executed (from some other lexical scope), it prints the variable from the scope where it was defined, not the scope where it's executed:

δ��ɣ�����

my $person = "Zaphod";

$closure = { print $person; }

 . . . 

my $person = "Trillian";

$closure( ); # prints "Zaphod"

���еıպ�(closures)�鶼����ͬ���ص㡣���еĿ��ܽ��ܲ����������Ϊʲôfor����һ��$_������������ѭ����ÿ���鶼����һ���Լ��Ĵ���������(lexical scope)�����еĿ鶼�������Ժ�ȥ�洢��ִ�����ǡ������Ŀ�洢����ִ����������ʹ�ô���������Ľṹ����֮ǰ���Ǹո����۹����ƽṹ��ִ�ж�������������Ŀ�֮�ڡ�һ������Ŀ�����Լ�����ִ�У��������洢�ĵ����������Ļ��Ǵ������IJ�����

The fact that all blocks are closures has some implications. Every block can take arguments. This is how for creates a $_ alias for the iterator variable. Every block defines a lexical scope. Every block has the potential to be stored and executed later. Whether a block is stored or executed immediately depends on the structure that uses it. The control structures we've discussed so far all execute their blocks where they're defined. A bare block executes immediately when it's alone, but is stored when it's in an assignment context or passed as a parameter:

δ��ɣ�������

# executed immediately

{

    print "Zaphod";

}



# stored

$closure = {

    print "Trillian";

}

��

my,our,temp,let

my��our������ͬ��ı�����my����һ�������ڵ�ǰ�Ĵ���������lexical scratchpad������our����һ����������ڰ��ķ��ű���

my $lexical_var;

our $package_var;

state�����������ͬmy��࣬state declares a lexical variable similar to my, but instead of reinitializing the value every time the block is executed it preserves the previous value:

state $static_var;

temp and let are not declarations; they are run-time commands to store the current value of a variable so it can be restored later. temp variables always restore their previous value on exiting the lexical scope of the temp, while let variables keep the temporary value, unless the lexical scope of the let is exited under an error condition (an undef or empty-list return value, or an exception):

temp $throwaway;

let $hypothetical;

temp and let don't change the value of the variable, they only store it.

��

�������

����ܸ����������������ƾ����These are called property blocks because they are themselves blocks (i.e., closures), attached as properties on the block that contains them. Property blocks are defined within the block they modify, by an uppercase keyword followed by a block (they're also sometimes called NAMED blocks):

NEXT {

    print "Coming around again."

}

Property blocks aren't executed in sequential order with the other code in the enclosing block��they are stored at compile time and executed at the appropriate point in the control flow. NEXT executes between each iteration of a loop, LAST executes at the end of the final iteration (or simply at the end of an ordinary block). PRE executes before everything else��before all other properties and code in an ordinary block and before the first iteration of a loop. POST executes after everything else��after all code and properties in an ordinary block and after the last iteration of a loop. PRE and POST are intended for assertion checking and cannot have any side effects. CATCH, KEEP, and UNDO are related to exception handling. KEEP and UNDO are variants of LAST and execute after CATCH. KEEP executes when the block exits with no exceptions, or when all exceptions have been trapped and handled; UNDO executes when the block exits with untrapped exceptions. There can be only one CATCH in a block, but there's no limit on the other types of property blocks.

This example prints out its loop variable in the body of the block:

for 1..4 {

    NEXT { print " potato, "; }

    LAST { print "." }

    print;



}

ÿ��ִ��ѭ����ʱ��NEXT�鶼��ִ�У���ӡ "potato"�������һ��ѭ����ʱ�� LAST���ӡһ����š��������Ľ����������ӣ�

1 potato, 2 potato, 3 potato, 4.

Property blocks�����ڴ���������(lexically scoped)�պϿ�(enclosing block)���������ǿ��Է��ʴ���������������壺

for 5..7 -> $count {

    my $potato = "$count potato, ";

    NEXT {

        print $potato;

    }

    LAST {

        print $potato, "more.";

    }

}

���ʵ���������$potato��ÿ��ѭ���������¶��岢����NEXT��LAST���б���ӡ�������������������

5 potato, 6 potato, 7 potato, more.

��

�쳣����

�쳣һ�����������ͣ�һ�������쳣 �����������쳣�����е��쳣�����洢���쳣����$!�С����е��쳣���̳���Exception�ࡣ

�����쳣ʹ��die����use fatal��ʱ��fail�׳����κο���Ҫ�쳣�����ʱ��ֻ��Ҫ�ں�һ��CATCH�顣CATCH��ֻ����$!�Ļ��⣬������򵥵IJ��Է�����ʹ��when�����Ƚ�һ��������֣��鿴���������֣���

CATCH { 

    when Err::Danger { warn "fly away home"; }

}

$!����ͬ��stringify������Ϣ�������ƥ��һ������ʽ��

CATCH {

    when /:w I'm sorry Dave/ { warn "HAL is in the house."; }

}

���CATCH����Ϊһ����ȷ��break����ʽ�˳���������break����when��default�����������Ѳ������쳣�������A when case with a continue statement leaves the exception unhandled, since continue skips the implicit break.���һ���쳣�����CATCH����û�б��������CATCH���ᱻ�Լ��ⲿ�Ŀ��á�

һ��һ���쳣���׳���ִ��ֹͣ�IJ���һֱ��CATCH���ʣ�����ᱻֹͣ��������ִ�п��а����� POST��KEEP��UNDO property blocks�����ǽ���CATCH��֮��ִ�С�

�������Ҫ����һ���쳣�IJ���������԰Ѵ����׳������Χ��һ��try���С�һ��try��д��CATCH������沢���ṩ��Ĭ�ϵ�CATCH������ץ���е��쳣�������Dz����ڳ����쳣��ʱ���Է���undef��tryͬ��Ҳ�DZ��������ֿɵõIJ��֡�

try {

    may_throw_exception( );



    CATCH {

        when Error::Moof { warn "Caught a Moof error."; }

    }

}

���������е��쳣��ʱҲ���ڿ�����û�д����ʱ�������������nextȥֹͣʣ�ಿ�ֵĴ��벢�ҿ�ʼ��һ��ѭ������ʵ�����Ѿ��׳�һ�������쳣�������쳣һ������next��last��ѭ�����׳��ġ���һ��return�쳣�����ӳ���򷽷��׳��ġ�

����Perl6�Ŀ�������

�ǵģ���ֹ���ڻ�û��һ����ƷPerl6����������#��%��������������

��������Ϊ������������˵�ɡ��ѵ���ֻԸ��һ���Ӷ�ʹ�ñ�����Ƶ�����������Ը�����Perl6�Ŀ�����

�����ܻ����Լ��ܷ���뿪�����顣Ҫ���������ǣ���ʹ��ֻ�ܲ���1+1=2��Ҳ��Ϊ�����Ŀ�����޴�ķ��ס������ϣ���ܾ����ʹ�õ�Perl6�μ����ǵĹ�������õ�ѡ��

�Ƚ����˵���Perl6һ����Ҫ����ĿPugs����Haskell������Ƶģ��Ѿ���ʼ������Perl6�����ˡ�������ȫ�����ƹ�Perl��̨���������Autrijus Tang���������Ŀ����������Perlchina��������һ��ϣ�����и�����˼��������Ŀ��

������ΪPerl6��Ƶ�һ�������ļ���ȡ������һ��MYSQL���ӳ��򣬵�һ��Templateϵͳ����һ��CGI�������ȵȣ����ǰ����е�CPAN�ᵽPerl6�ϣ��������˵���������¡�һ����Щ��������Pugs���С�����������ҵ�Pugs��Perl6�����㣬Perl6���Ѿ�������ˡ�

�����߶�Pugs����Ȥ����һ�����ܣ���IRC��½irc.freenode.net��#perlchina����������ҵ�Autrijus Tang�������������������Ŷ����

����β

����������ܻ���ô��ʱ�俴��һƪ�������