Вы находитесь на странице: 1из 58

Perl /b/

: 4.1.2013 19:34

1.1
1.2
1.3

? . . . . . . . . . . . . . . . . . . . . . . . . .
! . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . .

1.4
1.5

. . . . . . .
. . . . . . . . . .
1.5.1 . . . . . . . .
1.5.2 . . . . . . . .
1.5.3 . . . . . . . . . .
.
1.6.1 . . . . . . .
1.6.2 . . . . . . . . .
.
1.7.1 . . . . . . .
1.7.2 . . .

1.6
1.7

II

. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .

. . . . . .
. . . . . .

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

? . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .

/ . . . . . . . . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

1.8
1.9
1.10
1.11
1.12

III

IV

1.13 UTF8
1.14 . . . . .
1.14.1 . . . . .
1.14.2 .
1.15 . . . . . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

1.16 . . . . . . . . . . . . . . . . . . . . . . . . .
1.17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

FAQ

2
3
3

5
7
7
9
11
12
12
17
24
25
26
28

31
32
33
34
38

41

41
42
45
46
47

49

52
55

56

. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .

56
57


1.1

,
. , , .
.

, 1 , Perl. . ,
, . -? . ,
- , , ? ,
. .
, - .

. !
. , - . . ,
. . ,
! , .
.
. : 1.
. . ,
!
- .
Perl? , ,
Perla . . . , . Perl
( Perl ). , python php.
Perl . !
1

1.2

, / . . . , , .
. . , ! .
, , - .
, , , - ( ) ? , . ,
.
. - , . .
. .
, !
!
1.3

@conference.jabber.ru
http://perlobaron.tumblr.com/
perlothread@gmail.com
: 410011545542026
, !
/.

PHP , ,
Perl , , .

Jon Ribbens

. :
Perl , ,
.

, , . Perl Perl
. Perl . ? Perl , , 2
.
Perl ( ) . , ? , -
. ,
. .
. , . ,

. Perl, .
, ?
1. Perl
2.
Linux .
Windows:

:
http://strawberryperl.com/
Notepad++:
http://notepad-plus-plus.org/download/
2

Perl

windows .
:
1. - , .
2. bat.
:
cmd.exe
.
windows
Perl. , cp866, , cp1251.
, , ,
. ,
Notepad++. , /
:
>> 866
, !
1.4

?
! :

1
2

#!/usr/bin/perl -w
print "YOBA ETO TI?\n"; #

, ?
. . .
3 . ,
. !
. , windows ! -.
! ,
. ,
#!. , .
Linux:

:
1. 2 foo4 .pl.
:
3 - .
4 foo, bar baz . -,

-,

perl foo.pl
. foo.pl ,
:

perl /home/YOBA/perl/foo.pl
2. , .
foo.pl .

#!. , , Perl. -w . . , ,
/usr/bin. ,
, :

which perl
:

chmod +x foo.pl
! :

./foo.pl
3. ,
. -e 5
, :

perl -e print "YOBA ETO TI?\n";


Windows:

. pl , .
, . :

perl foo.pl
Windows . ,
win , print - qq(). , ,
. ,
Linux. :
5

, ,

Perl .

perl -e "print qq(YOBA ETO TI?\n);"


! !
. ? print. - .
. .
.
. ,
. ? !
, . . ,

(
). ! ? ,
. :
t
n

. . . .
. # . ? . , .
1.5
1.5.1

. - .
, - . , . ,
, ! , .
.
, Perl .
, 6 .
, ,
. 7
. :
6 -
7 - ,

, ..

$YOBA
$ololo
$ded_doest
. . ,
- .
. $ . , s
scalar. @ , , . a
array. % . .
! Perl . , $YOBA
$Yoba , !
,
.
.
- :

$foo= 12345679;
? - foo. .
=
==. , = : $foo
12345679. 12345679 . , , .
. -
, .
.
( ), ( )
( ). . ($foo)
(12345679). , (, , ) , .
:

=
;

$foo
12345679

1 #!/usr/bin/perl -w
2 $foo= 12345679;
3 $foo= $foo * 9; # $foo*= 9;
4
print " \$foo!!!$foo\n";
8

.
, , print .
, ?
() $.
, . :

$bar= "SERBIA STRONG!\n";


, :

$baz= 99/2*3+4-5;
:

. ,
.
$. ( , , ) .

$x = 10; #
$y = $x + 1; # $x ...
$z = $x . string; # ...
$ref = \$x; # $ref $x
$$ref = 10; # $x 10
1.5.2

, .
, .
. ,
. , ! - !
. ,
? ,
, :)
, - :

1
2
3
4
5

#!/usr/bin/perl -w
@jail=(" \n", " \n",
" \n", " \n");
$temp= $jail[3];
9

6
7
8

$jail[3]= $jail[0];
$jail[0]= $temp;
print @jail;

3 4 .
,, .
, .
, ( 8).
. ,
. ,
! .. ,
. 0

. 5 7 .
$temp.
,
, !
, . ,
, . ,
( ), .
:

print $jail[2];
. -1 , -2 .
:

print

$#jail +1 . "\n";

() . $#_ ,
-
.
():

push @array

pop @array; # .
unshift @array; # .
shift @array; # .
? ,
- . , :
10

1
2
3
4
5
6

#!/usr/bin/perl -w
@jail=(" \n", " \n",
" \n", " \n");
print " " . pop(@jail);
print " " . $jail[-1];

pop .
( )
. .. . , pop
, 5 pop(@jail) . , . 6
, ,
.
! , qw() ,
, . :

@a= qw( 4 5 6 7);


1.5.3

, .
,
, .
, .
% . , ,
, $ . :

1 #!/usr/bin/perl -w
2 use POSIX;
3
4 %pipirka= ( => 13.21, => 13.97,
5
=> 13.78, => 13.78,
6
=> 12.77);
7
8 print " , ?\n";
9 $country= <STDIN>;
10 chomp $country;
11
12 print " " . ceil($pipirka{$country})
13
. " !\n";
11

, POSIX8 . ceil ,
. 4-6,
9
=> . Perl 10
, .
. , .

. , 10 . chomp
.
$country. / .
12 .
:

$pipirka{$country};
$country.

ceil, .
, ( ). !\n
1.6
1.6.1

...
- . 1666. (,
, ) , , , . , . ,
.
,
. .

8 POSIX , - .
Unix

9 =>

10

12

.
. . :

-
-
-
.
, .
Perl:
"" ""? Perl , , , .

( , , ,
*). (..
), ""( ),
"". "". Perl ? ,
( ), ( ) , "" "". .

# " ""

1-1 # 0, " ""

1 # "1 ""

# , ""

"1"# " ""

"00"#

" ""( ,

"0.000"# "" ,

undef # , ""

: .
Perl:

"ALLO YOBA\n"
13

-1
2
555
0.1
. :

$foo;
$foo ,
.
Perl ? :

1 if( _)
2 {
3
..()..
4 }
5 else
6 {
7
..()..
8 }
if
. ( 2 4 ).
,
else . 5-8 .
, .
,
:

if (1)
{
# 1
}
elsif (2)
{
# 2
}
else
{
#
}
14

Perl .

unless :

unless()
{
#
}
, .
:

#!/usr/bin/perl -w
print "YOBA, SKOLKO TEBE LET?\n";
$age= <STDIN>;
chop $age;
if($age < 18)
{
print ", !\n";
}
else
{
print "PSH PSHHH\n";
}
, , .
:




: not .
. , .
or . ,
, . :

1 or 0 -
1 or 1 -
0 or 0 -
- ,
:

1 and 0 -
15

and .

0 and 1 -
1 and 1 -
,
. :

_ _;
:
print " !\n" if $bus == 359;
, :

==
!=
>
<
<=
>=
,
. ,
, . . .
:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#!/usr/bin/perl -w
use POSIX;
%pipirka= ( => 13.21, => 13.97,
=> 13.78, => 13.78,
=> 12.77, => 0);
print " , ?\n";
$country= <STDIN>;
chomp $country;
die $country . "? , \n" unless exists $pipirka{$country};
print " " . ceil($pipirka{$country})
. " !\n";

16

, ,
. die
,
.
12 . ,
. . unless () , unless , . exists .
. exists ,
,
, !
(,
..), =>
.
1.6.2

- .
, .
,
.
,
, . :
1.
2. - ,
.
3. . , , .
. , while .
,
, . :

while()
{
# -
}
:

17

1 #!/usr/bin/perl -w
2
3 @porn_stars= (" ", " ", " ");
4
5 $i= 0;
6 while($i <= $#porn_stars)
7 {
8
print "$porn_stars[$i]\n";
9
$i++;
10 }
$i . 6 " ?".
- . , .
, Perl , . :

while($i= pop @porn_stars)


{
print "$i\n";
}
pop ,
$i .
, pop 0, - .
:

2 2 4 4 6 6 8 8
... =
1 3 3 5 5 7 7 9
2
. . :

1
2
3
4
5
6
7
8
9
10
11
12

#!/usr/bin/perl -w
$a= 2;
$b= 1;
$pi= 1;
$e= 500000000;
$loop_counter= 1;
while($a < $e)
{
# . , $pi= $pi*($a/$b);
$pi*= $a / $b;
18

13
$b+= 2;
14
$pi*= $a / $b;
15
$a+= 2;
16
$loop_counter++;
17 }
18
19 $pi*= 2;
20
21 print " : $loop_counter\n : $pi\n";
$e . $e , 250 11 , 5 . 3 7
() ,
. $a .
9 $a
$e , - . , -
, . ,
.
, . , .
while .
Perl :

for(__; ; _)
{
#
-
}
:

1 #!/usr/bin/perl -w
2
3 @porn_stars= (" ", " ", " ");
4
5 for($i= 0; $i < $#porn_stars; ++$i)
6 {
7
print "$porn_stars[$i]\n";
8 }
.
- . ,
11 . ,
.

19

. ( )
, :

...
for(;;)
{
# -
}
...
.
.. .
, , . last . , . . ?
:

1
2
3
4
5
6
7
8
9
10

#!/usr/bin/perl
@a= (21,33,55,6,78,343,634,632314, 54);
for($i= 0; $i <= $#a; ++$i)
{
if($a[$i] == 634) {last;}
}
print "$i - !\n";

. - ( 5), -
( 7). - last 7.
. , , .
- , :

1
2
3
4
5

#!/usr/bin/perl
@a= (21,33,55,6,78,343,634,632314, 54);
push @a, 634;
20

6
7 for($i= 0; ; ++$i)
8
{
9
if($a[$i] == 634) {last;}
10 }
11# $#a- . ,
12 if($i < $#a) {print "$i - !\n";}
13 else {print " !\n";}
, . - ! . .
last :

next
redo
. , .
! !
, , foreach - . :

foreach _ ()
{
# -
}
, .
. !
, . :

1 #!/usr/bin/perl -w
2
3 @porn_stars= (" ", " ", " ");
4
5 foreach $star (@porn_stars)
6 {
7
print "$star\n";
8 }
. .
21

, , map.
- foreach.
, , , map .
. :

22

1
2
3
4
5
6
7
8
9

#!/usr/bin/perl -w
use strict;
use warnings;
my @a= ("one\n", "two\n");
my @b= map { my $scalar= uc $_; } @a;
print @a;
print @b;

@a,
. ,
. (
$scalar). .
$_. ,
@a. @a ,
, @a, .

23

1.7

, ,
, .
.
Rick Cook

17 $i++ while . $i , Perl


. , :
1.
2. .
, . , .
! . . ,
, .
, . 70%
.
print ,
- perldebug !
,
18. :

perl -d pi.pl
, ! ? .
5 10:

l 5-10
, .
. .
,
:

b 6
. r . 6 . p
- . :
24

p $e
s
( ) c . ,
enter. d , D q . .
Perl , . google
!
1.7.1

, ,
.

?
, . . .
, CPAN, Perl .
, ,
. 12
Perl.

1
2
3
4
5
6
7
8
9
10
11
12

#!/usr/bin/perl
use warnings;
use strict;
use constant
{
SPEED_OF_LIGHT => 299_792_458,
AVERAGE_DISTANCE_TO_MOON => 384400000,
};
print " " .
AVERAGE_DISTANCE_TO_MOON/SPEED_OF_LIGHT . "\n";

. , .
, .
5.8 :

Internals::SvREADONLY($RO_SCALAR => 1);


12

Perl

25

$RO_SCALAR, . :

Internals::SvREADONLY($RO_SCALAR => 0);


.
,
.
. , :

*PI = \3.14159265358979323;
:

print "$PI\n";
typeglob (
). Perl, -
. .
: , .
.
?
1. ;
2. ;
3. :
.
, , 13 , .
1.7.2

strict! ,
. . ,
! use Hash::Util, Luke.
: http://perldoc.perl.org/Hash/Util.html
. :
1. lock_keys/unlock_keys- /
. .
2. lock_value/unlock_value- / , .
13

, -

, .

26

3. lock_hash/ulock_hash- .
. .
,
. !

27

II

, , - . :

1
2
3
4
5
6
7
8
9

#!/usr/bin/perl -w
sub hello
{
print "Sup /b/!\n";
}
hello;
hello();
&hello;

sub (subroutine ) . .
:

hello;
hello();
&hello;
(TMTOWTDI14 ,
Perl). hello , . ,
.
,
. ,
Perl ! Pascal
. , - , . - ,
.
( ), . :

1
2
3
4

#!/usr/bin/perl -w
sub allo($)
{
14 Theres

More Than One Way To Do It ( )

28

5
print "ALLO \U$_[0]! pshh pshh\E...\n";
6
}
7
8 allo("yoba");
9 $name= Anton;
10 allo($name);

"yoba" . .
, . , , .
. . ? U
E. ?
@_, - .
0, . . ,
, .
:

1 #!/usr/bin/perl -w
2 sub add($$)
3 {
4
$_[0] + $_[1];
5 }
6
7 print add(10,15). "\n";
. ?
. add(10,25)
7 25, Perl
. , Perl
.
, return :

1
2
3
4
5
6
7
8
9

#!/usr/bin/perl -w
use strict;
use warnings;
use v5.10;
sub average
{
return 0

unless @_;
29

10
11
12
13 }
14
15
16 say

my $sum= 0;
map {$sum+= $_} @_;
$sum/= @_;

" : " . average(1,2,3,4);

,
Perl 5.10, , say 16 . print, , . ,
. , .
. unless , . 0 ,
0 return 0.
11
,
, @_. 12
,
. :
.
,
.
, ? , .
. :

1 #!/usr/bin/perl -w
2
3 sub maternal_capital ($@)
4 {
5
$mother= shift;
6
@human_larvae= @_;
7
$profit= $#human_larvae * 200_000;
8
print " $mother!\n";
9
if ($profit) {print " " . ($#human_larvae +1)
10
. " , ${profit}.\n" ; }
11
else {print " .\n"}
12 }
13
14 @childrens= ( , , );
15 $modest_girl= " ";
16 maternal_capital($modest_girl, @childrens);
30

@_ - .
. . ,
shift , @_.
, shift . .
. .. - maternal_capital
. , . .
. @human_larvae.
, . , .
@human_larvae ! , @_ ,
@human_larvae.
1.8

-, . ,
. . - . ?
"Need For Speed" .
- . , ,
. , .
. , - .
. w speedup ( ?), - stop
.
.
, . -
.
, . . , , .
, . ,
. , . :

print "feghpfdghpsdkjflgkldfsgsd\n"
31

, print,
. , - . ,
.
-
- . .
-
, .
1.9

. .
: .

. .
:

use strict;
Perl . my .
,
. :

#!/usr/bin/perl
@a= qw(Dima Serega Artem Ondrei);
$length= @a;
for($i= 0; $i < $lenght; ++$i)
{
print "\U$a[$i]\n";
}
strict :

#!/usr/bin/perl
use strict;
use warnings;
@a= qw(Dima Serega Artem Ondrei);
my $length= @a;
for($i= 0; $i < $lenght; ++$i)
{
print "\U$a[$i]\n";
}
32

use warnings;
Linux :

#!/usr/bin/perl -w
-w ? Windows
.
, , .
1.10

, , (, , ,
etc)
. . - ,
.
,
, - .
? , for if.
. , .
, .
:
.

#!/usr/bin/perl -w
use warnings;
use strict;
my $scalar= "";
for(;;)
{
print "$scalar\n";
my $scalar= "";
print "$scalar\n";
last;
}
print "$scalar\n";
:

33

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

#!/usr/bin/perl -w
use strict;
use warnings;
sub profit($$)
{
my($salary, $tax)= @_;
$salary-= ($salary * ($tax / 100));
}
print " , ?\n";
my $salary= <STDIN>; chomp $salary;
print " ( )?\n";
my $tax= <STDIN>; chomp $tax;
print " : " . profit($salary, $tax) . "!\n";
. " ${salary} :3\n";

, .
8, , .
.
. "" ,
. 31 ( 5 6) :

my ($mother, @human_larvae)= @_;


. $salary
, $tax 1 . $salary ,
$salary, 13.
. ,
.
18, , .
1.11

, Perl
. Pascal
Perl ? / @_?

34

.
:

1
2
3
4
5
6
7
8
9
10
11
12

#!/usr/bin/perl -w
use strict;
use warnings;
use v5.10;
my $scalar= 12345;
my $ref= \$scalar;
say $ref;
++$$ref; # : ++${ $ref };
say $scalar;

$scalar
$ref. 10
$scalar.
ref ( ),
. 11 .
$ ref,
, .
:

1
2
3
4
5
6
7
8
9
10
11

#!/usr/bin/perl -w
use strict;
use warnings;
use v5.10;
my @arr= qw(foo bar baz fobar qux quux);
my $aref= \@arr;
say $$aref[1];
say ${ $aref}[2];
say $aref->[3];

9-11 . ( ) .
:

1
2
3

#!/usr/bin/perl -w
use strict;
35

4 use warnings;
5 use v5.10;
6
7 sub merge ($$)
8
{
9
my ($first, $second)= @_;
10
@ {$first}= sort (@ {$first}, @ {$second});
11 }
12
13 my @a= qw(corge grault garply);
14 my @b= qw(foo bar baz quux);
15 merge(\@a, \@b);
16 map {print "$_ "} @a;
, .

@a. 10,
, ,
. sort
, , $first.
,
:

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

#!/usr/bin/perl -w
use strict;
use warnings;
use utf8;
use v5.10;
my %EOT=

);

(
=>
=>
=>
=>

,
,
49,
1.76

my %adress= (

=>

=>

=>
=>
=>
);

-,
,
76,
15,
9(943)345-228-0

36

21
22 my @data_base= (\%EOT, \%adress);
23 say $data_base[0]{};
22 ,
data_base. 23
.
.
:

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

#!/usr/bin/perl -w
use strict;
use warnings;
use utf8;
use v5.10;
my @data_base= (
{

);

=>
=>
=>
=>

,
,
49,
1.76},
=> -,
=> ,
=> 76,
=> 15,
=> 9(943)345-228-0}

say $data_base[0]{};

. .
Perl :

1
2
3
4
5
6
7
8
9

my @a= ([ , , , ],
[ , , , ],
);
say $a[0][1];
push @a[0], , ;
say $a[0][2];

37

@a, , .
.
. , push 7 ( , ). :

push (@a[0], , );
, ,
Perl . , ,
.
.
1.12

, .
. - . ,
.
, , , ,
, ASCII
(, , , ). :

1
2
3
4
5
6
7
8
9

#!/usr/bin/perl -w
use strict;
use warnings;
-e "some.txt" or die " .\n";
open(my $fh ,"<some.txt") or die " \n";
print $_ while <$fh>;

? -e 5, . _- or die _
. . (
7). open . , . , . < , . open -

38

. ? ,
.
while :
<_>
,
($_),
. , cat!
.
:

STDIN .
STDOUT , .
STDERR , .
print, , STDOUT, die STDERR. Linux
:

my_script.pl 2>./errors
, > .
:

1
2
3
4
5
6
7
8
9
10
11

#!/usr/bin/perl -w
use strict;
use warnings;
open(my $fh ,">some.txt") or die " \n";
my $line;
print $fh "$line" while ($line= <STDIN>) ne "\n";
close($fh);

. > .
( ).
, . .
print ( 9),
$fh, STDOUT. ? print
:
39

print STDOUT " \n";


STDOUT !
.. print .
, . ,
.

40

III


,
.

1.13

UTF8

. ,
! ,
. 256 ( 0
255). ?
, . , 128 ( 0 127) ASCII (American
Standard Code for Information Interchange). , 128 .
, 128 . ! - , .
- , , !
, . , .

. , ICQ .
, 2-3 . !
utf8. , .
!
, ,

. .
UTF8,
! 128 ASCII,
. . Jabber ( XMPP)
UTF8 ! .
, , utf8. , ,
utf8, . ASCII ,

Linux ut8, ! ,
41

cp866,
. cp1251. ! .
utf8,
, .
.
windows,
:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

use utf8;
use Encode;
use strict;
# :
#use encoding cp866;
binmode(STDIN, :encoding(cp866));
binmode(STDOUT,:encoding(cp866));
my $out= uc " \n";
print $out;
my $t= <STDIN>;
$t =~ s/\w*(.*)/$1/;
print $t;

, ,
utf8. utf8 ,
! Encode . ,
/ cp866 utf8,
, 6
7 8. , .
STDIN cp866 utf8 STDOUT utf8
cp866.
Windows ,
linux ,
.
1.14

! ,
. . .
? :
42

(. regular expressions, . RegExp, RegEx,


. ) , (-, . wildcard characters). - (. pattern, - , ),
.

. ,
notepad.exe. ,
. ,
, .
:

1
2
3
4
5
6

#!/usr/bin/perl -w
use strict;
use warnings;
my $text= "ALLOU, YOBA... PSH... PSHHHHHH...";
print "YOBA SLUSHAET\n" if $text =~ /YOBA/;

6 = ,
$text.
.
. :

$text =~ /YOBA/;
, .
. ,
- :

1
2
3
4
5
6

#!/usr/bin/perl -w
use strict;
use warnings;
my $text= "ALLOU, YObA... PSH... PSHHHHHH...";
print "YOBA SLUSHAET\n" if $text =~ /YOBA/i;

i,
, - .
. :

43

1
2
3
4
5
6
7

#!/usr/bin/perl -w
use strict;
use warnings;
my $text= "ALLOU, ABU... PSH... PSHHHHHH...";
print "-$text\n";
print "-$1 SLUSHAET\n" if $text =~ /\w*, (\w+)/;

- .
. ?
. , . \w
. .
.
, . . $1, ,
$2 . ,
7.
. ,
. .
. $1, .
.
,
:

1
2
3
4
5
6
7

#!/usr/bin/perl -w
use strict;
use warnings;
my $text= "ALLOU, ABU... PSH... PSHHHHHH...";
print "-$text\n";
print "-$+{name} SLUSHAET\n" if $text =~ /\w*, (?<name>\w+)/;
:

: (?<>___)
, %+,
name , 7.

44

1
2
3
4

#!/usr/bin/perl -w
use strict;
use warnings;
my $text= Variable names in Perl can have several formats. Usually,
they must begin with a letter or underscore, in which case they
can be arbitrarily long (up to an internal limit of 251 characters)
and may contain letters, digits, underscores, or the special
sequence :: or \. In this case, the part before the last :: or \
is taken to be a package qualifier; see perlmod.;

5
6 while ($text =~ /\b(\w{5,})\b/g)
7 {
8
print "$1\n";
9 }

6 ,
. , :

\b\b \b\b \b\b? \b\b \b\b \b\b \b\b \b\b?


. , ..
.
Perl , , , . 6 .
. , , .
g . . ,
.
1.14.1

, , :

#!/usr/bin/perl -w
1 use strict;
2 use warnings;
3 my $text= Variable names in Perl can have several formats...;
4
5 $text =~ /(.*\b)/g;
6
7 print "$1\n";

45

, .
. , s
. !

$text =~ /(.*?\b)/g;
. :)

. , ,
.

$text =~ /(.+?\b)/g;
, :

1
2
3
4
5
6

#!/usr/bin/perl -w
use strict;
use warnings;
my $text= a aaaaa aaaaaaaaaaaaaa;
print "$1\n" while ($text =~ /(a*)/g);

,
a, aaaa aaaaaaaaaaaaaa. g .
1.14.2

( ) , .
, A Z.
,
.

1
2
3
4

#!/usr/bin/perl -w
use strict;
use warnings;
my $text= Variable names in Perl can have several formats. Usually,
they must begin with a letter or underscore, in which case they
can be arbitrarily long (up to an internal limit of 251 characters)
and may contain letters, digits, underscores, or the special
sequence :: or \. In this case, the part before the last :: or \
46

is taken to be a package qualifier; see perlmod.;


5
6 while ($text =~ /\b([A-Zu]+\w*)\b/g)
7 {
8
print "$1\n";
9 }

u .
,
, :

1
2
3
4
5
6
7
8
9

#!/usr/bin/perl -w
use strict;
use warnings;
my $text= foo bar 666 42 baz quux 124;
while ($text =~ /\b([^a-z ]+?)\b/g) {
if ($1 eq "") {next;}
print "$1\n";
}

.
,
Perl. , .
.
! .
1.15

, - . , . :

s///
:

#!/usr/bin/perl -w
use strict;
use warnings;
my $text= Variable names in Perl can have several formats. Usually,
they must begin with a letter or underscore, in which case they
can be arbitrarily long (up to an internal limit of 251 characters)
and may contain letters, digits, underscores, or the special
sequence :: or \. In this case, the part before the last :: or \
47

is taken to be a package qualifier; see perlmod.;


$text =~ s/\b\w{3}\b/XXX/g;
print $text;
.

:

1 #!/usr/bin/perl -w
2 use strict;
3 use warnings;
4 use utf8;
5 #
6 #binmode(STDOUT, :encoding(cp866));
7 binmode(STDOUT, :encoding(utf8));
8 my $text= ;
9
10 $text =~ s/(\w*).(\w*)/$2-$1/;
11 print "$text\n";
:

1 #!/usr/bin/perl -w
2 use strict;
3 use warnings;
4 use utf8;
5 binmode(STDOUT, :encoding(utf8));
6 my $text= .;
7 my $text2= ;
8
9 $text =~ s/(\w*)(.*)\./$1, \l$1, \l$1-$2!/;
10 print "$text\n";
11
12 $text2 =~ s/(.*)/$1 ! - !/;
13 print "$text2\n";
.
l,
. .
.

48

IV


. .
.
. . ,
.
, . .
. , ,
.
, . , .
Perl CPAN. .
, . .
. , :

1
2
3
4
5
6
7

package coolstuff;
BEGIN {print "Entering " . __PACKAGE__ . "\n";}
sub coolfunkshun {print "a" x 16 .v10;}

AUTOLOAD {print "No such funkshun $AUTOLOAD\n"};


8 return 1;
9 END {print "Exiting ". __PACKAGE__ . "\n";}
coolstuff.pm, , .

coolstuff. BEGIN END ,
.
, . ,
. .
, /
.
__PACKAGE__ , .
49

AUTOLOAD .
, .
$AUTOLOAD . AUTOLOAD,
.
return 8 ,
, / .
:

#!/usr/bin/perl
use warnings;
use strict;
require coolstuff;
&coolstuff::coolfunkshun;
&coolstuff::anotherfunkshun;
do, require use.
/ ,
. , do require
. do require
require, ..
. ! , -
/ do.
, , .
( ,
),
, :: .
, my, . main.
_::_
.
( , ?)

package coolstuff;
BEGIN {
use Exporter();
@ISA= qw(Exporter);
@EXPORT= qw(&coolfunkshun $scalar);
}
sub coolfunkshun {print "a" x 16 .v10;}
50

AUTOLOAD {print "No such funkshun $AUTOLOAD\n"};


return 1;
END {}
:

#!/usr/bin/perl
use warnings;
use strict;
use coolstuff;
&coolfunkshun;
&coolstuff::anotherfunkshun;
- . Exporter ,
/,
. @ISA .
,
@EXPORT. ,
. - .
? , . 4 . , ( ) .
. :

#!/usr/bin/perl
use warnings;
use strict;
use mycounter;
&show;
&inc;
&show;
:

package mycounter;
51

BEGIN {
use Exporter();
@ISA= qw(Exporter);
@EXPORT= qw(&inc &dec &show);
}
our
sub
sub
sub

$counter= 0;
inc {$counter++;}
dec {$counter--;}
show {print "$counter\n";}

AUTOLOAD {print "No such funkshun $AUTOLOAD\n"};


return 1;
END {}
.
.
$counter :

$mycounter::counter;
$mycounter ,
my .
,
: my, our, local.
1.16

,
. ,
. , .
15 Perl . our
, .
, , .
main.
.
,
:
15 ,

52

#!/usr/bin/perl
use strict;
use warnings;
our $scalar= " !";
sub tell_the_truth
{
chop $scalar;
$scalar= $scalar . "- !";
}
tell_the_truth();
print "$scalar\n";
. tell_the_truth,
,
.
local , , :

#!/usr/bin/perl
use strict;
use warnings;
our $scalar= " !";
sub tell_the_truth
{
local $scalar= $scalar;
chop $scalar;
$scalar= $scalar . "- !";
}
print tell_the_truth() .v10;
print "$scalar\n";
, ,
. ,
,

53

, , . , v10 , ,
.
: " , ?! -
, ?". ! .
. , - - -
?
- my
. .
.

#!/usr/bin/perl
use strict;
use warnings;
my $text= "aXaxAx pRiVeT))). Ja ZaBolela(((((((((((";
sub make_readable
{
sub fix_smiles
{
$text =~ s/\){2,}/ :-D/g;
$text =~ s/\({2,}/ :,(/g;
}
sub fix_register
{
$text= lc($text);
$text =~ s/^(\w)/\u$1/;
$text =~ s/([.?!]+) (\w)/$1 \u$2/g;
}

fix_register();
fix_smiles();
my $text= pop @_;

print make_readable($text) .v10;


$text fix_register fix_smiles ,
$text .
54

1.17

, ,
. ,
.pm. , ,
,
.
, . ,
,
, , foo\bar\:
use foo::bar::MyModule;
, .pm, ( ) .

55

FAQ

1.

Perl?

Perl Makes You Happy!


Tim Bunce

2.

Perl?

. Perl . ,
. . Perl .
- 2d ( Perl SDL),
.
. - : ,
, shell-:
Perl ,
, , ,
Web-,
.

3.

,
. , ,
UTF8 .
, , . . LATEX :
, .
( ): . pdf ,
. :

Unrecognized character \x{2019}; marked by <-- HERE after


de(STDIN, <-- HERE near column 17 at 2.pl line 6.
. , .
! .

56

4.

Perl6? Perl5, 6 Perl?

Perl5 Perl6 . C++.


. .
5.

strawberryperl?

, Windows,
strawberryperl.

1.

! . .
jid16 , .
2.

.
3.

. .
,
.
4.

LATEX.

16 jabber

VIM.

ID

57