����� 12� ���� �����:
������������� ����������� ��������� �������� � �������� ����������� �������� ������ ����������� �������� ���������� ������ � �������������������� �� ������ ��������� �� ���, ��������, ������� � �������� "������� �������" � � ���, ��� ������������ ������� cd shell. � ��������� ���������������� ��� ��������� �������� �������� �������� �� ����������� �� ��������� ����� chdir. � Perl ���� ������������ ��� ���. ������� chdir � Perl ��������� ���� �������� � ���������; ��� ��� ���������� ������������ ��� ��������, ������� ���������� �������. ��� � � ����������� ������ ��������� �������, ��� �������� ��������� �������� �������� �� ������������� chdir ���������� �������� "������", � ��� ��������� ������ � "����". ��� ������: chdir("/etc") || die "cannot cd to /etc ($!)"; ������� ������ �� �����������, ������� ����� �������� � ����� �������: print "where do you want to go? "; chomp($where = <STDIN>) ; if (chdir $where) f # ���������� ) else { # �� ���������� > �� �� ������� ����������, ��� �� ����������, �� �������� ������� pwcf. � ������� ������ �� ��������� � ����� 14. * ��� �� ����������� ������� getcwd () �� ������ Cwd. ��� ������� ��������* ����������� ���� ������� �������. ����� ����������� ����� �������, �� ��������� ������� ������� ������ ������������� ��������, �� �� ���� ��� ����� �������������. ���� Perl-��������� ������ ���� �������, ��� �� ������ �� ������������ shell (��� ���� ������), ������� �������� ���� Perl-�������. ����� ��� �� ��������, ����������� Perl-����������, �� ������ �� ������� ������� ����� ���� ���������. ������� �������� ��� ����� ��������� ����������� �� �������� �������� Perl-���������. �� ��������� ������� chdir ��� ��������� ������ ������� ��������� �������, ����� ��� ��, ��� ������� cd shell. ������������� ���� � �������� ��������� � ��������� ������ ����� ��������� (*), �� shell (��� ��� ������������� ��������� ������, ������� �� �����������) �������������� �� ��� ������ ���� ���� ������, ����������� � ������� ��������. ���, ���� �� ���� ������� rm *, �� ��� ������ ��� ����� �� �������� ��������. (�� ������� �����, ���� �� ������ ����� ����� ���������� � ���������� �������������� � ��������� � �������������� ������.) ��� �������: �������� [�-�] *. � ������������ � ������ ���� ������, ����������� � ������� ��������, ������� ���������� � ����� �� ���� ������ �������� �������� � ������������� �� .�, � �������� /etc/host* � � ������ ���� ������ �������� /etc, ������� ���������� �� ����� host. (���� ��� ��� ��� ����, �� ����� ���������� ������� ����������� �������� �������������� ���������� � ��������� shell.) �������������� ���������� ����� * ��� /etc/host* � ������ ��������������� ���� ������ ���������� �������������� (globbing). Perl ������������ ������������� � ������� ����� �������� ���������: ���������� ������������� ������� ����������� � ������� ������ ��� �� ������������ ������� glob. @� = </etc/host*> @� = glob("/de/host*"); � ��������� ���������, ��� �������� ����, ����������� ������������� �������� ������ ���� ����, ������� ��������� � �������� (��� ���� �� shell ������� ��������� ���������), ��� ������ ������, ���� ���������� �� ����������. � ��������� ��������� ������������ ��������� ����������� ���, � ���� ���������� ��� � ������������ undef; ��� ����� ������ �� ������ �� ����������� �����. ��������, �������� ���� �� ������ ����������� � ������� ���������� ����: while (defined($nextname = </etc/host*>)) ( print "one of the files is $nextname\n"; > * ��� ����������� ��� UNIX � ����������� ������ ����������� ������������ ������. ����� ������������ ����� ������ ���������� � ��������, ���������������� ���� ������� � ��� (/etc/host), �������, ���� ��� ����� ������ ��������� ����� �����, �������� �������� �� ��������������, ��������: while ($nextname = </etc/host*>) ( $nextname =~ s#.*/##; # ������� ����� �� ��������� ����� ����� print "one of the files is $nextname\n"; } ������ ��������� ����������� ��������� ��������� ��������; ��� ������ �������������� ��������, � ����� ��������������� ���, ��� ����� ��� ���� ������� ������: @fred_barney_files = <fred* barney*>; ������� �������, ��� �������� ������������� ������ �� �� ��������, ������� ���������� �� ������������� �� ������� echo � ������ �� �����������.* ���� ������������� ������ ������ � ������������� � ���������� ���������� �������������� ����� ���������, ����������� ������� ����� ���������� ������ �����. �� ������� ��� ���������, ����� ������ ����������, ������ ����� <\. �$> �� ������� ��� �����, ����� ������� ������������� �� .�! � ��������� ������� glob ����� �������������� ������������ ������������ ����������. � ������� Perl-���������� ����� �������� �����, ���������� �������, ����������� �� ����� ����������: if (-d. "/usr/etc") ( $where = "/usr/etc"; } else ( $where = "/etc"; ) Sfiles = <$where/*>; ����� ���������� $ where ������������� ���� �� ���� ���� ��������� � ����������� �� ����, ���������� ������� /usr/etc ��� ���. ����� �� �������� ������ ������, ����������� � ��������� ��������. �������� ��������: ���������� $where �������� ��������������, �.�. ����������� ������������� ��������� �������� /etc/* ��� /usr/etc/*. ���� ���� ���������� �� ����� �������: ������� <$var> (������� �������� ������������� � �������� ��������������� ��������� ���������� $var) ������ ������������ ��� <${var}>. � ������� ��������� ����� ���������� ������ ����� �� ���������.** * ��� ��� �� ����� ��������� ������ � ���, ��� ��� ���������� ������������� Perl ������ ��������� C-shell, ������� ���������� ��������� ������ ����������, � ���������� �������������� ������ ����, ��� �������� �������. ** ����������� <$fred> ������ ������ �� ����������� �����, ��������� ���������� ��������� ���������� $ fred. ������ � ���������� ������� �������������, �� ����������� � ����� �����, ��� ����������� ��������� ������������ ��������� ����������� ������, ��� ��� ����������� ���������� � �������������� ���, ��� ����� ��� ������. ����������� ��������� ���� � ����� ���������� ������������� ������������ ������� ������� ������������ ������� readdir ��� �� �������������� ����������, Perl ������������ ������ � ���� ��������� (� �� ���������) � ������� ������������ ���������. ���������� �������� � ��� ��� �� ���������� ������������ ����, � ��������������� � ������������, ������� �������� ������������ ������, �������� ����� � ������������ ��������� (������ ������������ ����������������� �����, ������������� ������������ ������� �������). ���������� ����� fred � ���������� �������� fred �� ������� ����� �����. ���������� �������� ������������ ����� ���������� � ���������� ���������. ������ ������ ������ (��� �� ����������� �����) �� ����������� ���������� �������� ��� ������ ������ ���� ������ � ���� ��������. ����������� ��������� ������ ����������� ������ ��� ������; ������ ������������ ���������� �������� ��� ��������� ����� ����� ��� �������� �����. ���� ������� readdir() � �� �������� � ���������� ��� (� ��� ����������� ����� Perl ������� ������ �� �� �������������), �� ������������� ����� �� ���� �������� �������� � ��������� ������ � ���� ��������� ���������� �� �������: ��� �������� ���������� �� ���������� ������ ������ ����. Perl ������ ��������� ����������� ��� �� ������� ������� �����, �� ����� ������ ��� �� ����������. �������� � �������� ����������� �������� ������� opendir �������� ���������� ������������� ������ � ��� �� ������ � � � C++. �� ������ ��� ������ ����������� �������� � ��������� ��������, �������� ��� ������������ ��������. ���� ������� ����� ���� ������, opendir ���������� �������� "������"; � ��������� ������ ��� ���������� "����". ��� ������: opendir(ETC,"/etc") || die "Cannot opendir /etc: $!"; ����� ����� ������ ������� ������� ���� ����������� � ������������ �������� etc, �� �������, ��������, ����� �����������, ��� ��������� ���������� ��������. ��� �������� � ������� ������� closedir, ������� ������ ������ �� close: closedir(ETC); ��� � close, closedir ����� ����������� ��������, ������ ��� ��� ����������� ��������� ������������� ����������� ����� ��������� ��������� ���� � ����� ���������. ������ ����������� �������� ������ ���������� ��������, �� ����� ��������� ������ ���� � ������� ������� readdir, ������� ��������� ������������ �������� � ���������� ��������. ������ ����� readdir � ��������� ��������� ���������� ��������� ��� ����� (������ �������� ���: � ������������ �������� ������� ����� ���) � �������, ������� �� ������ ������ ������� ���������*. ���� ������ ���� ���, readdir ���������� undef**. ����� readdir � ��������� ��������� ���������� ��� ���������� ����� ������ � ���� ������ � ����� ������, ������������ �� ������ �������. ��� ������, � ������� �������� �������� ���� ���� ������, ������������ � �������� /etc: opendir(ETC,"/etc") II die "no etc?: $!"; while ($name = readdir(ETC)) f t ��������� ��������, �� ������ �� ���� print "$name\n"; #������� ., .., passwd, group � �.�. 1 closedir(ETC) ; � ��� ��� ����� �������� ��� ����� � ���������� ������� � ������� ������� sort: opendir(ETC,"/etc") || die "no etc?: $!"; foreach $name (sort readdir(ETC)) ( # ��������� �������� � ����������� print "$name\n"; #������� ., .., passwd, group � �.�. ) closedir(ETC) ; � ���� ������ �������� ����� ������, ������� ���������� � �����. ��� �� ������ �� ��������� �������������, ������������ � �������������� <*>, ��� ������� �����, ������������ � �����, �� ������������. � ������ �������, ��� ������ �� ��������� ������ ������� echo* shell. * ������ ������ � ��� �������, � ������� ����� ������ ����������� � ��������, �.�. ��� �� "������������� �������", � ������� �� ��������� ����� � �� UNIX � ���������� ������ ������� .find ��� Is -f. ** ��� ��������, ��� ��� ������ � ������-w ��� �������� ������������ ���� while (defined ($name = readdir (...)). ���������� 1. �������� ���������, ������� ��������� �������, �������� �� ����������, � ����� ������ ������ ���� ������ ����� �������� � ���������� �������. (� ������, ���� ������� � ������� �� ����� ���� ��������, ��������� ������ ������������ �� ���� ������������.) " 2. ������������� ��������� ���, ����� ��� �������� ����� ���� ������, � �� ������ ��, ����� ������� �� ���������� � �����. ���������� ������� ��� ��� � ������� �������� �������������, ��� � ����������� ������������� ����������� ��������. |