Read standard input:<stdin> in Perl
foreach (<STDIN>) {print "I saw $_";}
The diamond operator <>: Its parameters are derived from the @argv array.
The @argv array is checked first, and if it is empty, it is entered from the standard input stream. The value of the @ARGV can be from the command line, or you can assign a value yourself.
The value of the./program Fred Barney Betty # @ARGV is three files after the command line.
while (<>) {chomp; Print "It's is $_ that I saw";}
Standard output in Perl: Print. When you output multiple items, you can separate them by adding "," and enclose them in double quotation marks respectively.
When you output an array with print. Print @array; #会输出 all elements in the @array, separated by spaces . Equivalent to array interpolation.
Format the output with printf. %g/%d/%s/printf "The Items are:\n". ("%10s\n" [email protected]), @items; #定义多个%s
Note that the print parenthesis is an explicit function call, which is the list context, which returns the list. Returns a scalar of 0/1 when no parentheses are added.
Print <>; The input is output directly.
File handle: The name that represents the connection between the Perl process and I/O, not the name of a file,
File I/O flow in the shell:./program <dino >wilma #表示从文件dino输入, the output to the Wilma,perl program is still input stdin,
Output to stdout. The rest is handled by the shell.
netstat | ./your_program 2>/tmp/my_errors #对STDERR重定向.
Bare Word (Bareward) file handle: Open CONFIG, ' < Dino '; #打开dino文件作为输入, the operator of the file handle is called CONFIG.
The file handle opened by default in Perl is input. ' > ' output, ' >> ' appended to an already existing file. Note Enclose the file in single quotation marks.
Close CONFIG; #关闭文件句柄
File handle with scalar variable: it is generally defined first with the lexical variable my.
My $rocks _fh;
Open $rocks _fh, ' > ', ' file.txt '; #一般用后缀_fh来表示文件句柄.
In Perl, you can use die and warning to end the current Perl process, and output warning to stderr, respectively.
Die "Cannot create LOG flie:$!" #$! represents the return value when a system call is made. Perl adds the Perl file name and line number to the log by default.
Die "Cannot create LOG flie:$! \ n "#加了换行符后, Perl will no longer include the file name and line number.
When entering using a file handle: Add the handle name directly in the <> brackets.
while (<PASSWD>) {...}
When using a file handle to output: Add a file handle name after print/printf. do not add commas, however , as part of the number of words that require the output string. or directly with
Curly braces {} Enclose the name of the file handle.
print {LOG} "HAHA";
Change the default file handle, using the SELECT keyword. Select LOG; Select STDERR;
Starting with Perl5.10, you can use say instead of print. Say automatically adds line breaks at the end of the output character.
The Camel Spirit in Perl (iii)