Perl: Input and output
Last Update:2015-04-23
Source: Internet
Author: User
Input and output:
<STDIN> operator: used to read data from STDIN:
If you read the input character at the end of the file, it will return undef.
$ line = <STDIN>; #Read the next line.
chomp ($ line = <STDIN>); #Remove the last line break of the input line
while (<STDIN>) {
print "$ _";
}
foreach (<STDIN>) {
print "$ _";
}
Print function: used to output information to STDOUT.
$ _ = "Str1 str2…";
print; #print will print the value of $ _ by default.
print @arr; #Print array elements, but no spaces in between.
print "@arr"; #Print array elements with spaces in between.
printf function: formatted output:
%%: percent sign
% c: character
% u: decimal
% o: octal
% x: hexadecimal
% s: string
% d: decimal integer, round off the integer directly
% g: Floating point number, automatically select scientific notation or decimal.
% e: floating point number, scientific notation
% f: floating point number, decimal
% m.nf: m is the total number of digits, n is the number of digits after the decimal point
space: Add a space before the non-negative number.
+: Add a plus sign in front of non-negative numbers, the default is not.
-: Left aligned, the default is right aligned.
0: Right-aligned with 0 to fill, the default is a space.
#: Enable the prefix, 0b / 0B means binary, 0x / 0X means hexadecimal, and 0 means octal.
printf (“% s \ n” x @arr), @arr; #print array
say: output
say will automatically add a newline character at the end of each line.
The other syntax of say is the same as print.
<> Diamond operator:
Read data from file or standard input
perl program file1 file2…
while (<>) {
chomp;
print "$ _";
}
print <>; # Similar to cat command.
print sort <>; # Similar to the sort command.
@ARGV array parameters:
Perl's built-in array @ARGV is used to store parameters, you can also specify this array in the program.
@ARGV = qw # file1 file2 –options file3 #;
select function: select output
After using select, the default output to STDOUT is output to the specified output handle.
select FH; # This is the default before the next select and the specified handle
$ | = 1; #Refresh the buffer immediately
if (STDERR / STDOUT / STDIN .., ">> filename") {#equivalent to copying a standard file handle.
…
}
If the standard file handle fails to copy, Perl uses the original standard handle by default.
die function: handle errors
Die will terminate the program and print an error message to stderr.
The $! Special variable indicates a readable system error message.
if (! open FH, ">> filename") {
die "Can not create file description: $!"; #Output information about yourself and the system.
}
if (@ARGV <2) {
die "not enough arguments \ n"; #Adding a line break will not output a specific error line number.
}
warn function: process warnings and print warning messages to stderr.
Close the handle:
If you reopen a handle perl will close the previous handle;
Perl will automatically close the handle when the program ends.
close FH;
read
readline
write
tell
truncate
binmode
dbmclose
dbmopen
eof
fileno
flock
format
getc
seek
sysopen
syscall
sysread
sysseek
syswrite
perl: input and output