. Perl --------------------------------------------------------------- © Copyright (C) : maslov@klgtts.kaluga.su mailto:maslov@klgtts.kaluga.su maslov@news.kaluga.rosmail.com mailto:maslov@news.kaluga.rosmail.com --------------------------------------------------------------- Perl 5.003 Unix FreeBSD 2.1.0. ===============================================================
* * Perl. . Perl 5.003 Unix FreeBSD 2.1.0. , . (C) . * * , , Perl 5.003 FreeBSD 2.01. OS/2 , MS-DOS Windows NT , . 1 test1.pl : #!/usr/local/bin/perl # test1.pl print " !\n"; . #!/usr/local/bin/perl -. - -. # test1.pl . '#' " ", . . . print " !\n"; . " !". print - "". - , \n - ';' - . . ';'. '#' - . "hello", "hello all", " ", " " . .. , 10$, ! , " ". , , . test1.pl . : chmod +x test1.pl . : which perl : /usr/bin/perl : perl: Command not found. . . man ( - man manual ). 01 which. : test1.pl 50$ , : ./test1.pl , . test1.pl , . 2 . /etc/passwd. #!/usr/local/bin/perl open(PASS, "</etc/passwd") || die " !"; while(<PASS>) { print; } close(PASS); : open(PASS, "</etc/passwd") || die " !"; "" .. PASS " !" . while(<PASS>) $_. { . print; $_ } . close(PASS); . - . cat /etc/passwd. ! ( ). 3 . #!/usr/local/bin/perl open(PASS, "</etc/passwd") || die " !"; while(<PASS>) { ($login, $pass, $uid, $gid, $name, $home_dir, $shell) = split(':'); print "$login \t $name\n"; } close(PASS); : ($login, $pass, $uid, $gid, $name, $home_dir, $shell) = split(':'); , ':'. print "$login \t $name\n"; login - . '\t' - . 4 . #!/usr/local/bin/perl open(PASS, "sort -n -t : +3 -4 +0 /etc/passwd|") || die " !"; while(<PASS>) { ($login, $pass, $uid, $gid, $name, $home_dir, $shell) = split(':'); print "$login \t $gid \t $name\n"; } close(PASS); : open(PASS, "sort -n -t : +3 -4 +0 /etc/passwd|") || die " !"; ! sort . . . #!/usr/local/bin/perl open(PASS, "sort -n -t : +3 -4 +0 /etc/passwd|") || die " !"; while(<PASS>) { ($login, $pass, $uid, $gid, $name, $home_dir, $shell) = split(':'); write(); # . } close(PASS); exit 0; # ############ ################## format STDOUT = : ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $name ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $name ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $name ---------------------------------------------------------------------- Login:@<<<<<<<< Uid:@<<< Gid:@<<< Home dir:@<<<<<<<<<<<<<<<<<<<<< $login, $uid, $gid, $home_dir ---------------------------------------------------------------------- . # : : - . . . 2-23-06,,, ---------------------------------------------------------------------- Login:uucryst Uid:1055 Gid:66 Home dir:/var/spool/uucppublic/ ---------------------------------------------------------------------- : ,,, ---------------------------------------------------------------------- Login:uudilen Uid:1075 Gid:66 Home dir:/var/spool/uucppublic ---------------------------------------------------------------------- , . ! * * : perl [] () : 1. '-e' 2. . #!/usr/bin/perl "" . 3. / . '-'. 1 2 '-x', '#!' 'perl'. '__END__'. '#!' . '#!/bin/perl -d' . "" . . . exit() die() exit(0) . : -O - . \0. -a (split) $_ $F. -n -p. -c . -d . -D D . -d14 . 1 p 2 s 4 l 8 t 16 o 32 c / 64 p -P 128 m 256 f 512 r 1024 x 2048 u 4096 L "" 8192 H 16384 X scratchpad 32768 D -e . -F -a -i '<>'. , . : perl -p -i.old -e "s///" file - "" "" file file.old -I includ- . -P /usr/include /usr/lib/perl. -l . . 1. -n -p 2. $\. print . -n . sed awk. BEGIN END . . -p -n . -P . 'if', 'else' 'define' .. - . -s . '-' , true. -S PATH . "#!" . -T "" . (setuid, setguid). CGI . -u . undump. -U . . -v . -w , , , , "", , , 100 . -x . '#!' perl. - '__END__' . DATA. * * () . (). , , 0 null. (). . '#' . (statements) . . . : sub test; # test $var1 = test $0; # . require use. . . . ';' ';' . eval{} do{} . single ';'. single : if EXPR unless EXPR while EXPR until EXPR EXPR - true false. while until do . if EXPR - "". EXPR true. : $var = 1; $var2 = 3 if $var > 0; # : $var2 = 3 while EXPR - "". EXPR = true : $var = 1; print $var++ while $var < 5; # $var : 1234 until EXPR - " ". EXPR = false : $var = 1; print $var++ until $var > 5; # $var : 12345 unless EXPR - " " . if. EXPR = false. : $var = 1; print $var++ unless $var > 5; # $var : 1 . . , eval{} '{}'. : if (EXPR) BLOCK if (EXPR) BLOCK else BLOCK if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK LABEL while (EXPR) BLOCK LABEL while (EXPR) BLOCK continue BLOCK LABEL for (EXPR; EXPR; EXPR) BLOCK LABEL foreach VAR (LIST) BLOCK LABEL BLOCK continue BLOCK , C Pascal. . if (EXPR) BLOCK - EXPR true . : $var =1; if ($var == 1) { print $var,"\n"; } : 1 if (EXPR) BLOCK else BLOCK2 - EXPR=true BLOCK BLOCK2. : $var =2; if ($var == 1) { print "\$var = 1\n"; } else { print "\$var 1\n"; } : $var 1 if (EXPR1) BLOCK1 elsif (EXPR2) BLOCK2 ... else BLOCK - EXPR1=true BLOCK1 EXPR2=true BLOCK2 ... BLOCK. : $var = 1; if ($var == 0) { print "\$var = 0\n"; } elsif ($var == 1) { print "\$var = 1\n"; } else { print " \$var\n"; } : $var = 1 while BLOCK EXPR = true. LABEL ':'. next, last redo. . continue EXPR. EXPR3 for next. . next - continue . .. . : M1: while ($i < 6) { ++$i; # 1 next M1 if $i < 3; # $i < 3 ++$i; # 1 } continue { print "$i "; # $i } : 1 2 4 6 last - break . . continue . : M1: while ($i < 6) { ++$i; # 1 last M1 if $i > 3; # $i > 3 ++$i; # 1 } continue { print "$i "; # $i } : 2 4 redo - EXPR continue . : M1: while ($i < 6) { ++$i; # 1 redo M1 if $i == 3; # $i = 3 ++$i; # 1 } continue { print "$i "; # $i } : 2 5 7 for. LABEL for (EXPR1; EXPR2; EXPR3) BLOCK for for . EXPR1, EXPR2 = true , EXPR3. : for ($i = 2; $i < 5; ++$i) { print $i, " "; } print "\n i = $i\n"; : 2 3 4 i = 5 foreach. LABEL foreach VAR (LIST) BLOCK VAR LIST . VAR $_. VAR .. VAR . foreach for - . : @ = ("","",""); # foreach $i (@) { print $i," "; # $i } : : @ = ("","",""); # foreach $i (@) { $i = uc($i); # } print @; : : for $i (3,5,7) { print "$i "; } : 3 5 7 switch. . next, last, redo - . switch () . switch . if ... elsif ... else ... : SWITCH: { if ($i ==1 ) { .....; last SWITCH; } if ($i ==2 ) { .....; last SWITCH; } if ($i ==3 ) { .....; last SWITCH; } $default = 13; } . goto. goto , " ". "" "" . "" goto . goto. goto - , goto - goto - . goto - . goto - - . "M1:", "M2:" "M3:" $i 0, 1 2 : goto ("M1", "M2", "M3")[$i]; $i . goto - .. "" . POD . . . POD . '=' : = head '=cut'. . pod . * * : , (hashes) - . , - . . . '$' . : $var1 # 'var1' @var1[0] # 'var1' $var1{'first'} # 'first' "" "" '@'. : @var1 # var1 ( $var1[0], $var1[1], ... $var1[n]) @var1[1,3,10] # $var1[1], $var1[3], $var1[10] @var1{'first','last'} # ( $var1{'first'}, $var1{'last'} ) "" '%'. : %var, %key, %years '&' . : &sub1, &test_prog, test(12) '*'. $var1 $var1[0] , $var1[0] @var1. \ @var1 %var1 - . - . . $var1 $Var1 - . . . .. "" . : (list). - . - . : $var1 = <>; # @var1 = <>; # @var1 $var1 = (1,2,3); # $var = 3 - @var1 = (1,2,3); # @var1 1,2,3 . , . , . - . , . - . "" "" "" . . "TRUE" () 0. (null) - (defined) (undefined). - . , . defined() . $# csh. $#var1 - . 0 $#var1+1 . $#var1 - "" . $#var1 - , - . ( ). : @var1 = (4, 3, 2, 1); # $i = @var1; # print $i; # 4 - - print @var1; # , . scalar(). : print scalar(@var1); # "true" "-". 2/8 8 - "" 2 - . . : 123 123.123 0.12 .12E-10 0xABCD # 0377 # 0 - 123_456_123 # . (') (") : ', !' " ." . qq. . : $var1{first} $var1{'first'} - . __LINE__ ( ), __FILE__ ( ). __END__ , DATA. "" . . "here-doc" ( ) . << -. : print <<EOF; # EOF - . , ! . EOF . - . . : @var1 = (1, 2, '', 1.2); # . $var1[0] = 1, $var1[1] = 2, $var1[2] = '' $var1[3] = 1.2 $var1 = (1, 2, '', 1.2); $var1 = 1.2 .. . , . : @s1 = (1, 2, 3); # @s2 = (6, 7, 8); # @s = (0, @s1, 4, 5, @s2, 9, 10); # @s1 @s2 print @s; # : 012345678910 - . () -. , . : print ('','','')[1]; : . : ($a, $b, $c) = (1, 2, 3); # $a = 1, $b = 2, $c = 3 . : $x = (($a, $b, $c) = (1,2)); # $x=2 : -. : %_ = ('', 31, '', 30); : $_{} = 31, $_{} = 30 => . : %_ = ( => 31, => 30, ); typeglobs typeglog . '*'. , . . : sub new_sub { local *IN; # open (IN, "test") || return undef; # . . ......... return; } . * * . "" .. '$' - . "" : use English; : use FileHandle; : -> () () . . $_ $ARG - . . $ . . 1. $& $MATCH . $` . $' $POSTMATCH . $+ $LAST_PAREN_MATCH "". $* $MULTILINE_MATCHING 1 .. '\n' - . 0 . 5 . $. $INPUT_LINE_NUMBER $NR . . $/ $RS $INPUT_RECORD_SEPARATOR - . '\n' $| $OUTPUT_AUTOFLUSH . -0 $, $OFS $OUTPUT_FIELD_SEPARATOR print . $\ $ORS $OUTPUT_RECORD_SEPARATOR print . $" $LIST_SEPARATOR "$," "....". $; $SUBSEP $SUBSCRIPT_SEPARATOR - . '\034'. $# $OFMT . $% $FORMAT_PAGE_NUMBER . $= $FORMAT_LINES_PER_PAGE . 60 . $- $FORMAT_LINES_LEFT . $~ $FORMAT_NAME . . $^ $FORMAT_TOP_NAME . $: $FORMAT_LINE_BREAK_CHARACTERS . '^'. '\n-'. $^L $FORMAT_FORMFEED ( ). '\f'. $^A $ACCUMULATOR write() format(). formline() .. write() . $? $CHILD_ERROR : pipe, system(), wait() `...`. $! $ERRNO $OS_ERROR errno. - . die(). $@ $EVAL_ERROR eval(). $$ $PID $PROCESS_ID . $< $UID $REAL_USER_ID UID . $> $EUID $EFFECTIVE_USER_ID UID . $( $GID $REAL_GROUP_ID GID . $) $EGID $EFFECTIVE_GROUP_ID GID . $O $PROGRAM_NAME . ps, . $[ . - 0. $] $PERL_VERSION . perl -v . / 1000. $^D $DEBUGGING '-D'. $^F $SYSTEM_FD_MAX (system file descriptor). 2. $^I $INPLACE_EDIT inplace-edit . undef. $^P $PERLDB . . $^T $BASETIME 1970 . $^W $WARNING '-w'. true - false - . $^X $EXECUTABLE_NAME . argv[0] . $ARGV '<>'. @ARGV