Perl Self-study notes finishing

Source: Internet
Author: User

Input and output

1. Read standard input (keyboard input is called standard input)
$ line =; #Read the next line
chomp ($ line); #Truncate the last newline

chomp ($ line =); #Common usage, same effect as above
If the end-of-file is read, the "line input" operator returns undef. This design is for use with loops and can naturally jump out of loops:
while (defined ($ line =)) {
    print "I saw $ line";
}
while () {
    print "I saw $ _";
}
foreach () {
    print "I saw $ _";
}
Note: The while loop is different from the foreach loop. During the while loop, perl reads a line of input, stores it in a variable, and executes the body of the loop. Next, it will go back and look for other input lines. But in a foreach loop, the "row input" operator is executed in the context of the list (because foreach needs to process the contents of the list item by item). To do this, the loop must read all its inputs before the loop can begin executing. If you enter a log file from a WEB server with a size of 4000M, their differences will be very obvious! Therefore, the best practice is to use the shorthand of while loop as much as possible, and let it process one line at a time.

2.Diamond operator input
One way to read input in perl is to use the diamond operator <>.
E.g:
[[email protected] aaa] # cat aaa.txt
aaa
bbb
ccc
[[email protected] aaa] # cat test2.pl
#! / usr / bin / perl
# test2
while (<>) {
chomp; # Without parameters, chomp will directly act on $ _. Save keys and start small.
print "I was $ _ that I saw! \ n";
}
[[email protected] aaa] # ./test2.pl aaa.txt
I was aaa that I saw!
I was bbb that I saw!
I was ccc that I saw!

Note: If the diamond operator cannot open a file and read the content, the relevant error diagnostic information will be displayed, like:
Can't open wilma: ca n’t open wimal: No such file or directory
The diamond operator then automatically jumps to the next file, just like cat or other standard utility programs do.

3. Diamond operator call parameters

The diamond operator doesn't actually go back and check the call parameters. Its parameters are actually from the @ARGV array, which is a special array built into Perl's implementation.
The diamond operator looks at the array @ARGV to decide which file names to use. If it finds an empty list, it uses standard input stream; otherwise, it uses the file list in @ARGV.
E.g:
[[email protected] aaa] # cat 1.txt
nihao
beijing
[[email protected] aaa] # cat 2.txt
nihao
shanghai
[[email protected] aaa] # cat 3.txt
nihao
guangzhou
[[email protected] aaa] # cat test2.pl
#! / usr / bin / perl
# test2
@ARGV = qw # 1.txt 2.txt 3.txt #; #Force the diamond operator to read these three files.
while (<>) {
chomp;
print "I was $ _ that I saw! \ n";
}
[[email protected] aaa] # perl test2.pl
I was nihao that I saw!
I was beijing that I saw!
I was nihao that I saw!
I was shanghai that I saw!
I was nihao that I saw!
I was guangzhou that I saw!

4.Standard output
print <>; #Similar to cat under Unix
print sort <>; #Similar to the sort command under Unix.
print (2 + 3); #It will print 5.

5.Format print output using printf

[[email protected] ~] # cat test.pl
#! / usr / bin / perl
#test
$ user = zhangsan;
$ days_to_die = 30;
printf "Hello,% s; your password expires in% d days! \ n", $ user, $ days_to_die; #% s specifies the string format,% d refers to the number format
[[email protected] ~] # perl test.pl
Hello, zhangsan; your password expires in 30 days!

[[email protected] ~] # cat test.pl
#! / usr / bin / perl
#test
$ user = zhangsan;
$ days_to_die = 30;
printf "Hello,% s; your password expires in% d days! \ n", $ user, $ days_to_die;
printf "% g% g% g \ n", 5/2, 51/17, 51 ** 17;
printf "in% d days! \ n", 17.85;
printf "% 6d \ n", 42;
printf "% 2d \ n", 2e3 + 1.95;
printf "% 10s \ n", "wilma";
printf "% -10s \ n", "wilma";
printf "% 12f \ n", 6 * 7 + 2/3;
printf "% 12.3f \ n", 6 * 7 + 2/3;
printf "% 12.0f \ n", 6 * 7 + 2/3;
printf "Monthly interest rate:% .2f %% \ n", 5.25 / 12;
[[email protected] ~] # perl test.pl
Hello, zhangsan; your password expires in 30 days!
2.5 3 1.0683e + 29
in 17 days!
    42
2001
     wilma
wilma
   42.666667
      42.667
          43
Monthly interest rate: 0.44%

6.File handle
There are six special file handles reserved by Perl. Their names are STDIN, STDOUT, STDERR, DATA, ARGV and ARGVOUT. Although you can choose any file handle name you like, you should not use reserved names unless you really need to use the above six handles in a special way.

7, open the file handle
open CONFIG, "open BEDROCK,"> fred ";
open LOG, ">> logfile";

my $ selected_output = "my_output";
open LOG, "> $ selected_output";
In the relatively new Perl (after version 5.6), open has another way of writing with three parameters:
open CONFIG, "<", "dino";
open BEDROCK, ">", $ file_name;
open LOG, ">>", & logfile_name ();

8.Close the file handle
close LOG;
close CONFIG;
close BEDROCK;

9.Using file handles
print LOG "Welcome to Beijing! \ n";
printf STDERR "% s. \ n", aasdf;

10.Change the default file output handle
select BEDROCK;
print "I hope Mr. Slate does n’t find out about this. \ n";
print "Wilma! n";
By default, if you do not specify a file handle for print (printf), its output is sent to STDOUT. However you can use the select operator to change the default file handle.
$ I = 1, if you want to change back to the original STDOUT, you can use it
Source of this article: http://www.benet.wang/perl%E7%BC%96%E7%A8%8B/89.html ";

This article comes from "Jianghu Xiaoxiaosheng" blog, please keep this source http://xuexuhui.blog.51cto.com/9647696/1663767

perl self-study notes


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.