| ���������� | ����������� | �������� | ������
| ����� 1 | ����� 2 | ����� 3 | ����� 4 | ����� 5 | ����� 6 | ����� 7 | ����� 8 | ����� 9 | ����� 10
| ����� 11 | ����� 12 | ����� 13 | ����� 14 | ����� 15 | ����� 16 | ����� 17 | ����� 18 | ����� 19
| ���������� � | ���������� � | ���������� � | ���������� � |

����� 10

    � ���� �����:


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

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

���������� ����� � Perl-��������� � ��� ��� ���������� ��� �����-������ ����� ����� Perl-��������� � ������� �����. �� ��� ������ ����������� ������ � ������������ ���, ���� ���� �� ����: stdin � ��� ����������, ������� ��������� ���������� ����� Perl-��������� � ����������� ������ UNIX. ����������� ������� � Perl ���������� stdout (��� ������������ ������) � stderr (��� ������������ ������ ������). ��� �� �� ����� �����, ��� � ������������ ����������� ������������ �����-������ � � � C++, ������� Perl ����������� � ����������� �������� �����-������.

����� ������������ ������ ������ �� ����� ���������� ������, �� ��� ������� �� ������� ������������ ���� (������� � ��� ����� ���� ������ $fred, ������ Sfred, ��� %fred, ������������ sfred, ����� fred, � ������ � ���������� ����� fred). ��� � ����� ������, ����������� ������ ������������ ��� ������������ ����������� �������, ������� �� ����� ������� � ������������� ��� ���������� � ������� ������������������ ������� (��� ������, ����������� � ��.). ����������� ���������� ����������� ������ ������ �� ��������� ����. ��-������, ��� ����� ������ ���������� � ������ ���������, �, ��-������, ��������� ����� ��������� �� ���� ���� ��� �������� ������ ������������������ �����.

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

� Perl ���� ��� ����������� ������, stdin, stdout � stderr, ������� ������������� ����������� ��� ������ ��� ���������, ������������� ������������ ��������� ��������� (��������, shell). ��� �������� �������������� ������������ ������������ ������� open. ��� ����� ��������� ���������:

open(����������,"���") ;

��� ���������� � ����� ���������� �����, � ��� � ��� ����� (��� ����������), ������� ����� ������� � ����� ������������. ���� ����� ��������� ���� ��� ������. ����� ������� ���� ��� ������, ����������� �� �� ������� open, �� ��������� ����� ������ ����� ���� "������" (��� � shell):

open(OUT, ">��������_����");

�� ������, ��� ������������ ���� ����������, � ������� "������������� ������������ ������". ��� � � shell, ���� ����� ������� ��� ����������, �������� ����� ������ ��� ����� "������ ���", �.�.

open (LOGFILE, "����_����_�����������") ;

��� ����� ������� open � ������ ��������� ���������� ���������� �������� "������", � � ������ ������� � "����". (��������, ��� ������� �������� ����� ��� ������ �������� �������� "����", ���� ����� ��� ��� ������ � ���� ��������; ��� �������� ����� ��� ������ ������������ �������� "����", ���� ���� ������� �� ������ ��� ���� ���������� ������ � ������� ���� ������ � ����� ��������.)

�������� �������� � ������������ �����, �� ������ ������� ���, ���������������� ��������� close:

close(LOGFILE) ;

������� ���������� �������� ����������� ����� �������� � ��������������� �������� ����� ��������� �����. ��� �� ���������� � ��� ������ �� ���������. �� ���� ������� ������ Perl-��������� �� ���������� ���� ����������� �������� close. ���� �� �� ������, ����� ��� ���� ������� ��������� ��� ����� ��� ������ ������������ �� ���� ��������� �� ���������� ���������, ��� �������� ����������. ����� close ����� ����������� ��������, ���� ���� ����������, ���� ��������� ������, �� ������� ��������� ����, ���� ���������� ���� ���� �������� �����-������ ���� ��������. ����������� ������ ��������� ������������ �������� ���� ��������� �������.

��������� �����������: ������� die

�������� ���� ������ ������� �������, ��������� ������� ��������. ���������� �����, ������� �� ������� ������� �������, ��� ����� ����� �������������� � ���������, ������ ��� �����-���� ��������������*. ���� �� ������� ������ �� ����������� �����, �� ����� �� �������� ������� ����� �����. ���� �� ����������� ������ � ����������, ��� ������ �������� ������������� (��� �������� ���������� ������������ ������������� ��������).

������ �����, �� �������� ��������� ��������� ���������� ������� open � �������� ��������� �� ������, ���� ���� ��������� �� �������� ���� ��������. �����������, �� ������ ���������� ���� ��������� ������� �������� �����

unless (open (DATAPLACE,">/tmp/dataplace") ) (

print "Sorry, I couldn't create /tmp/dataplace\n";

} else {

# ��������� ����� ��������� }

�� ��� ����� �������� ������, � ����������� ��� ���������� �����, ������� � Perl ��� ��� ������������� ����������� ����������. ������� die �������� ������, ������� ����� ���� �������� � ������� ������, ������� ���� ������ (��� ��� ������ print) �� ����������� ���������� ������ ������, � ����� ��������� Perl-������� (���, ������� ��������� Perl-���������) � ��������� ����� ������ (�������, ��� �������, ��������, ��� ��������� ����� ���������**). ��������� ��� �������, ����������� ���� ��� ����� ���������� ���:

unless (open DATAPLACE,">/tmp/dataplace") f

die "Sorry, I couldn't create /tmp/dataplace\n";

}

* ��������� ����� ���������

����� ����� ��� ������. ��������, ��� ��� ���������� ������ ����� ������������ �������� | (���������� ���):

open(DATAPLACE,">/tmp/dataplace") I I

die "Sorry, I couldn't create /tmp/dataplace\n";

����� �������, die ����������� ������ � ��� ������, ���� ��������, ���������� � ���������� ���������� ������� open, � "����". ������ ��� ����� ���: "������� ���� ���� ��� �������!" ��� ������� ������ ���������, ����� ���������� �������� ������������ � � ���� ���.

* ���� �� �� ���������� ��������� � ������ -w.

** ���������� die ������ ���������� ����������, �� ��������� �� �� ���������� ���, ��� ������������ ����������, ��� ����� ���� ���, ��� ����� ��������. ����������� ��. � ����� 3 ����� Programming Perl (������� eval) ��� �� man-�������� perlfunc{\).

 

� ���������, ����������� � ������ "������" (��� �������� �� ������ ��������� ������� die) ������������� �������������� ��� Perl-��������� � ����� ������, ������� �� ������ ����� ����������, ����� ������ ������� die ����� ��������������� �� ��������������� ����� �� ���������. ���� �� �� �� ������ ��������� ����� ������ ��� ��� �����, ��������� � ����� "�������������" ������ ������ ����� ������. ��������:

die "you gravy-sucking pigs";

������� ���� � ����� ������, �

die "you gravy-sucking pigs\n";

�� �������.

��� ���� ������� ����� ������ die-����� � ���������� $!, ������� �������� ������ � ��������� ����� ��������� ������ ������������ �������. ������������ ��� ���:

open (LOG, "�logfile") || die "cannot append: $!";

��������, � ���������� ����� ���� ������ ��������� " cannot append:

Permission denied".

������� ����� ������� "������ ��� ��������", ������� ����������� ������������� ����� ��� warn. ��� ������ ���, ��� ������ die, ������ "�� �������". ����������� �� ��� ������ ��������� �� ������� �� ����������� �����:

open(LOG,"�log") 11 warn "discarding logfile output\n";

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

����� ���� ��� ���������� ����� ������ ��� ������, �� ���� ����� ������ ������ ����� ��� ��, ��� �� ������������ ����� stdin. ��������, ��� ������ ����� �� ����� ������� ������������ ����� ���:

open (��,"/etc/passwd");

while (<EP>) (

chomp;

print "I saw $_ in the password file!\n";

}

�������� ��������: ����� �������� ���������� ������� � ������� ������, ���������� ���� ��� ����� �� ������������ stdin.

���� �� ������� ���������� ����� ��� ������ ��� ���������� � ������ ������ � ���� ���-���� (� ������� ������� print), ���� ���������� ����� ��������� ����� �� �������� ������ print, ����� ���������� �����������. ������� ����� ������������ � ���������� ����������� ���� �� ������:

print LOGFILE "Finished item $n of $max\n";

print STDOUT "hi, world!\n"; # ��� print "hi, world!\n"

� ���� ������ ���������, ������������ �� ����� Finished, ���������� � ���������� ����� logfile, �������, ����������������, ��� ������ � ��������� �����. ��������� hi, world ������������ �� ����������� �����, ��� � ������, ����� �� �� ��������� ����������. �� �������, ��� stdout � ��� ���������� ����� �� ��������� ��� ��������� print.

���������� ��� ������ ����������� ������ �� �����, ��������� � ���������� $�, � ����, ��������� � ���������� $�. �� ������������ ����� ���, � ��� �� ������������ �� ��������� ���������� ���������:*

open(IN,$a) || die "cannot open $a for reading: $!";

open(OUT,">$b") || die "cannot create $b: $!";

while (<IN>) { # ��������� ������ �� ����� $� � $_

print ��� $_; # ������� ��� ������ � ���� $� ) close(IN) || die "can't close $a: $!";

close (���) || die "can't close $b: $!";

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

������ �� ������, ��� ������� ���������� ����� ��� ������, ��������� ������������ ���� � ����� �� ������. �����������, �� ������ ��������������, ��� ����� � ����� ������ �� ���������� (����� �������� ���������� ����������� ����� ����������� ������� ��� ����� ������� ��������� ���� ��������). ���� �� �� ������ �������� shell, �� ������������ �� ��� �������� ������������� ����� ����� ����� -� ���_�����. ����������� ������� � Perl ����������� �������� -� $filevar, ������� ��������� ���� ������������� �����, ��������� � ��������� ���������� $filevar. ���� ���� ���� ����������, ��������� � "������"; � ��������� ������ �������� ���� "����"**. ��������:

$name = "index.htm";

if (-e $name) (

print "I see you already have a file named $name\n";

} else (

print "Perhaps you'd like to make a file called $name\n";

}

* ���� ��� ������� ������ File:: Copy ���� ������ ����������� ������.

** ��� �� ������ ������, ���� �� ��������� � lock-������� ��� ���� ����� ����� ���������� � ��������. � ���� ������ ��� ����� ���������� � �������� sysopen � flock, ������� ������� � ����� Programming Perl, ��� ������� �������, ����������� � ����� 19.

 

������� �������� -� � ����� ��������� ���������, ���������� �������� ���� ��������� ������, ������� ��������� �������. ��� ������, � ������� ����������� ������� ������ index-htmlindex.cgi � ������� ��������:

if (-� "index.htm" && -� "index.cgi") (

print "You have both styles of index files here.\n";

1

���������� � ������ ��������. ��������, -r $filevar ���������� �������� "������", ���� �������� � $filevar ���� ���������� � ����� ���� ��������. �������� -w $filevar ��������� ����������� ������ � ����. � ��������� ������� ���� � �������� ������������� ������ ����������� �� ����������� ������ � ������:

print "where? ";

$filename � <STDIN>;

chomp $filename; # ��������� ���� ����������� ������ ����� ������ if (-r $filename &S -w $filename) (

# ���� ����������, � ���� ������ ��� � ���������� � ����

}

���� ����� ������ �������� ��� �������� ������. ������ �������� �� �������� � ������� 10.1.

������� 10.1. �������� ��� �������� ������ � �� ��������

�����������

��������

-r

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

-w

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

-X

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

-�

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

-R

���� ��� ������� �������� ��� ������ �������� �������������, �� �� "�����������" ������������� (���������� �� -r ��� �������� � ������������� ����� ����� �������������� ������������)

-W

���� ��� ������� �������� ��� ������ �������� �������������, �� �� "�����������" ������������� (���������� �� -w ��� �������� � ������������� ����� ����� �������������� ������������)

-X

���� ��� ������� �������� ��� ���������� �������� �������������, �� �� "�����������" ������������� (���������� �� -� ��� �������� � ������������� ����� ����� �������������� ������������)

-0

���� ��� ������� ����������� ��������� ������������, �� �� "������������"������������ (���������� �� -� ��� �������� � ������������� ����� ����� �������������� ������������)

�����������

��������

-�

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

-2

���� ���������� � ����� ������� ������ (�������� ������� �� ������)

-s

���� ��� ������� ���������� � ����� ��������� ������ (�������� � ������ � ������)

-f

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

-d

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

-1

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

-S

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

-P

������ ������� � ����������� ����� (FIFO-����)

-b

������ ������� � ����-��������������� ���� (��������, ����������� ����)

-�

������ ������� � ����-��������������� ���� (��������, ���� ���������� �����-������)

-u

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

 

 

������

-g

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

-k

� ����� ��� �������� ���������� ���-�������

-t

���������� �������� isatty() ��� ������������ ����� ���� �������� "������"

-T

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

-B

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

-M

����� � ������� ���������� ��������� (� ����)

-A

����� � ������� ���������� ������� (� ����)

-C

����� � ������� ���������� ��������� ���������� ����������� (� ����)

 

����������� ���� �������� ���������� ������ �������� "������" ��� "����". � ���, ������� ����� �� ������, �� ������ ���������.

�������� -s ���������� �������� "������", ���� ���� ��������, �� ��� �������� ������� ����. ��� ����� ����� � ������, ������� ���������������� ��� "������" ��� ��������� ��������.

�������� -�, -� � -� (��-��, � ������� ��������) ���������� ���������� ���� �������������� � ������� ���������� ��������� �����, ������� � ���� � ��������� ��� ���������� �����������*. (��������� ���������� �������� ��� ���������� � �����; ����������� ��. �� man-��������, ����������� ���������� ������ stat.) ������������ �������� � ���������� �����,

* ��� �������� ������������ ������������ ������� ������� ���������, ����������� � ��������� ������� ������� � ���������� $ "�. ���� ������������� �������� ��������� � �������, ������� ��������� ����� ������ ������ ���������, ��� ����� ���� �������������.

 

��������������� ���������� ������� � ��������� �� 1 �������: 36 ����� ������������ ��� 1,5 ���. ���� ��� ������ ������ ����� ��������� ��������� ����� ���������� � ����� ������ (��������, � 3), �� �� �������� ������ �� �����, ������� ���� �������� ����� ������� ���� �����, �� �������� ������ � �� �������� �����. ��� ������, ��� ��� ��������� ���� ������, �������� ������������� ���������� ��� ������� ��������� � ��������� �� ���� �� ������� ����, ���, ��������, ����� ����� ������������ �������� ��������� ����������*, � �� �������� ��������� ��������.

��� �������� ����� �������� �� ������ � ������� ������, �� � � �������������. ��� ����� ����� ������ ������� � �������� �������� ���������� �����. ���, ����� ���������, �������� �� ��� ���������� ����, �������� ��� somefile, ����� ������� ���������:

if (-� SOMEFILE) (

# ����, �������� ��� SOMEFILE, �������� ��� ����������

}

���� ��� ��� ���������� ����� �� ������� (�.�. ���� ������ ��������

*� ��� -s), �� �� ��������� � �������� �������� ������� ����, ��������� � ���������� $_ (����� ��� ����������!). ���, ����� ��������� ������ ���� ������ � ����������, ����� �� ��� �������� ��� ������, ����� ������-�������� �������� ���������:

foreach (@some_list_of_filenames) (

print "5_ is readable\n" if -r; # �� ��, ��� � -� $_ >

������� stat � Istat

�������������� �������� ������ ���������� ��� �������� ��������� ��������� ����������� ����� ��� ����������� �����, �� ������ ���������� � �� ������� �������� ������. ��������, �� ������������� �������� ��������, ������� ���������� �� ����� ������ �� ����. ����� ��������� �� ��������� �������� � �����, �������� ������� stat, ������� ���������� ����������� ���, ��� ���������� ��������� ����� stat � POSIX (��������, �� ������� ������, ��� �� ������ �����).

������� ������� stat � ���������� ����� ��� ���������, ����������� �������� ������������ ��� �����. ������������ �������� � ���� undef, ���� ����� ��������, ���� 13-���������� ������**, ������� ����� ����� ������� � ������� ������ ������ ��������� ����������:

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev, $size, $atinie, $mtime, $ctime, $blksize,$blocks) = stat (. . .)

* ��� �������� int.

** ���� ��� ������ ��������� ������� ��������, ������������ �������� stat, ������ ���������� � ������ File: :stat, ������� ���������� � ������� 5.004. �� ������������ ������ � ���� ��������� ��������� �������:

$file_owner = stat($filename)->uid

 

����� ����� ������������� ������ ��������� stat, �������� ��������� �� man-�������� stat(T). ����������� ������� ����������� ��� ��������� ���������.

��������, ����� �������� �������������� ������������ � ������ �� ����� �������, ����� ��������:

($uid,,$gid) = (stat("/etc/passwd")) [4,5];

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

����� ������� stat � ������ ������������� ������ ���������� ���������� � ���, �� ��� ��������� ��� ������, � �� �������� � ����� ������ (���� ������ ��� �� ��������� �� ���-�� � ������� ������ �����������). ���� ��� ����� ���������� � ����� ������������� ������ (������� ������ �����������), ����������� ������ stat ������� istat (������� ���������� �� �� ������ � ��� �� �������). � ����������, ������� �� �������� �������������� ��������, ������� istat �������� ���������� stat.

��� � � ��������� �������� ������, ������� ������� stat � Istat �� ��������� � $_. ��� ������, ��� �������� stat ����� ����������� ��� ������, �������� ��������� ���������� $_.

����������

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

1. �������� ��������� ������ ����� ����� �� stdin, �������� ����� ����� � ������ ��� ����������� � ������������ ������ ������ ������ ����� � ����������. ��������, ���� ������� ��� fred, � ���� fred ������� �� ���� �����, a a a, bbb � � � �, �� ������ ������� fred: � � �, fred: bbb � fred: ���.

2. �������� ���������, ������� ���������� ������ ��� �������� �����, ��� ��������� �����, ������� ������ � ���������� ������, ����� ���� �������� ������� ���� � ��������, ������� ��� ���������� ������� ���� �������. ���������� ��������� ��� ��������� � ����������� �������. ������ �� �� �������� ������������ ���� (�� ��������� ��� �������� ��� ���-�� ������!)? ������ �� �� ������������ ������� ���������� ��������� � ������� ������? ������ �� �� ������������ $1 � ���������� ������?

3. �������� ��������� ������ ������ ���� ������ � ������ ���������� � ���, ����� ����� �������� ��� ������, ��� ������ � (���) ��� ����������, � ����� ����� �� ����������. (������ �������� ����� ��������� ��� ������� ����� ����� �� ���� �� ������ ��� ��� ���� ������������ ���� ����� ��������� �� ����. �� �������� ������� ������ ����� ������ � ����� ������� ������������ ����� �����.)

4. �������� ��������� ������ ������ ���� ������ � ������ ����� ��� ������ �������. �������� �� ����� ��� ����� � ��� ������� � ����.




|     �����     |     ������     |


| ���������� | ����������� | �������� | ������
| ����� 1 | ����� 2 | ����� 3 | ����� 4 | ����� 5 | ����� 6 | ����� 7 | ����� 8 | ����� 9 | ����� 10
| ����� 11 | ����� 12 | ����� 13 | ����� 14 | ����� 15 | ����� 16 | ����� 17 | ����� 18 | ����� 19
| ���������� � | ���������� � | ���������� � | ���������� � |