Perl Learning note Four--input and output

Source: Internet
Author: User

Reading standard input

Standard input with <STDIN>: Chomp ($line =<stdin>);

If the end of the file is read, the line input operator returns UNDEF. Can use this nature to jump out of the loop.

while (defined ($line =<stdin>)) {

Print "I saw $line";

}

Abbreviated as:

while (<STDIN>) {

Print "I saw $_.";

}

Note: This shorthand works only in the earliest notation, and if you place the line input operator anywhere else (especially in a row), he does not read the line input and automatically saves the default variable $_. This shorthand works only if the line input operator is in the conditional expression in the while loop.

Also, foreach has this usage:

foreach (<STDIN>) {

Print "I saw $_";

}

The difference is that while a row is processed every time a row is read, foreach needs to be read and processed once.

#!/usr/bin/perl UseStrict; Usewarnings; while(<STDIN>) {    Print "I saw $_ \ n";}foreach(<STDIN>) {    Print "I saw $_ \ n";}

Input from the diamond operator (<>)

You can use the diamond operator to write tools such as cat, sed, awk, sort, grep, LPR, and so on.

Program Invocation parameters: $./my_program Fred Barney Betty

The program collects data from the standard input stream if it is more than providing any invocation parameters. Also: If you use a hyphen (-) as a parameter, it means that you want to read the data from the standard input. So if the parameter fred-betty is called, then the program should process the file Fred and then process the data provided by the standard input stream, and finally Betty.

The diamond operator is a special case of the line input operator. But instead of reading the input from the keyboard, he reads from the user-specified location:

while (defined ($line =<>)) {

Chomp ($line);

Print "It is $line that I saw!\n";

}

If the call argument is now Fred, Barney, Betty, then the output will be "It was [a line read from the file] lines that I saw!" Wait until the end of the file is encountered. Note:<> reads a file and then reads the new file, then outputs the content in line. As with the diamond operator, it is reported that these files are merged into a large file, and the diamond operator returns undef and jumps out of the while loop only when it touches the end of all inputs.

The above code shorthand:

while (<>) {

Chomp

Print "It is $_ that I saw!\n";

}

Note: Diamond operators generally handle all inputs, so it is usually wrong to see multiple diamond operators in a program.

# !/usr/bin/perl  while (<>) {    chomp;     Print " $_ \ n " ;}
Note that the following execution instructions in the code are added-and read in from the output pipeline of grep

Perl Learning note Four--input and output

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.