����� 1� ���� �����:
���������� ����� Perl ����������� �������� ������� �������� �� ������ Perl ���������� ��������������� �������� ����� Perl ����� Perl �������� ������������� ��������� Practical Extraction and Report Language (������������ ���� ���������� � �������), ���� ������ ��� �������� Pathologically Eclectic Rubbish Lister (������������� ����������� �������� ������). �� ����� ������� � ���, ����� �� ���� �������� ����� ����������, ������ ��� ��� ��� ����������� ����� �����, ��������� � �������� �����������, ���������������� � ������� ����� Perl. ����� ������ ���� ����, ����� ������� ����������� ������ �� �������� ������ ������� ���������� �� �������, ������� �� Usenet-�������, � ����������� �������������� � �� ����� ����������� ������� ������ awk ��������� ������������. ������ ��������� (�� ���� �������) �������������, ����� ����� ������� ������ �������� � ������, �������� ��� ����� �����-������ ������������� ����������, ������� �� �������� ������������ � � ����������. � ���������� ��������� ������ ������ ����� Perl. ������������� ������� � ���� �������, ������� ���-���, ����� ��������� �� ���������� ��������� ���������� ��������������� Usenet, ���������� ����� ��� "����" (the Net). ������������, ������� ������ � �������� � ������� Usenet �����������, ������������ �� ����� ����� (� �� � �� ����� ���� ��������� �������� �����), ���������� ��� ��������� Perl ����������� "�������� �����", ���������, ��� ������ ����, ������, ������. ������ �� ���� ����� ����� ���� � �� ��������� ������� ����� ����� ��������� ����� ������ ����������������. � ���������� Perl ��� ��� � ���, ������ ����� � ��� �� ���������, ��� � ������������ ������� UNIX. (���������� ��� ��������: ��� ���� UNIX ����� ��������� ������ ������� 32 �! ������ �� ���������, ���� ��� ������� �������� ��� � ��������� ����������.) ������� � ��� �����������.
���������� �������������. ��, ��� �����-�� ���� ���������� ������, ������ �������������� ������� ������� ������������, � ������ ������� ������ ������� man-�������, 600-���������� ���������� ����� Nutshell, ��������� ��������� ��������������� Usenet � 200000 �����������, � � ������ ��� � ��� �������� �����. ����� ��� �� ������������ Perl � ��������, �� ��������� ���� ������������ ����� �������� �����������. A Perl ��� ������ � ������. ������� ��������, ������������ � ���� �����, �� ��������� �� Perl ������ 5.004 (�� ������ ��������� ����� ��� ��� ����� ��������� ������). ��� ���������, ������� ����� ���������������, ������ �������� � Perl ������ 5.0 � �� ����� ������������ ��������. �� ���� ����, ���� ���������, ���������� �� ����� Perl ������ 1.0, �������� ������� �������� � ���������� ��������, ���� ������ �� ������������ � ���������� �������������� ��������������, ���������� �� ��� ���������. ���������� ����� Perl ���������� ����� Perl � ������ ������������ � ���������� �������� �����, ������� ��� shell ������� ������ ��� ����� ����������, � ����� �������� ������, ���������� ��� ������ ��� ����������� �� � ��� ���� ������������ � UNIX �����. ���������� ������������ ������ Perl, ��, ��������, ����������, ��� ��������� ������� ������ ������� �� ���������� ���������� � ������� ��������� ���������� shell (��� �� ���������� ���������� �-����������), � ������ � �� ������ Usenet-�������� � ������� � ��� �� �����, ������ ��� Perl � ������������� �������� ��� ������ ����������������� ��� ������������. ������ ����������� ����� ����� ��������� ��������� (� ����������� �������� ���) ��������� ����� ����������� ������������������ ������� � ������������� �����������. ��� ����������� ����� ������������ � � ����������, ������ ��� ���������� �� Perl ��������� ���������� ������� �������������� � ����������� � �������������. � ���������� � ��� �������� ��� ������ ������� ��� ������ Usenet-�������� � ��������� � �������� ����� �������. ��� � ����� ����, Perl ����� ���� ������ "������_���_���������" ��������, ������� ����� ����� ���������� ���������. ������ ��� ���������� ������� �� ������ �������� ����� ������ ����������������� ����������. ��, ������ Perl-����� �������� ��� ������������� ��� ��������� ����� ��������, �� ���������� ������ Perl-����������� �����, ��� � ����� ������ ���� ����������� ����� � ������ ��� ������ ����� ���� ��������������. ���� �� ������ ��������� ���������, ����������� � ����� �����, ���� ��������� ����� ������� ��� ������ � �������� ��� ������������� � ��, ��������, �� �������� ������� "���������� �� ���������".
����������� ���� ��� ������� ������� Perl �� sneil �� �������� ��������� perl: not found ��� ������, ��� ������ ���������� �������������� ��� �� �������� Perl-���������. ���� ���� Perl �� ������������� � ����� �������, ��� ����� �������� ��������� (��� ����� ���������). Perl ���������������� �� �������� �������� GNU (GNU Public License)*, �������� ������� "�� ������ �������������� �������� ����� ����� Perl ������ � ��� ������, ���� �������������� �������� ��� ���������, � ���� �� ������������� ��, �� ������ �������������� � ��� ���� ���������". �� ���� ���� ��� ��������, ��� Perl ���������������� ���������. ��� �������� ����� ����� �������� �� ���� ������ ����� ��� ������� ��������� �������� ���������� ���������� ���������� �� ���������� �����. ��� ���� ����� �� ����� "����������" ��� Perl � ������� ��� ������ �������� �����, ��������������� �����-���� ����������� ������������� � "�������������� ���������� ����������". �� ���� ����, Perl �� ������ ���������, �� � �������� ���������� ������ ����� �� ����, ��� �������� ���� UNIX ��� UNIX-�������� �������� � �������� �-����������. ��� ����������� ���, ��� ����� ���������������� � ���������� ���������� ������������, ���������� Configure, ������� ����� � ����� �� ��������� ��������� � ������� ������ �� �����, ��������������� ������� ������������ ������������ ����� � ������������ �������, ��������� � ��� �� �������������� ����������� ����� ���������. ������������ ��������� ��������� ������ Perl, ���, ������ UNIX- � UNIX-�������� ������, ������ ������������ ��� � � �������� Amiga, Atari ST, �������� ��������� Macintosh, VMS, OS/2, ���� MS-DOS �, �������, � Windows NT � Windows 95. � ���� �������, ����� �� ������ ������ ��� ������, Perl, ��������, ��������� � �� ������ ������ �������. �������� ������ ����� Perl (� ������ ������������������� �������� ����� ��� He-UNIX-����������) ����� �������� �� ����� �� �������� ���� CPAN (Comprehensive Perl Archive Network). ���� �� ������ ������ � World Wide Web, �������� ������ hftp://www.perl.com/CPAN, ���������� ����� �� ��������� "����������" (�����������) ��������. ���� �� ���������� �������, ��������� �� ������ bookquestions@ora.com �������� � �������� "��� ����� �������� Perl?!?!" ^Where 1 ��� set Perl?!?!"). ��� �� ��������� ����� ����������� "�������������� ��������" (Artistic License), �������� ������� Perl ���������������� � ���� ������������.
�������� ������� �������� shell � ��� �� ��� ����, ��� ������������������ ������ shell, ����������� � ���� ���������� �����. ���� ���� ����� ������������ � ����������� ����� ��������� ���� ���������� (����������� ������� chmod + � ���_�����), ����� ���� �� ����������� shell �������� ��� �����. ������� ���������� �����-������ �������� shell. ��������, �������� ���������� ������� date, � ����� ������� who ����� �������� � ��������� ���: % echo date >somescript % echo who �somescript % cat somescript date who ' % chmod +x somescript % somescript [��������� ���������� ������� date, ����� ������� who] % ����������� ������� Perl-��������� � ��� ����� Perl-���������� � �����������, ���������� � ���� �����. ����� ���������� ��� ����������*, ����� ���� �� ����������� shell �������� ��� �����. ��� ����, ������, ���� ������ ����� �������, �����������, ��� ��� Perl-���������, � �� ��������� shell. � ����������� ������� �������� �������� ������ �������� ����������� � ��������� � ������ ����� ������ #!/usr/bin/perl ���� �� ��� Perl ������������� � �����-�� ������������� �������� ��� ���� ���� ������� "�� ��������" ������, ������������ � �������� #!, �������� ������� ���-��� ���. ������� �� ���� � ����, ��� ������������� ��� Perl. � ��������, ����������� � �����, ��������������, ��� �� ����������� ������ ����, ������������ ���������� ����������� Perl-��������. Perl � ���, � ��������, ���� �� ��������� �������� ������ �������� (����� �) � ���������� �������, ���������� ����� ��������� (���������� ���������, �������� print ��� +), �� �����������, ���� ��� ����� ������� ������� ���������� ������� �� �����-�� ������ �������. � ��������� ������ �����-������ ���������� ������ �������� ������������. (� ���������� �������� ��������� �������, ����� ���������, ������� ����� ������, ������� �������� �������, ������� �������� �� ����� ��������.) ������� ����� ��� �����������, � ������� ��������� ������������ ������������ ���������� ������ � ������������ �����, �� �� ���� �� ���������, ����� ������ �� �� ��������. �� ������ �������, ��� ��� � * ��� ����������� ��� UNIX-������. �������� � ���, ��� ������� �������� ����������� � ������ ��������, �� ������ ����� � �������� ������� �� ����� ���������� ������� (FAQ) �� Perl ��� � ������������, ����������� � ����� ������������ �������.
���������� ���������� �������� ����� ��������� �� ���� ������ ������� ����� ���� �������������. ���� ����� ������ Perl-��������� ����� �������� � ���� ������, ��� ��������� ������ ����� � ���������, ��� �-���������, ������ ��������� ��������� ���������� � ������� ��������, ��� ������������. � ���� ����� �� ������� ��������� ��������, ���������� � ��������� ��� ����� Perl ���������. ���������� �������� shell, Perl-��������� ������� �� ���� ���������� Perl, ��������� � ����� � ��������������� � ������������ ��� ���� ������� ���������, ���������� ����������. ������� "��������" (main) ���������, ��� � �, ����� ���. ����������� � Perl ������ �� ����������� shell (�����������). ������������ �������� ���, ��� ������� �� ������������� � ������� ������ # ������ �� ����� ������. ������������� ������������, ��� � �, ����� ���. � ������� �� ����������� shell (�� ���������� awk � sed), ������������� ����� Perl ����� ����������� ��������� ��������� ��������� �� � ����������� � ���� ���������� ������. ��� ������, ��� ����� ������� ��������� �� ������� �� �������� ��������� � �������������� ������ � ��� ���������� ������� � ����������� �� ��������� ��� ���������� ���������. ����� ����� ������������ ������� ���������� �������� ����� Perl ����� ������� � �������� �������������� �������� � ������ �� ������������� � � �������� ���������� ����� ������ ���� �� ��� ���������, ��� � � ������������� ����. �� ��������� ���������� ��� �� ������� �������, � ���������� ������� Perl-���������, ������� ������ ��������� ���� ��������� ������ (�� ��������� ���, ������� ��� �������� ���������), � ����� ����������� ���� ������, �� ����� �����������, ��� ����� �� ���������� �������� �������� ����� �� ��������� �� �������� ����������. Perl, ����� �������, �������� � ��� ����������, � ��� �������������. � ����� �������, ��� ����������, ������ ��� ����� ����������� ������� ��������� ��������� ��� ��������� ����������� � �����������. � ������ �������. Perl � �������������, ������ ��� �������� ���������� ����, ����������� ����� �� ����� � �������� ����������, � ������ ������ ���. ������� �������, �� �������� � ���� ������ �� ����������� � ��������������. ������� ��, ���� �� ������ �������, ���� �� ����������� �����-�� ����������� ���������������� ���������� ���� ����� ��������, � �� � ��� ���������� � "������" �������� ���. ������� ������ ������ ����������� ���������� ��� ����������, � ������ �����������, ��� ��� ������ � ������ 5.005. � ������� ��������� ��� ����� ������ � �������� FAQ, ����������� Perl.
�������� �� ������ Perl ���� ����������� �� ������ Perl �� ������ � ��������� ��������. � ���� ���� �������� �� ����������� � ���������� ������������� ����� Perl �� ������� ���������� ����������. ����� ��������� ���� ����� ������� ���������; ������ ���� ������� ��������� ���������� � ��������������� �����. ��� �� ����� ��� �������� �������� ������ ���� ��� ����������� ������ "�������������" ���� ����, � �� ������� ������, ������ �� �� ���������� ����� �� ����� ��� �� ����������� ������� � ����� Usenet-�������� � ������ �������. ��������� Hello, World ������� ���������� ��������� ���������, ������� ������ ���-�� ��������. ��� ���� ������� ���������, ������� ������� �� ����� ����� "Hello, World": ^! /usr/bin/perl -w print ("Hello, World\n") ; ������ ������ ������� � ���, ��� ��� ��������� �������� �� ����� Perl. ����� ����, ������ ������ �������� ������������; ���� ������������ � Perl, ��� � �� ������ ���������������� ������ ����������������, �������� ��� �������, ������� ����� ����� # � �� ����� ������� ������. ��, � ������� �� ���� ��������� ������������, ���������� � ��� ���������, ����������� � ������ ������ ���������: Perl ���� ����� �������������� ���������. � ������ ������ ������������� ���� -w. ���� ����� ������ ���� ���� Perl �������� �������� �������������� ��������������� ��������� � ������������ ������� ������������. ��� ������� ������ ������������ � ����� ���������� ���� -w. ������ ������ � ��� ����������� ����� ������ ���������. ����� �� ����� ������� print. � ������ ������ ���������� ������� print ����� ����� ���� ��������, �-�������� ��������� ������. � ���� ������ ���������� �������� \� ���������� ������ ����� ������. �������� print ����������� ������ � ������� (;). ��� � � �, ��� ������� ��������� � Perl ����������� ������ � �������*. ����� �� ��������� ��� ���������, ���� ��������� ������������� Perl, ������� ��������� ��� ��������� (��� ������, ������� ������, �.�. �����������), � ����� ��������� ��������������� �������. ������ � ������������ �������� � ���������� ������� print, ������� �������� �������� ����� ���������� �� ���������� ������. �� ��������� ���������� ��������� ���� Perl-������� ����������� � ���������� ������������� shell ��� ��������� ����������. * ����� � ������� ����� ��������, ���� �������� �������� ��������� ���������� � ����� ��� ����� ���� ���������� eval.
����� �� ������� Perl-���������, � ������� print � ������ ������� ������ ���������� � �������� ��������, � ������ � ��� ���. ������� ����� �������: ������� ������ ��� ���������� ������� Perl �� �������� �� �������������, �� ������������. �� ���������� ����� ��������� ��������, � ����� � �������� ��, ��� ��� ����������� ����������� �����. ��� �������� ������� � ���������� ��������� ������� ������� �������� ������, ���� ����������� Hello, World � ������� ������� � ���������. ������� ���, ����� ��������� �������� ��� �� �����. ��� ����� ��� ����� ����� ��� �������� �����, ������ ������� ������� �� ����� � ������ ��������� ������. ���� �� ���� ��� �������� �������� (����� �����) � ��������� ����������. ��� �������� ������ ����� � ����� ��������� �� ���������� ��������� ���������� $name. � ����� 2, "��������� ������", �� ����� �������� ������ � ���, ��� ����� ������� � ���� ���������� � ��� ����� � ���� ������. ���� �� �����������, ��� �� ����� ������� � ��������� ���������� ������ ���� ����� ��� ������ (������������������ ��������). ��������� ������ ����� ����������� �������� � ��� ���. ��� ����� ��� ����� ������ ������ ����������� � ������ �������� �� ��� ������. ���������� ��������� �������� ���, ��� ����� ����������, ��������� ��� ����� ������� print. ��������� �� ������ � ��������� �������������� � ������� ����������� <stdin>, ������� (� ����� ������) �������� ���� ������ ��������� ������. ��������� ������ �� ����������� ���������� $ name. ��� ���� ��� ��������� ���������: print "What is your name? "; # ��� ���� ���? $name = <STDIN>; �������� ���������� $name ���� �������� ����������� ������ ����� ������ (��� Randai ��������� ��� Randal\n). ����� ���������� �� ����� �������, �� ������������� �������� chomp, ������� � �������� ������ ������������� ��������� ��������� ��������� ���������� � ������� �� �� ���������� �������� ����������� ������ �������� �� ����� ������ (���������� ������), ���� �� ������������: chomp ($name) ; ������ ��� ����� ���� ������� Hello � ������� �������� ���������� $name, ��� �� ����� ������� ���, ��� � shell, �������� ��� ���������� � ����������� � ������� ������: orint "Hello, $name ! \n"; ��� � � shell, ���� ��� ����� ������ ���� �������, � �� ������ �� ��������� ����������, �� ����� ���������� ���� ���� �������� ����� ������.
������ ��� ������, ��������: �ft! /usr/bin/perl -w print "What is your name? " ; $name = <STDIN>; chomp ($name) ; print "Hello, Sname ' \n" ; ��������� ����������� ������ �������� ������, ��� � ��� ��������� �����-�� ������ ����������� ��� ������������ �� ����� ������, � ��� ��������� � �������. ��� ����� ��� ����� �������� ���, ������� ���� �������, �� ������� Randal, �, ���� ��� ���������, ������� ���-�� ������. ������� ������� � ��������� �-�������� ����� tf-then-eise � �������� ���������: #!/usr/bin/perl print "What is your name? "; $name = <STDIN>; chomp ($name} ; if ($name eq "Randal") { print "Hello, Randal! How good of you to be here!\n"; } else { print "Hello, $name! \n"; # ������� ����������� � �������� eq ������������ ��� ������. ���� ��� ����� (�.�. ��������� ��� ������� � ����� ����� ���������), ����������� ����� "������". (� � � C++ �������� �������� ���*). �������� if ��������, ����� ���� ���������� (����������� ����� ������� ��������� ��������) �����������; ���� ��������� ���� � ���������� �������� "������", ����������� ������ ����, � ��������� ������ ����������� ������ ����. ��������� ����� ������, ����� � ��� ���� ���, ������� ������� ���, ����� �������, ����������� ��� ���������, �������� ��������� �����. � ����, ����� �������, ��������� ����� ���������� ��������� ������ ��� �����, ���� ������������ �� ������� ����� ���������. ������� �� �������� ����� ���������, ����� ����� ��������� � ���: #! /usr/bin/perl -w $secretword = "llama"; # ��������� ����� print "What is your name? "; $name = <STDIN>; chomp $name; if ($name eq "Randal") { print "Hello, Randal! How good of you to be here!\n"; * ��� ��������� ������������ ���������� ����� ������������ ����������� ������������ libc. �� ��� �� ��������.
} else { print "Hello, $name ' \n"; # ������� ����������� print "What is the secret word? "; $guess = <STDIN>; chomp ($guess) ; while ($guess ne $secretword) { print "Wrong, try again. What is the secret worcl.�' '� ; $guess = <STDIN>; chomp ($guess) ; ������� �� ������ ��������� �����, ������� ��� � ��������� ���������� $secretword. ����� ����������� ��������� ���������� (����������� ������ ��� ����� ������� print) � ������������ (�� �������) ��� ������� ���������� �����. ���� ������� ������������ � �������� ��������� ������ � �������� ne. ������ �������� ���������� �������� "������", ���� ������������ ������ �� ����� (�.�. ������ �������� �������������� �������� eq). ��������� ��������� ��������� ������ while, ������� ��������� ���� ���� ���������� �� ��� ���, ���� ��������� ���� �������� "������". �������, ��� ��������� ����� ��������, ������ ��� �����, ���� ������� ��������� ��������� �����, ����� ������ �������� �� ���������� � ��������� � �����������, � �� � ����������� ��������� ����� � �������� ������. ��, ������, �� �������� ����������� ������� ����������� ������������, � ���� ������ �������� ���������� ��� ������� ������� ������. ��������� ��������� ���� ������� ���������, ��� ����� �������������� ��� ��������� ���, ����� ��� ��������� ��������� ��������� ����. ��������� ��, ��� �� ��� ������, ����� ���� �� ����������� ���������� �������-������� � ����� ���������� �������, ���������� � ��������� ��������� ����������. ����� ������, ������, ���� �� ������ �������������� ��� �������������� � ����������� �� ��� ������ � ����. ����� ����������� ������� � ������� ��� ��������� ������ � ��������� ������, ������� ���������� ������, ��� (����������������) ������. ������ ������� ������� � ��� ��������� ��������� ����������, ������� ����� ����������� �������� � ����� ������������ �� ���������� �� ������. ����� ����� ����� ����� ��������� �������� ����� �������. �� ����� ����� ��������� �������� ����� ������� � ������ @words ���, ����� �� �������� ��� ��������� ���������� ������: @words = ("camel", "llarna", "alpaca"); ����� ����������-�������� ���������� � ������� @, ��� ��������� �������� �� �� ���� ��������� ����������. ���������� ��� ���� ������ ������ ���� ����������� ���, ����� �� ����� ���� ������� ��� ��� ������� � � ������� �������� qw ( ) , ��������: @words = qw (camel llama alpaca) ;
��� ��������� �� �� �����; �������� qw �������� ���, ��� ����� �� ����� � ������� ������ �� ���� �����. �������� �������� ��������� �������, �� ����� ���������� � ������� �� ��� �� ��������� ������. ���, $words [0] � ��� camel, $words [1] � llama, a $words [2] � alpaca. ������ ����� ���� � ����������, ������� ���� �� �������� $i �������� 2, �� ��������� $words [$i] ����� alpaca. (��������� ������ ���������� � ������� $, � ��� @, ������ ��� ��� ���������� ���� ������� �������, � �� ���� ������.) �������� � ������ ����������� �������: #! /usr/bin/perl -w @words == qw (camel llama alpaca); print "What is your name? " ; $name = <STDIN>; chomp ($name) ; if ($name eq "Randal") { print "Hello, Randal! How good of you to be here!\n"; } else { print "Hello, $name ! \n"; # ������� ����������� print "What is the secret word? "; $guess = <STDIN>; chomp ($guess) ; $i = 0; # ������� ��������� ��� ����� $correct = "maybe"; # ������� ����� ��� ���? while ($correct eq "maybe") { # ���������� �������� if ($words [$i] eq $guess) { # �����? $correct = "yes"; # ��! } elsif ($i < 2) { # �������� ��� �����? $i==$i+l; #� ��������� ��� ���������� ��������� ����� } else { # ������ ���� ���, ������ ����, ������� print "Wrong, try again. What is the secret word?"; $guess = <STDIN>; chomp ($guess) ; $i = 0; # ����� ������ �������� � ������� ����� } } # ����� ����� while ��� �������� ���� } # ����� ����� "�� ������" ��������, ��� �� ���������� ��������� ���������� $correct ��� ����, ����� ��������, ��� ��� ���� �� ���������� ������ ��� ��� ����� ���. � ���� ��������� ������� ����� ���� eisif ��������� if-then-eise. ����� ����������� ��� �� � ����� ������ ����� ����������������; ��� ����������� ������ ����� else � ����� �������� if, �� ��� �������� ��� ����� ���� �������� ������. ��������� ������ ������� � ��������� ������� if-eisif-eisif-eisif-eise ����� ���������� ��� ����� Perl. � ��� ��� ����������� ��������� switch ����� � ��� ��������� case ����� �������, �� �� ������ ���� ��� ������ ������ ������� ����� ��������. ����������� ��. � ����� 2 ����� Programming Perl � �� �������� ����������� perlsyn(1).
������ ��������� ����� ��� ������ ������������� � ���������� ������ ����� ������������ ��� ������� ���� �� ���� ��������� ���� � �������� ������ � ���������. ���� �� �����, ����� ��� ������� ������������ ���� ������ ���� ��������� �����, ��� ����� �������� ����� ������� ������������:
�������� ��������: � ��������� ���� ������������� ���������� ��������� �����. ����� �����������. ����� ������� ������ ��������� ����� ������� � ������������ ���. � ������ �������� ���� ���������� ��������� ��������� �������� (��� � � ������� ������ ������� ����), �� � ������������ ������� �������� ���� �������� ����. ������ ����� ���� ����� ��������� �������� (����� ������ ��� �����, � ��� ����� ������� � ������������� �����). ����� ������� ��� ��� ������ Swords (�������� �������� �� ��, ��� ������������ ������ ^ ������ @) � ������� � ����������, ������� � ����������� ���� �������, �� �������� Swords �������� (����� ��� ��, ��� �� ������ ������ � ��������): Swords = qw( fred camel barney llama betty alpaca wilma alpaca ); ������ ���� � ���� ������ ������������ � ���� ���� ���� � ��������������� ��� ��������. �������� �������� �� ��, ��� �� ������� ��� ��������� ������������ �� ��������� ����� ��� �����-���� �������� ����������� �����, ������ ��� ���������� ������� � Perl-���������� ������ ������� ���� �� ������. ����� ����� ��������� ����� ��� Betty, �� ������ ������������ ��� Betty ��� ���� � ������ �� ��� Swords � ������� ��������� ����� $words { "betty"}. �������� ���� ������ - alpaca, ��� ������ �� ��, ��� �� ������ ������, ������� � ������ ��������. ��� � ������, ������ ����� ���� ����� ���������, ������� ��������� $person � �������� betty � ���������� $words { $person} ����� ���� � ���������� alpaca.
����� ��� ��� �������, �� �������� ����� ���������: #! /usr/bin/perl -w %words = qw ( fred camel barney llama betty alpaca wilma alpaca ) ; print "What is your name? "; $name = <STDIN>; chomp ( $name) ; if ($name eq "Randal") { print "Hello, Randal! How good of you to be here!\n"; } else { print "Hello, $name !\n"; # ������� ����������� $secretword = $words {$name}; # �������� ��������� ����� print "What is the secret word? "; $guess = <STDIN>; chomp ($guess) ; while ($guess ne $secretword) { print "Wrong, try again. What is the secret word? "; $guess = <STDIN>; chomp ($guess) ; } } �������� �������� �� ��, ��� ���������� ����� ���������� �����. ���� ��� �� �������, �� ��������� ���������� $secretword ����� ������ ������*, � �� ����� ������������ �������� if, ����� ������ ��������� ����� �� ��������� ��� ����-������ ���. ��� ��� ��� ��������: [ . .. ��������� ����� ��������� ������� . . . ] $secretword = $words ($name}; # �������� ��������� ����� if ( $secretword eq "") { # �� ������� $secretword = "groucho"; # �������, #����� ������������ } print "What is the secret word? "; [. .. ��������� ����� ��������� ������� .. . ] ��������� ��������� ��������� ����� ���������� ����� ���� ������ Randai �� ������� Randai L. Schwartz ��� randal, �� ��� ����� ������ ������� ����� �� ������ �����������, ������ ��� ��������� eq ������������ ������ ���������. ������� ���������� ���� ������ ������� ������ ��������� ��������� ��������� �����. * �� ����� ���� ��� �������� undef, �� ��� �������� eq ��� �������� ��� ������ ������. ���� �� � ��������� ������ �� ������������ ���� -w, �� �������� �� �������������� �� ���� ����. ������ ������� �� ��� ����� ��������.
��������, �� ������ ����� ��� ������, ������� ���������� �� ����� Randal, � �� ������ ������, ������ Randal. � sed, awk ��� grep ��� ����� ������� � ������� ����������� ��������� � �������, ������������� ������������ ��������������� �����. ��� � � sed, awk ��� grep, � Perl ���������� ����������, ������� ������������� ����� ������, ������������ �� ����� Randal, ����� ^Randai. ����� �������� ��� �� �������, ������������ � ��������� ���������� $ name, �� ���������� �������� �������������: if ($name =~ /^Randal/) ( +f ��, ��������� ) else ( ## ���, �� ��������� �������� �������� �� ��, ��� ���������� ��������� ���������� ����� ������ � ����� ������. ������� � ������ ���������� �������, ����������� ����� ������, ����� ��������, ��������� ��� �������� ������ ������. ��� ����� ������ ���� ������, �� �� ��������� ������� randal ��� ��������� Randall. ����� ������� randal, �� ��������� ����� ������������� �������� � ��������� ����� i ����� ����������� �����. ����� ��������� Randall, �� ������ � ���������� ��������� ����������� ������ ������� ����� (������� ���� ��� ��� �������� � vi � � ��������� ������� grep) � ����� \b. ��� �����������, ��� ������, ��������� � ���������� ��������� �� ������ ������ 1, �� �������� ��� ����� ������. � ���������� ���� ���������� ��������� ��������� ��� /^randal\b/i, ��� �������� "����� randal, ������� � ������ ������, �� ������� ��� �� �����, �� �����, ��� ���� ������� �� ����� ��������". ��������� ���� �������� � ��������� ������ ���������, �������: #! /usr/bin/perl %words = qw ( fred camel barney llama betty alpaca wilma alpaca ); print "What is your name? "; $name = <STDIN>; chomp ($name); if ($name =~ /^randal\b/i) { print "Hello, Randal! How good of you to be here!\n"; } else { print "Hello, $name! \n"; # ������� ����������� $secretword = $words {$name}; # �������� ��������� ����� if ($secretword eq "") { # �� ������� $secretword = "groucho"; t �������, ����� ������������ } print "What is the secret word? "; $guess = <STDIN>;
chomp ($guess , while ($guess ne Ssecretword) ( print "Wrong, try again. What is the secret word? "; $guess = <STDIN> ; chomp ($guess) ; ��� ������, ��� ��������� ��� �������� ������ �� ����������� Hello, World. ���� ��� � ����� ����, �� ������ ��������������, ������ ��������� ��������� ����������� ������ ���������� ��������. � ���� � ����� Perl. � Perl ������� ���, ��� ���������� ��� ������ � ����������� �����������, �.�. �� ������������� ��� �����������, ������� ������������ ����� ����������� ������� UNIX (� ���� ��������� �������������). ������ ������������� �����, ������������ � Perl, ��������, ���� �� �� ����� ������� ������������ � ������� �������, ������� ������������������ ������� ��� ���������� Perl-�������� ������ ������� �� ���������. (���������� �� Perl grep-�������� ��������� ����� ����������� ����������� ������������ ��������� grep �� �*. ��� ������, ��� grep �� ��������� ������ ���� ������������ ���� ������.) �������������� ��� ���� ����, ������ � ���� ������ Randal, randal ��� Randal L. Schwartz, �� ��� ���� � ����������? ����� ������ ������� � �������� barney (��� ������ ������ ���� ������ ����� barney). ����� ���� ������������� �� ��������� � �����, �� ������ ����� ������� ����� � ������� ����� ������ ����� �� ����, ��� �������, � ����� �������� ��� ��� ������� ��������� ������� ��������. ��� �������� � ������� ���� �������� � �������� �����������, ������� ������� ���������� ��������� � �������� ��� �������, � �������� ��������, ������� ��������� ������� ���� ������ � ������ �������. ������� � �������� �����������: �� ����� ����� ���������� ���������� $ name, ����� ������ ����������� (�� �������������� � ������) ������ � ������ ��� �������, ������� � ���� �����, ��� �� �����, � �� ����� ������. ������� ���������� ��������� ����� ��� /\w.*/. ����� \w ���������� ����������� ������ (�.�. ��� ����� �����, ����� � ����� �������������), � . * ���������� ����� ������� � ����� ����� �� ����� ������. ����� ������ ��� �������, ����� ����� �� ����� ������, ������� ��������� � ��������������� ���������� ����������, � �������� �� ������ �������: $name =~ s/\W.*//; �� ���������� �� �� �������� =~, ��� � ������, �� ������ � ��� ������ ����� �������� ����������� � ����� s, �� ������� ������� ����������� ����� ����� ������ ���������� ��������� � ������. (������ � ������ ������ GNU-������ ������� egrep ��������� ��� �������� ������� �������, ��� Perl. ������� � ��� ������ ������ ����� ������ � ������� ������.) ��� �������� �������� � ����������� �� ������ ��� ��, ��� �������� ����������� � ����������-����������. ������ ��� ����, ����� ��������� ��� ���������� ������� � ������ �������, �� ����������� ��� ������ � ������� �������� tr*. ��� ����� ������ �� UNIX-������� tr, �.�. �������� ������ ������� �������� � ������ ��������, �������� ������� ������� ����������. � ����� ������� ��, ����� ��������� ���������� ���������� $name � ������ �������, ���������� ����� ������: $name =~ �tr/A-Z/a-z/; ����� ������ ��������� ������ ������� � ���������� �� ��������. ����� ����� ������� � � z ���������� ��� �������, ����������� ����� ����, �.�. � ��� ���� ��� ������, � ������ �� ������� �������� �� 26 ��������. ����� tr ������� ������ �� �����-���� ������ ������� ������, �� ���������� ��������������� �������� �� ������� ������. � ���������� ��� ��������� ����� �, �, � � �.�. ���������� ���������**. ��������� ��� ������ � ��������� ������ ���������, ��������:' #!/usr/bin/perl Swords " qw ( fred camel bamey llama betty alpaca wilma alpaca print "What is your name? "; $name = <STDIN>; chomp ($name); $original name = $name; # ��������� ��� ����������� $name =~ s/\W.*//; # ���������� �� ���� ��������, ��������� ����� ������� ����� $name =~ tr/A-Z/a-z/; # ��������� ��� � ������ ������� if ($name eq "randal") ( * ������ ����� ��� �������� print "Hello, Randal! How good of you to be here!\n"; else ( print "Hello, $original_name! \n"; ^������� ����������� $secretword = $words($namel; # �������� ��������� ����� if ($secretword eq "") ( # �� ������� $secretword == "groucho"; 4 �������, ����� ������������ } print "What is the secret word? "; $guess = <STDIN>; chomp ($guess); while ($guess ne $secretword) ( * ������� � ��������������� ������� ������ �������� �� ���������. ����������� ��. �� man-�������� ���11���1�(\) � ������ ����� Pel-15.004. ** ����������� ������� ,��� �� ����� ����� �������� ����� ������/(\3*) .*/\L$1/, ����� ������� ��� ��� �� ���� �������, �� �����������, ��������, �� ����� ������ ������ ������.
print "Wrong, try again. What is the secret word? "; $guess = <STDIN>; chomp ($guess); �������� �������� �� ��, ��� ������������� � ���������� ���������� ��� ����� Randai ����� ����������� � ������� ������� �������� ���������. ���� ����, ��� � RandaL L. Schwartz, � Randai ����� ����������� � �������� ������������ � randai. ��� ���� ��������� ������������� ����������� �� �� �����, ������ ��� Fred � Fred Flinstone ������������ �� fred; Barney Rubbie � Barney, the little guy � � barney � �.�. ����, ��������� ����� ���������� ���������� ���� ��������� ����� ������� ����� �����������. �� �������, ��� ���������� ������� ����������� �� �������� ����������� ����� ���� ���������� ������� ������ � ���� �� ������ ������� ������ ����� Perl. �������, ������, ��� � �������� ��������� ����� (�.�. ��� ��� �����������, ����������� ��� ���������� �������� ��������� � ������ ������������ � �������) ������������� ��������� ��� ������������. ������� ����� ���������� ����� ��������� ��������� ��� � ���������� $original name. (��� � ����� � �, ����� ���������� � Perl ������� �� ����, ���� � ������ �������������, ������ ����� �� ����������� �� ����������.) ��������� ����� �� ������������ ������ ��������� �� $ original name. � Perl ������� ����� ��������, ����������� ��������� ������ � ��������� �������� � �������. � ������������ �� ��� �� ������������� � ������ 7 � 15. ��������� ������� ����������� ������, ����� �� �������� ��� ����� ����� � ������ ��������������� ����, ��� ��� ��� ��������� ����� �������� ������� ����� ������ ���������� ���������. ������� ���� �� ������� �������� ��������������� ������ (������ �����, �����, ������������ ��� ��������� ��������� ��������� ����) �� �������������� (��������� ���������� ���������� ����� � ��������). ��� ���������� �������, ��������, ��� ���������� ��������� ��������� ������� ��������������, ��� �� ��� �������, ��� ���� ������� ����� ��������������� �����, � ������ � �������������� ���������. � Perl ���������� ������� ������������, ������� ��������� � ������������ ��������. ������������ ������������ � ��������� ���� ���, �� �������������� ����� ����������� ����� ������ �� �� ������ ����� ���������. ������� �������� ��� ����� ���������, �� ������ �������� ��������� ������������ good_word, ������� ����� ��������� ��� � ������� ����� � ���������� �������� "������", ���� ��� ����� ������� ���������, � "����", ���� ����� ������� �����������. ����������� ����� ������������ �������� ��������� �������:
sub good_word ( my($somename,$someguess) = @_; # ������� ��������� $somename =~ s/\W.*//; # ���������� �� ���� ��������, ������� ����� # ������� ����� $somename =~ tr/A-2/a-z/; t ��������� ��� ������� � ������ ������� if ($somename eq "randal") ( # �� ����� ��������� return 1; # ������������ �������� � true I elsif (($words($somename} 11 "groucho") eq $someguess) ( return 1; # ������������ �������� � true ) else { return 0; * ������������ �������� � false ��-������, ����������� ������������ ������� �� ������������������ ��� ���� ����� ����� sub, �� ������� ���� ��� ������������ � ���� �� ���� (���������� ��������� ��������). ��� ����������� ����� ������ � ������ ��������� ��� ������, �� ����������� ������������� �������� ��� � �����. ������ ������ � ������ ���������� ����������� � ��� �������� ������������, � ������� ������� �������� ���� ���������� ������������ ���������� � ��� ��������� ���������� � ������� $somename � $someguess (��������� �� ( ) ���������� ��� ���������� ��� ��������� ��� �����, � ������� ��� ������ (� ������ ������ ��� ���� ������������), � ��������� ������������� ��������� � ����������� ��������� ������� � ������ @ .) ��������� ��� ������ ������� �������, ������� ����� ����� (����� ��� ��, ��� � ���������� ������ ���������). �������� if-elsif-else ��������� ����������, �������� �� ��������� ������������� ������� ����� ($someguess) ������ ��� ����� ($somename). ��� Randal �� ������ ������� � ��� ������������, �� ���� ���� � �������, �� ����� ������� ��� ����� ����� ������ ��� ����������. ��� ���� ����� ������������ ���������� ���������� � ��������� �� ��������� ��������� � ������������ ��������, ����� ��������������� ���������� ��������. � ���������� ������ ��������� ������������ ��������� �������� ��������� ���������, ����������� � ������������. �� ���������, ��� ������������ ������������ ��������, ����� ���� ��� ����� ����������� ������������. �������� � ����� eisif �������� �������� �������; ������� �������� �� �� ���������: ($words($somename) � "groucho") eq $someguess ������ ������� � ������� ������� ������������ ���������� ��� ��������� ��� ���-������, � ���������� �������� ������������ ��������� �������� � ������� %words �� ��������� �����, ����������� �� ������� $somename. ���� 1 1, ������� ����� ���� ��������� � ������� groucho, ���������� �������� ���, ����������� ���, ��� ������������ � ����� �, awk � � ��������� shell. ���� ����� � ���� ���� ��������� �������� (���
������, ��� ���� $ some name ��������� � ����), �� ��� � ����� �������� ��������� ������� ���������. ���� ���� ������ �� ���, �� ������������ ������ groucho. ��� ������ ���������� ��� Perl: ���������� ��������� ���������, � ����� � ������� �������� 1 1 ��� ���� ����������� �������� �� ��������� �� ��� ������, ���� ����������� ������ �������� �������� "����". � ����� ������, ���� �� �������� �� ���� ��� ����������� �� ��������� �������� groucho, �� ���������� ��� � ���������, �������� �������������. ���� ��������� ��������� �����������, ������������ �������, � ��������� ������ ������������ ����. ������� ��� ��� � ���� �������: ���� ��� � randal ��� ���� �������� ������������� ������� ������������� ������ �� ����, ������������ � ������� %words (�� ��������� �� ��������� groucho, ���� ��� � ������� �� �������), �� ������������ ���������� 1; ����� ������������ ���������� 0. ������ ������� ������ ��� ����� ������ � ��������� ������ ���������: #! /usr/bin/perl Swords = qw( fred camel barney llama betty alpaca wilma alpaca ) ; print "What is your name? "; $name = <STDIN>; chomp ($name) ; if ($name "~ /^randal\b/i) ( t ������� �� ������ ���� :-) print "Hello, Randal! How good of you to be here!\n"; I elscj/ { print "Hello, $name! \n"; t ������� ����������� print "What is the secret word? "; $guess = <STDIN>; chomp ($guess); while (! good word( $name, $guess)) ( print "Wrong, try again. What is the secret word? "; ?guess � <STDIN>; chomp ($guess) ; t... ����� ����������� ����������� ������������ good_word() .,,] �������� ��������: �� ����� ��������� � ������������� ����������� ��������� ��� �������� ������� ����� Randal � �������, ������ ��� ������ ��� � �������� ��������� �� ��������� �������� ������ ��� � �������� ��� ��� ������� ��������� ������� ��������. ���������� ������� ���� ��������� �� ���������� ������� � ���, ��� ����� ������������ ���� while, ���������� ������������ &good_word. ��� ������ ���� ������������ �� ���������� ��� ���������, $name � $auess. �������� $somename ��������������� ������ ������� ���������, � ������ ������ $name. ����������� ������� $someguess ���������� �� ������ ���������, $guess. ��������, ������������ ���� ������������� (1 ��� 0, ���� �� ������� ����������� ���� �������), ��������� ������������� ���������� ��������� ! (���������� ��). ��� �������� ���������� �������� "������", ���� ��������� �� ��� ��������� �����, ��� "����", ���� ��� �������. ��������� ���� �������� ��������� ������ while. ������ ������ ����� ������ ��� "�� ��� ���, ���� ����� ������� �����������...". ������ ������ ���������� Perl-��������� ����� ������ �� ������� ���������� ���� � ����, �������, �� ��������� ���� ����� ���������� �� � ������ Perl, �� � ����������. (�� �������������� ������ ���� �� ����� ������� ��������� ��� �� �����.) �������� ��������: ��� ���������� ���� ������������ ��������������, "�� �������� ���� %words �������� � �������� ���������. ����� ���������� ������ � ������������� ���������� ���������� ������ ���, ��� ���������� � ���� ����� ����� ���������. ������ � �����, ����������, �� ��������� � ������� ��, ��������� ��� ���� ���������, ����� ��� ���������� �� ��������� ������ �� ���������� ���������� �����, � ������� ��� ���� ���������. �� �� ������������: � ����� Perl ������� ��������� ������ �������������� ����������, ������� ����������, ��������� ��� ����� (��� ������), � ����������, ��������� ��� �������, ������� ��������� ���� �������� �� ������ � ������ � � ��� ��� ��� ��, ��� �� ����� �� ����� ������������. �������, �� ������ ����� ������ ���������� � Perl �������� ���� ���������� ������ ��������� �� ��� �����. ����� �� ������ ���������� ������ � �����, ����������, ��� ��������� � ����������, �������������, ������� � �������� � ����� Programming Perl, ��� ���������� � ���������� ������������, ��������� �� man-��������� perlsub(\), perlmod(l), perlobJ(l) � perltoot(l). ������� ������ ��������� ���� � ��������� ���� ��������, �� ������ �� ������������ ������ ��������� ���� � ���� ����������. ���� �� ��������� ���� ������ ���, ��� �� ��� ��� ������, ��� �������� �������������� ��� ��� ��������� (����, ��������, ����� �����, ��� �� ��������� ������ ������ ���� �� alpaca, a swine). ��� ����� ����� ��������� ��������, �������� ���� ����� ���������� ��������������. ������� ������� �������� ������ ���� � ����, � �����, ����� ������ ������ � ���������, ������ ��������� ����. ��� ����� ����� ������� ����� �����-������, ������� ���������� ������������ �����. ���� Perl-��������� ������������� �������� ��� ����������� ������, stdin, stdout � stderr, ������� ������������� ���� ����������� ������� �����-������ � ����������� ���� ����������������. �� ��� ���������� ���������� stdin ��� ������ ������, ����������� �� ������������, ������������ ���� ���������. ������ ����� ������ ������� ��� ���������� ���� ����� ������ ����������. ��� �������� � ������� ���������� ����: sub init words ( open ('..'ORDSLIST, "wordslist"); while ($name = <WORDSLIST� ( chomp ($name); $word = <WORDSLIST>; chomp ($word); $words ($name} = Sword; close (WORDSLIST) ; �� �������� ��� � ������������, ����� �� ������������ �������� ���������. ��� �������� �����, ��� ����� �� ������ �������� ����� �������� ������ ���� � ���� ��� ������. ����������� ��������� ������ ������ ���� � ���� ������� � ������ � ������������ ���� � ��������� ����. ��� ����� ���� ������ �� ����� �� ����� ������: fred camel barney llama betty alpaca wilma alpaca ������� open �������������� ���������� ����� wordslist, �������� ��� � ������ wordslist, ����������� � ������� ��������. �������, ��� ����� ���� ������������ �� �������� �������� ��������� �������, ����� ��� ����, ��� ���������� ���� ����������. ����� ����, ����������� ������ ������ ������������ ���������� ������� (���� ��� � �� �����������); ������� ����� �� ���������� �������. ��� ���������� ����� while �������� ������ �� ����� wordslist (����� ���������� ����� wordslist) �� ����� ��� ������ ������� �����. ������ ������ ��������� � ���������� $name. �� ���������� ����� ����� �������� <wordslist> ���������� ������ ������*, ������� ��� ����� while �������� "����", � ��������� ����. ���� �� �� ��������� ��������� � ������ -w, ��� �������� �� ���������, ���������� �� ���������� ������������ ��������. ������ ������, ������� ���������� �������� <wordslist>, �� ������ �����: ��� ����� �������� undef. � ��� �������, ����� ��� �����, �������� ��������� �� �������� undef ������������ �������� defined. ��� ������ ����� �� ����� ��� �������� ����������� �� ��������� �������: while ( defined ($name = <WORDSLIST) ) ( * �� ����� ���� ��� ����� undef, �� ��� ��������� ������� ��������� ���������� ����������.
�� ���� �� �� ���� ��� ����� ���������, ��, ��������, ��������� �� ����� � ��, ���������� �� ������� open �������� "������". ���, ������, � ����� ������ �������� ����. ��� ������ �� ��������� � ���������� �� ������ � ������, ���� ���-�� �������� �� ���, ����� ������������ ���������� ������� die. �� ���������� ������ ���� ������� � ��������� ������ ����� ���������. � ������ �������, ��� ���������� �������� ������� �� ��������� ������ (������� ������ ����� ������) � ���������� $ name. ������� � ������� ������� chomp ��������� ������ ����� ������, ����� ����� ��������� ��������� ������, ����� �������� ��������� ����� � ��������� sro � ���������� $word. ������ ����� ������ ��� ���� ���� ���������. ��������� ������ ����� while �������� $word � %words � ������ $name, ����� ���������� $word ����� ������������ ��������� ����� ���������. �� ���������� ������ ����� ��� ���������� ����� ������������ ��������, �������������� ������ ���� � ������� ������� close. (����������� ������ ������������� ����������� � ����� ������ ��� ������ �� ���������, �� �� ��������� ���� �����������. ������ ���� �� �� ���� ��-���������� �����������, �� �� ���� ��������� ��, ���������� �� ^lose �������� "������" � ������, ���� ������ �����, � ������� ��� ����, ����� "���������", ���� ������� �������� ������� ����� ������������ ��� ��������� ��� �����-������ ����������. ����� ���� ������ ���������. ������ ����� ����� �� �������.) ��������� ���� ����������� ������������ ����� ���� ����� ������� ������������ ����������� ��� ����� ���. ������ ���� ����� �������� ����������� %words � ������ ���������, �� ����� ������ �������� ��� ������������ � ������ ���������� �������� ���������. ���� �� ��������� ���������� ����� ��������� ����� ��������� ���: fr! /usr/bin/perl init words () ; print "What is your name? "; $name = <STDIN>; ;homp $name; if ($name =~ /^randal\b/i) ( * ������� �� ������ ���� :-) print "Hello, Randal! How good of you to be here!\n"; I else ( print "Hello, $name! \n"; # ������� ����������� �print "What is the secret word? "; $guess = <STDIN>; ^homp ($guess) ; ^hile (! good word($name, $guess)) I print "Wrong, try again. What is the secret word? "; $guess = <STDIN>; :homp ($guess); } ## ����� � ������������ sub init_words ( open (WORDSLIST, "wordslist") 11 die "can' tl open woraJ.isT:: ^' .ihile ( defined ($name = <WORDSLIST�) ( chomp ($name) ; $word = <WORDSLIST>; chomp $word; $words( $name) = $word; I close (WORDSLIST) II die "couldn't close wordlist: $"' iub good word ( my($somename,$someguess) = @_; * ����������� ��������� $somename =~ s/\W.*//; # ������� ��� �������, ������� ����� ������� ����� # ������ ����� 'somename =~ tr/A-Z/a-z/; # ��������� ��� ������� � ������ ������� :.f ($somename eq "randal") ) <t �� ����� ��������� return 1; * ������������ �������� - true elsif (($words($somename) II "groucho") eq $someguess) ( return 1; * ������������ �������� - true else ( �eturn 0; i ������������ �������� � false ������ ���������� ���� ��� �������� ��������� ��� ��������� "��������" ���������. �������� ��������: ������ ����������� ������ � ����� ������������ init_word() . ������������ �������� � ����������� ����������� �� ������������, � ��� ������, ������ ��� �� �� ���������� ������ �������������� ��������. � ������ ������ ��� �������������� �������� "������" (� ���������, �������� 1), ������ ��� ���� �� close �� �����������, �� die ������ �� ��������� � stderr � ����� �� ���������. ������� die �������� ����������� � ����� 10, �� ��������� ����� ����� ��������� ������������ �������� �����, ��� ����� ����������� ��������, �� ������� �� ������� ������������ ��� ������� � ������ ������. ���������� $! (���� ��������������� � ����� 10) �������� ��������� ��������� �� ������, ����������, ������ ������ ��������� ����� ���������� ��������. ������� open ������������ ����� ��� �������� ������ ��� ������ � ��� ���������� � �������� �������� ��� ������ (����� ��� ���� ���������). ������ �������� ���� ������� ����� ���� ������� �����, � ����� 10. ��� ���������� �������� ������� ������������ '���� ������ ��������� ���� ������ �������� ������� ��� � ������!", � ������� ������� �������� ������� ��������� ����. �� �� �����, �������, ��������� ������������� ����������� ������ ������, �� ������ ���� �� ������������� �� � ���, ��� ������ ��������� ���� �� ��������� � ������� ���� ���� � �����. ����� ����� ������ ��� � ������������ init_words ( ) ; �� ��� �������� � ��� � ������ wordlist. Perl-�������� -� ���������� ��������, ������ ���������� ����, ���������� � ������� ��������� ����� ��� ����������� �����, ������� ��� ����� ������ ����������, ��������� �� ��� �������� ����� ���� ��� ����������� ����� wordslist: sub init words ( open (HORDSLIST, "wordslist") 11 die "can't open wordlist: $!"; if (-� WORDSLIST >= 7.0) ( f � ������������ � ���������������� ��������� die "Sorry, the wordslist is older than seven days. "; I while ($name = <WORDSLIST� ( chomp ($name) ; $word = <WORDSLIST> ; chomp ($word) ; $words($name} = $word; close (WORDSLIST) 11 die "couldn't close wordlist: $!"; �������� -� wordslist ������������ �� ��������� 7. ���� ��� ������, �� ��, �������, �������� �������. ����� �� ����� ����� ��������, �������� die, ������� ����� ����� ������� ��������� �� �����* � ��������� ���������. ��������� ����� ��������� ��������� �� ������������, ������� � ����� �������� ������ �� �� ��������� �� �����. ������ ����������� "��������" ����� �� ����� ������ ��� ��� ���������, ������, ����� ���������� ������� � ���� � ��� ��������� ��������, �������� �������� � ������ �����. ����� �������� �� ���� �������� � ����� 10. ��� ������������ ������������, ���� �� ������ � ���� ������� ���������, ��� ����� ��������� ������� �������� ��������� ����������� ����� ������ ���, ����� ������������ ��������� ���� ��������� ����� �������. ��� ����� �������������� ������ ������������ good word ( ) (����������� ������������ ����������� ����� Perl), ������ ��� ��� ����������� ���������� ��������� � ��� ���. �������� ��������� ����� ������� ��� � ��� ������, ���� �� �������� ���� ����� ����������� ����� ���, ��� � ��������� �������� YOUR_AD-DRESS_HERE. ���, ��� ��� ����� ��� ����� �������, ��� ��������������� ����� ���, ��� ���������� �� ������������ 0, ������� ���������� �����, ������� ���������� ����� �������� ��������� (mail): ��� good word ( my($sornename,$someguess) = @ ; # ����������� ��������� �^somename =~ s/\W.*//; t ������� ��� �������, ������� ����� � ������� ����� * ���� ������, �� � ���������� ����� stderr, �� ������ ��� � �������� ��������. $somename =~ tr/A-Z/a-z/; # ��������� ��� ������� � ������ ������� if ($somename eq "randal") ( # �� ����� ��������� return 1; # ������������ �������� � true elsif (($words($somename}ll"groucho") eq $someguess) { return 1; ff ������������ �������� � true else ( open MAIL, "Imail YOUR_ADDRESS_HERE"; print MAIL "bad news: $somename guessed $someguess\n"; close MAIL; return 0; 4f ������������ �������� � false ������ ����� �������� ����� � open, � ������ ������� ��������� �������� ����� �������� ������ (1). �� ���������, ��� �� ��������� �������, � �� ����. ��������� �������� ������ ��������� ����� ������ �������, �� ��������� ������� ���, ����� ����� ���� ����������� � ���� ������. (���� ��������� �������� ������ � �����, � �� � ������, �� ����� ����� ������ �������� ���������� �������.) ��������� ��������, print, �������� ��� ������ �� stdout*, � ���������� �����, ������� ����� �������� ������ print � ����������� ������ �� ����� ����������. ��� ������, ��� ��������� � �������� ����� ������ ������� ����������� ��� ������� mail. �������, �� ��������� ���������� �����, � ���������� ���� ����������� ��������� mail � �������� ���� ������. ����� �������� ��� ������������, �� ����� �� �������� �� ������ ���������, �� � ���������� �����, �� ����� ���, ��� ����������� ��� ����� ����� (��� �������� � ������� ����������� �����), ����� �� ������ ���������, ������� �� ������� ����� �������� ����������. Perl ����� ����� ��������� ����������� ������, �������� ������� � ������������ �����������, ���� ��������� ����� ������� ��������� � ��������� ��� (��� �����) ����� ��������� ������������. �������� ������� (��� � shell) ���� ����������� �������� ���������� ������ ������� ��� ������. ��� ��� ����������� � ����� 14, ��� ��� ������� ������. ��������� ������ ��������� ���� � ������� �������� ������� ������ ������� ������ ����������� ����� ����� ��������� ����. ������ ����� � ������ wordslist ����� ������ � ������� �������� �����, ��������������� �� .secret. �������� shell ������ ������� �������� ����� ����. echo *. secret " ������ ����������� ������ � ��������� � ������� ������ ���������� �����. ud ����, ������, �� ��������� �����. ��� �� ����� �������, Perl ��������� ������� ��������� ���� � �������������� ������������. ��� ��� �������� � ����������� ������������ init_words () : ;ub init words ( while ( defined($filename = glob("*.secret")) ) ( open (WORDSLIST, $filename) 11 die "can't open wordlist: $!"; f (-M WORDSLIST >= 7.0) ( while ($name = <WORDSLIST� ( chomp $name; Sword = <WORDSLIST>; chomp $word; Swords ($name ) = $word; ) ) close (WORDSLIST) II die "couldn't close wordlist: $!"; ������� �� ��������� � ����� ���� while �������� ����� ������������ �� ���������� ������. ����� ������� ����� � ������� glob. �� ������������ �������� ��� ���������� filename glob. ��� ������� �������� ����� ��� ��, ��� <stdin>: ��� ������ ��������� � ��� ��� ���������� ��������� �������� �� ������ ���� ������, ������� ������������� ������� shell, � ������ ������ * . secret. ���� ����� ���� ������ ���, ������������ ������ ������*. ����� �������, ���� ������� ������� �������� ����� fred. secret � sarney.secret, �� ��� ������ ���������� ����� while ��������� ���������� $fiiename �����������.secret (���������������������).��� ������ ���������� ����� ��������� $filename ����� fred .secret. ��������� ��� ������� ������ ������� glob ��� ���������� ������ ������, �� ������ ������ �� ��������, ��� ��� ���� while �������������� ��� �������� ��� "����", ��� �������� � ������ �� ������������. � ���� ���������� ����� while �� ��������� ���� � ���������, ���������� �� ����� �� ���������� (� ������� ���������� ��������� ������ ������ �� ����� ���� ����). � ����� ���������� ������ ������� �� �������� ��� ��, ��� � ������. �������, ��� � ���������� ������, ����� ������� ��������� �� � �������� *. secret � "�������" ������� �� �������� �� ���� ����, ������������ ����������, �� �������� �� ������ ���������� ����� � ������ %words. ��� ������, ��� ���� �������� ������������ ������ groucho. ���������. (� �������� ���� ����� ������� �� ������������ ��������� �� ������ �������� �������� ���������� ��������� � ������� %words � � ��� �������������������� ���������� ��������� ������� die. �������� �������� �� ������� keys, ����� �� ������ �� ����������� ����� � ����� 5.) * ��-��, ����� undef. /. ��� �������� ������ ��������� ���� ����, ������� �������� ������� ��������� ���� ������ �������� ����� ��� ���� ��������� ������, ������������ � ������� ������, � ��������� �� "��������". ���� �� �� ������� ����������� � ���������� �������� ���������� �����, � ��� ����� ����� �������� ��� ��������� ��������� ������������ ������������ ��� ������. ������ ������� ������� ��� ���� ��������� �����, ���������������� ��� ����� ������ ���� �� ������������ init_words ( ) : while ( defined($filename = glob("*.secret")) ) ( open (WORDSLIST, $filename) II die "can't open wordlist: $!"; if (-M WORDSLIST >= 7.0) < while ($name - <WORDSLIST� ( chomp ( $name ) ; $word = <WORDSLIST> ; chomp (Sword) ; *** ������ ���������� ����� ��� } ) I close (WORDSLIST) 11 die "couldn't close wordlist: $!" � ������� ���������� ���� ����� ���������, ��� ��� ����������� "������ �������� ����� ���", �� ����� ��� ����: ��� ����� (���������� � ���������� $filename), ���-�� ��� (� ���������� $name) � ��������� ����� ����� �������� (���������� � $word). ����� � ����� ������������ ��������� � Perl ����������� ������������ �������. ��� ����� ���-�� � ��������� �� ������ ���������� ������������ ������ (������ ��� �������� � �����, ��� � ��� �����������): format STDOUT = @�����<�� @<���� @�����< $filename, $name, $word ����������� ������� ���������� ������� format stdout=, � ����������� ������. ��� ������ ����� ������ ������� � ������ � ��� ��� ������. ������ ������ ������� � ��� ������ ����������� �����, � ������� �������� �����, ����� � ��� �����. � ���� ������� � ��� ��� ����. ������, ��������� �� ������� ����������� ����� � ��� ������ ������ �������� �����. ������ �������� �������� ������ ���������, ������� ����� ����������� ��� ������������� �������; ���������� ���������� ���� ��������� ����������� � ����, ������������ � ���������� ������. ���������� ������������ ����� ������� ������ �������� write, ��������: *! /usr/bin/perl while ( defined($filename = glob("*.secret")) ) ( open (WORDSLIST, $filename) 11 die "can't open wordlist: $"';
ir (-M WORDSLIST >= i.u) { while ($name = <WORDSLIST>) { chomp ($name); $word = <WORDSLIST> ; chomp ($word) ; write; # ������� format STDOUT � STDOUT close (WORDSLIST) II die "couldn't close wordlist: $!"; } format STDOUT � @<<<<<<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<<<< $filename, $name, $word ����� ���������� ������. Perl ��������� ���������, ��������� � ������ ��������, � ���������� ������, ������� �������� � ���������� ����� stdout. ��������� write ���������� ���� ��� ��� ������ ������� �����, �� ������� ��� ����� (��� ����� ������ ��� ������� ���������� �����); �������� ����� ����� ������ �� �������. ��-�. �� ������ ���� �������� ��������. �������, ��� ���������� ����� �������. ����� ������ ������ ������ ������ ��������: format STDOUT_TOP = Page @� $% Filename Name Word ���� ������ ���������� stdout_top; �� ����� �������������� ��� ������ ������ ������� stdout, � ����� ����� ������ 60 �����, ���������� � stdout. ��������� �������� ��������������� ����� �� �������� ������� ^tdout, ������� ��� �������� ���������. � ������ ������ ��������� ����� ������������ ����� (Page) � ����������� ������������ ����. ��������� ������ � ������ �������� �����, � ������ ������ ��� �������� ���������. ��� ��������� �������� ���������� $%*, � ������� ���������� ����� ���������� �������. ������ ������ ������� �����. � ��������� ��� �� �������� ������� �����, �� ��������� �� ��� ������ ���� ������; ��� ���������� ����� �� �����, ���������� ���� ����� ������� ���� ������� �������� � ����������� �������� ���������� ������ ������. ��������� ��� ������ ������� ����� �� �������� ������� �����, ������� ��� ���������� �� ����� � ��� ����, � ����� ��������. ����� �������, ���� ������ ������������ �������� ������� �����, ���� �� ������� �������� �� �������� � ��������. *��������� ������ English ����� ������������ ��� ���� ���������������� ��������� ���������� ����� ������������� ���������� ����� �������������� ��������. ����� ��� ����������� ����������, ���������� ������������ ��� � �����-����� ���������. Perl ������ ������ ������ �������� �������������. � Perl ������� ����� ����, ������� ������������� � ������������� �� ������� ���� ������� ������. ���� ���� ����� ���������������� ������������� ������������ � �� ������� � �� ������ �����. �������� �� ���� �� ���������, ����� ������ �� �������� , � ����� 11. ��� ������� ������ ������ ���� ����� ��������� ������������ ����� *.secret � ������� ��������, ��, ��������, ��������� ������� ������ �����. �� ��� ��� �� ������ ���������� ��. ������� ������� ��������� ��� � ����������� ��� ����� � *.secret.old, ����� � ������� ����������� ����� ���� ����� - �� ����� - ����� ����� ���������� ��������. ��� ��� �������� ������������ init_words () , ���������������� ��� ���������� ����� ��������: sub init_words { while ($filename = <*.secret>) { open (WORDSLIST, $filename) || die "can't open $filename: $!"; if (-M WORDSLIST < 7) { while ($name = <WORDSLIST>) { chomp ($name) ; $word = <WORDSLIST> ; chomp ($word); $words {$name} = $word ; } } else { # rename the file so it gets noticed rename ($filename, "$filename.old") || die "can't rename $filename.old: $!"; } } �������� �������� �� ����� ����� ��������� else � ����� �������� "��������" ������. ���� ���� �� ��������� ���� ���� � �����, ������� rename ��������������� ���. ��� ������� ��������� ��� ��������� � ��������������� ���� �������� ������ ����������, ���������� ��� ���, ��������� �� ������ ���������. � Perl ����� ������ ����� ��������, ����������� ��� ��������������� �������; ���, ��� ����� ������� � ������ � �-���������, ����� ������� � ��� � Perl.
52 ������� Perl ������� ���� ������ � ������� ����������� ����� ��������� ���� ������� ��������� �� ���, ����� ������ ������������ ��������� ��� ��������� ������ ���� ������� ���������� �����. ��������, ������������ ��������� ������, ������� ������� ��� ����� � ���. ��������, �������� $last_good{$name} == time; ����������� �������� �������� �������, ���������� �� ���������� ������� (��������� ������� �����, ����� 800 ���������, �����, ������� ������������� �� ������� ������ �������) �������� ���� %last_good, �������� ��������� ��� � �������� �����. � ����������� ��� ���� ���� ������ � ������� ���������� ����������� ����� ���������� ����� ������ �� �������������, ������� ������� ��� ���������. ��� ����, ������, � ����������� ����� �������� ��������� ��� �� ����������. ������ ���, ����� ��������� ����������, ����������� ����� ���, �.�. �� �������� ����� �� ������ ��� ������� �������������� ���, � �� ���������� ���������� ��������� ���������� ��� ���� ��������. ������� dbmopen* ���������� ��� � ���� (���������� � ���� ������), ��������� ��� DBM-����. ��� ������������ ��������� �������: dbmopen (%last_good,"lastdb",0666) || die "can't dbmopen lastdb: $!", $last_good{$name} = time; dbmclose (%last_good) 11 die "can't dbrnclose lastdb: $!"; ������ �������� ��������� �����������, ��������� ����� ������ lastdb. dir � lastdb.pag (��� ������������ ����� ��� ������ lastdb, ���������� DBM-����). ���� ��� ����� ���������� ������� (� ��� ������ ��� ������ ������� �� �������������), �� ��� ��� ��������������� ����� ������� 0666**. ����� ����� ������� ��������, ��� ��� ������������ ����� ������ � ������������ ������ � ��� �����. ���� �� ��������� � UNIX-�������, �� �������� ����� ���� ������� � ����� �� ������� �� man-�������� chmoc/(2). � ������ �������� chmod() ����� �������� ��� ��, � ����� � �� ��������. ��������, � MS-DOS ��� ������ �� ��������������� ����� �������, ����� ��� � Windows NT � ���������������. ���� ����������� ���, �������� �������� ������ ����� �������. ������ �������� ����������, ��� �� ���������� ���� ��������������� ��� ��� ��������������. ��� ����, ������, ��� �������� ��� ������������� ������-���� �������� ���� ������������� ����������� �����, ���������� DBM-����. ��� ����������� ��������� � ���� �������� ��� ��������� ��������� ��������������� �� ��� ����������� �� �����. ��������� ����� * ����� ����� ������������ �������������� ������� tie � ���������� ����� ������; ���� ������� �������� ������ � ������ 5 � 7 ����� Programming Perl � �� man-��������� perHfc(\) � AnyDMB_File (3). ** ���������� ����� ������� � ���� ������ ������������ � ���������� ���������� ���������� �������� � ��� ������ 0666 � ������� ��������� ���������� umask ������ ��������. 7. �������� 53
��������� ��� ��������� ���������� � � ������� ����� �������� ������ ���������. ������ �������� ����������� ��� �� DBM-�����, ����� ��� ����������� ��� ��, ��� �������� close ��������� ����. ���� ��� ����� ��������� ���� ��������� ��������� ����� ���� ������ (� ���� ������������ �� ��������), � ��� ���� ��� �������� ������� �������� ��������� � ���� ���� ������ ����������. ��� ����� �������� ������� ��������� ����������, ������� �������� �������� ���: #!/usr/bin/peri dbmopen (%last_good, "lastdb", 0666) I I die "can't dbmopen lastdb: $!"; foreach $name (sort keys (%last_good) ) { $when = $last_good($name); $hours = (timed - $when) / 3600; # ��������� �������� ����� � ����� write; } format STDOUT = User @<<<<<<: last correct guess was @�< hours ago. $name, $hours ����� �� ������������ ��������� ����� ��������: ��������� ���� foreach, ��������� ������ � �������� �������� ������ �������. ������� ������� keys ��������� ��� ���� � �������� ��������� � ���������� ������ �������� ���� ������ ����� ���� � ������������ �������. ��� ���� %words, ���������� �����, ��������� ����� �������� �����: fred, barney, betty, wiima, ������ ����� ����� ���� ����������� � ������������ �������. � ������ ���� %last_good ����������� ����� ������ ���� �������������, ������� ��������� ����� ���� ��������� �����. ������� sort ��������� ���� ������ � ���������� ������� (��� ���� �� �� ���������� ��������� ���� ����� ������ sort). ��� �����������, ��� ������, �������������� ��������� ����� ���������� foreach, ������ ����� ������������ �� ��������. ��� ���������� Perl-�������� foreach ����� ����� �� �������� foreach shell �. �� �������� ������ �������� � ����������� ������ �� ��� �� ������� �����-���� ��������� ���������� (� ����� ������ � ���������� $name), �������� ��� ������� �������� �� ������ ���� ������ ����� (����). ���, ��� ���� ���� � ������ %last_good �� ������ ���� ��������, ��� ���� ������ ��� ���������� $name ����� ������ ��������. ��� ���������� ����� foreach ���������� �������� ���� ����������, ������������ ����� � ������� stdout, � ���������� ���� ������. �������� ��������: �� ��������� "�������" �����, ������� ���������� (� �������) ��������� ����� �� �������� ������� (������� ���������� ������� time), � ����� ���� ��������� �� 3600 (����� ������������� ������� � ����).
54 ������� Perl � Perl ������������ ����� ������� ������� �������� � ������� ��������� ��� ������ (��������, ����� �������) � ��� ������ � ������������� ������ ������� (�����, ��� ���� ������ "��������� �����������", ������� ����� ��������� login). ��� ������� ������� � ����� 17. ������������� �������� �������� ����� ������ �������� ������������ ���������, ������� �� ������� � ���� �����, � ������������� ����. ������� � ���������-�����������: ^!/usr/bin/peri &init_words ( ) ; print "what is your name? " ; $name = <STDIN>; chomp ($name) ; if ($name =~ /^randalVb/i) { # ������� �� ������ ���� :-) print "Hello, Randal! How good of you to be here!\n"; } else { print "Hello, $name! \n"; # ������� ����������� print "What is the secret word? "; $guess = <STDIN> ; chomp $ guess ; while (! good_word( $name, $guess)) { print "Wrong, try again. What is the secret word? "; $guess = <STDIN> ; chomp $guess; } } dbmopen (%last_good, "lastdb", 0666); $last_good{$name} = time; dbmclose (%last_good); sub init_words { while ($filename = <*.secret>) { open (WORDSLIST, $filename) || die "can't open $filename: $!"; if (-M WORDSLIST < 7) { while ($name = <WORDSLIST>) { chomp ($name) ; $word = <WORDSLIST> ; chomp ($word); $words {$name} = $word ; } } else { # rename the file so it gets noticed rename ($filename, "$filename.old") || die "can't rename $filename.old: $!"; } close WORDSLIST; } } sub good_word { my($somename,$someguess) = @_; # ����������� ��������� $somename =~ s/\W.*//; # ������� ��� �������, ������� ����� ������� ����� $somename =~ tr/A-Z/a-z/; # ��������� ��� ������� � ������ ������� if ($somename eq "randal") { # �� ����� ���������
1. �������� 55
return 1; # ������������ �������� � true ) elsif (($wordsf$somename( II "groucho") eq $someguess) ( return 1; # ������������ �������� � true } else { open MAIL, "| mail YOUR_ADDRESS_HERE"; print MAIL "bad news: $somename guessed $someguess\n"; close MAIL; return 0; # ������������ �������� � false � ������ � ������ ��������� ����: #!/usr/bin/peri while ($filename = <*.secret>) ( open (WORDSLIST, $filename) I I die "can't open $filename: $!"; if (-M WORDSLIST < 7) { while ($name = <WORDSLIST>) ( chomp ($name) ; $word = <WORDSLIST> ; chomp ($word) ; write; # ������� format STDOUT � STDOUT } } close (WORDSLIST) ; format STDOUT = @<������� @��<�� @����<� $ filename, $name, $word format STDOUT_TOP = Page @� $% Filename Name Word �, �������, ��������� ������ ������� ���������� ����������� ����� ������: #!/usr/bin/peri dbmopen (%last good, "lastdb", 0666); foreach $name (sort keys %last_good) ( $when = $last_good ($name); $hours = (time - $when) / 3600; # ��������� �������� ����� � ����� write; } format STDOUT = User @<�����: last correct guess was @�< hours ago. $name, $hours . 56 ������� Perl
�������� � ���� ���������� ������ ��������� ���� (����� � ������� ���-��.secret, ����������� � ������� ��������) � ���� ������ lastdb.dir � lastdb .���, � � ��� ����� ���, ��� �����. ���������� ����������� ���� ����������� ������������, ������ � ������� ������ � ���������� �. ��� ���� ����� ������ ��� ���� ���� ����. 1. �������� ���������-������� � ��������� �� ��������. (��� ����������� ������� ������ ��������� ����.) ���� ����������� ������ � ���������� � �������� Perl-����. |