Perl Byte Tutorial Collection of Perl Tutorials

1. Perl environment configuration

1. Get perl
  Perl is usually located in /usr/local/bin/perl or /usr/bin/perl. You can get it for free using anonymous FTP on the Internet, such as ftp://prep.ai.mit.edu/pub/gnu/perl-5.004.tar.gz

2. The installation process is:
      (1) Unzip:
      $gunzip perl-5.004.tar.gz
      $tar xvf - <perl-5.004.tar.gz
      (2) Compile:
      $make makefile
      (3) Place:
      compile the generated Copy the executable file to the directory where the executable file usually resides, such as:
      $copy <compiled excutable file> /usr/local/bin/perl

3. Run
  Edit your Perl program with a text editor, add the executable attribute: $chmod +x <program> to execute: $./<program>. If the system prompts: "/usr/local/bin/perl not found", it means that you did not install successfully, please reinstall.
Note: The first line of your program must be #!/usr/local/bin/perl (where perl is located).

4 Comments:
      The way to comment is to use the character # at the beginning of the statement, such as:
      # this line is a comment
      Note: It is recommended to use comments frequently to make your program easy to read, which is a good programming practice.

Second, constants, variables and other issues

1. Single quotes and double quotes
  Simple variable substitution is supported in strings inside double quotes For example: $text = "This text contains the number $number."; escape
  characters are supported in strings inside double
  quotes There are two differences between double-quoted strings. One is that there is no variable substitution function, and the other is that backslash does not support escape characters.
2. Repetition and connection
Repeat: print "t" x 5 (5 ts will be output, note: where x is a lowercase x in English letters)
Connection: $a.="bc" (equivalent to even equal)
3. Simple variables, Array, list
Simple variable: declare with $, such as: $a="hello";
array: declare with @, such as: @arr=('a','b','c');
list: the list is contained in A sequence of values ​​in brackets, which can be any value or empty, such as: (1, 5.3 , "hello" , 2), an empty list: ()

3. File operation

1. Open the file: such as open (MYFILE, "file1") || die ("Could not open file"); MYFILE is the declared file handle, file1 is the file name/file path, the whole line of code means: if the opening fails , then output "Could not open file";
   close the file: when the file operation is completed, use close(MYFILE); to close the file.
2. Read the file
  Statement $line = <MYFILE>; Read a line of data from the file and store it in the simple variable $line and move the file pointer backward by one line. <STDIN> is a standard input file, which is usually input by keyboard and does not need to be opened.
  The statement @array = <MYFILE>; reads the entire content of the file into the array @array, and each line of the file (including the carriage return) is an element of @array.

1

2

3

4

#!/usr/bin/perl

open(MYFILE,'1.txt');

@arr = <MYFILE>;

print @arr;

3. The form of writing a file
  is:
     open(OUTFILE, ">outfile");
     print OUTFILE ("Here is an output line.\n");
  Note: STDOUT, STDERR are standard output and standard error files, usually the screen, and No need to open.
4. Judging the file status

1. The file test operator syntax
  is: -op expr, such as:
    if (-e "/path/file1") {     print STDERR ("File file1 exists.\n");     }

file test operator

operator describe
-b Is it a block device
-c Is it a character device
-d Is it a directory
-e does it exist
-f Is it an ordinary file
-g Is the setgid bit set
-k Whether the sticky bit is set
-l Is it a symbolic link
-o Do you own the file
-p Is it a pipeline
-r Is it readable
-s Is it non-empty
-t Is it a terminal
-u Whether the setuid bit is set
-w Is it writable
-x Is it executable
-z Is it an empty file
-A how long since last visit
-B Is it a binary file
-C How long since the inode of the file was last accessed
-M How long ago was last modified
-O Is it only owned by "real users"
-R Is it only readable by "real users"
-S Is it socket
-T Is it a text file
-W Whether only "real users" can write
-X Whether only "real users" can execute
Note: "Real user" refers to the userid specified when logging in, which is opposite to the current process user ID. The command suid can change the effective user ID.

 例:
    unless (open(INFILE, "infile")) {
    die ("Input file infile cannot be opened.\n");
    }
    if (-e "outfile") {
    die ("Output file outfile already exists.\n");
    }
    unless (open(OUTFILE, ">outfile")) {
    die ("Output file outfile cannot be opened.\n");
    }
  等价于
    open(INFILE, "infile") && !(-e "outfile") &&
    open(OUTFILE, ">outfile") || die("Cannot open files\n");


四、模式匹配:

1.概念:模式指在字符串中寻找的特定序列的字符,由反斜线包含:/def/即模式def。其用法如结合函数split将字符串用某模式分成多个单词:@array = split(/ /, $line);
2.匹配操作符 =~、!~
   =~检验匹配是否成功:$result = $var =~ /abc/;若在该字符串中找到了该模式,则返回非零值,即true,不匹配则返回0,即false。!~则相反。

五、控制结构
(1)、条件判断:if()elseif()else();
(2)、循环:
  1、while循环
  2、until循环
  3、for循环
  4、针对列表(数组)每个元素的foreach循环

1

2

3

4

5

open(MYFILE,'1.txt');

@arr = <MYFILE>;

foreach $str (@arr){

 print $str;

}

  5、do循环
  6、循环控制:退出循环为last,与C中的break作用相同;执行下一个循环为next,与C中的continue作用相同;PERL特有的一个命令是redo,其含义是重复此次循环,即循环变量不变,回到循环起始点,但要注意,redo命令在do循环中不起作用。
  7、传统的goto语句:goto label;
(3)、单行条件
  语法为statement keyword condexpr。其中keyword可为if、unless、while或until,如:
    print ("This is zero.\n") if ($var == 0);
    print ("This is zero.\n") unless ($var != 0);
    print ("Not zero yet.\n") while ($var-- > 0);
    print ("Not zero yet.\n") until ($var-- == 0);
    虽然条件判断写在后面,但却是先执行的。

六、子程序
(1)、定义
  子程序即执行一个特殊任务的一段分离的代码,它可以使减少重复代码且使程序易读。PERL中,子程序可以出现在程序的任何地方。定义方法为:
  sub subroutine{
    statements;
  }
(2)、调用
  调用方法如下:用&调用
       &subname;
       ...
       sub subname{
          ...
       }

Guess you like

Origin blog.csdn.net/jh035512/article/details/128141736
Recommended