徐小玉的博客。
分类:
2010-06-23 11:25:57
1: the print function
1.1 print函数用单引号\n \t 一类的符号不会被解释,原样打出来
如果串用的是单引号,会原样打印出来:
$now = localtime();
print 'Today is $now, $name.';
(output) Today is $now, $name.
String Literals | |
Escape Sequences |
Descriptions (ASCII Name) |
\t |
Tab |
\n |
Newline |
\r |
Carriage return |
\f |
Form feed |
\b |
Backspace |
\a |
Alarm/bell |
\e |
Escape |
\033 |
Octal character |
\xff |
Hexadecimal character |
\c[ |
Control character |
\l |
Next character is converted to lowercase |
\u |
Next character is converted to uppercase |
\L |
Next characters are converted to lowercase until \E is found |
\U |
Next characters are converted to uppercase until \E is found |
\Q |
Backslash all following nonalphanumeric characters until \E is found |
\E |
Ends upper- or lowercase conversion started with \L or \U |
\\ |
Backslash |
1。2: 几个特殊的符号。预定义的。
__FILE__ __LINE__ 文件名,行号:
print "This string contains \t\ttwo tabs and a newline.\n" # Double quotes
(Output)
This string contain two stabs and a newline.
这些特殊的定义还有:
Special Literals | |
Literal |
Description |
_ _LINE_ _ |
Represents the current line number |
_ _FILE_ _ |
Represents the current filename |
_ _END_ _ |
Represents the logical end of the script; trailing garbage is ignored |
_ _DATA_ _ |
Represents a special filehandle |
_ _PACKAGE_ _ |
Represents the current package; default package is main |
:在单独的一行上写__END__ 代表程序脚本的逻辑结束,之后的任何命令都会被忽略。
1 print "We are on line number ", _ _LINE_ _, ".\n"; 2 print "The name of this file is ",_ _FILE_ _,".\n"; 3 _ _END_ _ |
4 print ; 5 __DATA__ #这里用 __END__也可以。 6 This is a test. 7 I hope it will be printed. (output): This is a test. I hope it will be printed. |
练习:
1:Use the print function to output the following string:
"Ouch," cried Mrs. O'Neil, "You musn't do that Mr. O'Neil!"
print '"Ouch,"cried Mrs.O\'Neil,"Yout must\'t do that Mr.O\'Neil!"',"\n"; print "*************************************\n"; print "\"Ouch,\"cried Mrs\.O\'Neil,\"Yout must\'t do that Mr\.O\'Neil!\"\n"; |
2:Use the printf function to print the number $34.6666666 as $34.67.
printf " The \$34.66666666 is %8.2f\n", 34.6666666; (output) The $34.66666666 is 34.67 |
3:
#! /usr/bin/perl my $mei_time=localtime(); print "Today is $mei_time (use localtime())\n"; print "The name os this PERL SCRIPT is",__FILE__,"\n"; print "Hello. The number we will examine is 125.5.\n"; print "The Number in decimal is 125.\n"; print "The following number is taking up 20 spaces and is right justifies.\n"; printf " |%10d|\n",125; print "My boss just siad,\"Can't you load me \$12.50 for my lunch?\n I flarly said, \"No Way!\" \n Good-bye \a \n"; ###### FOR 4 print ; __DATA__; Life is goos with Perl. I have just completed my second exercise! |