All numbers have the same format internally in Prel. in fact the Perl use double-precision floating-point value to store numbers.
Creating charaters by code point: chr(),and ord() metheds.
e.g.
$alef = chr(0x05df) ;$code_point = ord('d') ;
In Perl you may write func() wit or without the parentheses. This is a general rule in Perl: except in cases whtere is changes the meaning to remove them,parentheses are always optional.
Undef is a good thing.
Array & List:
Scalar and List Context:
Expressing in Perl always return the appopriate value for their context. In other words,a same thing in defferent context that it's meaning is defferent. Using List-Producing Expressions in Scalar Context.@people in a List context,it gives the List of elemetns. But in a scalar context,it returns the nunber of elements in the array.sortalways returns undef.reverse returns a reversed string.Using Scalar-producing expresions in List Context.always get a List. e.g. @william = undef; # Gets the one-element List (undef) @betty = (); #A correct way to emtyp an array. Forcing Scalar Context in List Context. use scalar function. <STDIN> in List Context: returns all of the remaining lines up to the end-of-file. each line as a separate element of the List. use EOF finishing the input. in linux is Ctrl+D,in wionDWs is Ctrl+Z.
Subroutines
The use strict pragma. impose a little discipline. The return operator. returns a value from a subroutine immediately. state variales. Persistent Private Variables pretty like static in C,but state is a priavet variable. you can make any varibale(scalar and List) type a state variable; but you can't initalize a state variable in List contexts. e.g. state @array = qw(a b c); # error
input and Output
input to Standard input. STDIN this is standard input filehandle. the <filehandle> of <STDIN> is line_input operator and gives you the next line with \n. - the hyphen. If you use a hyphen as one of the argumets pass in perl,that the hyphen means standard input as well. If you just only a hyphen argument you can omiss it. because if no invocation arguments,the program should process the standard input stream. The @ARGV array. This is a special array that is preset by Perl interpreter as the List of the invocation arguments. In other words,It store command arguments. but is can bu changed.<> the Diamond Operator. read line data from the @ARGV array. It also is line_input operator.
while (<>) { chomp ;...}
Formatted Output with printf. e.g.printf "Hello,%s your age is %d.\n","Tom",18 ; the % sign is called conversions. print a number in what's generally a good way,use %g.decimal use %d,foalt use %f,strng use %s. Arrays and print. It is defferent print @arry and print "@arry". the print "@array" has a whilespace speasue two elements but print @array is not. filehandles. a filehandle is the name in Perl program for an I/O connection between your Perl process and the outsIDe world. In fact,It's just the name of connection,not necessarily the name of files. The six special built-in filehandlle name. Its is STDIN,STDOUT,STDERR,ARGV,DATA,ARGVOUT. Perl already uses for its own purposes. filehandles name. Before Perl 5.6,all filehandle names were barewords,and Perl 5.6 added the ability to store a filehandle reference in a normal scalar variable. opening a filehandle. If you use bareword as a filehandle name,the open styanx is open LOG,'[< | > | >>] file' ; or open LOG,' < | > | >>','fle' ; in 5.6 add "three-argument" open. the file can be a scalar. if the file is a scalar you must use "..." around arugments. In default open a file is only read,if you like this invocate open LGO,'file' ; If filehandles in a Scalar. e.g. open my $log_fh,'<','file' ;
Specify an file enCoding. If you use the three-arugments open you can specify an enCoding. e.g. open LOG,'<:enCoding(UTF-8)','file' ; you can get a List of all of the enCodings that perl understands with a Perl command: # perl -MEncode -le "print for Encode->enCodings(':all')" use binmode func tell output Unicode to STDOUT. e.g. binmode STDOUT,':enCoding(UTF-8)' ; if you don't do this,you might get a warning "WIDe charater in print at test line .."
Use :crlf Specify file line endings. in DOS the file each line ends with CR-LF pair.(also as "\r\n"). Unix line endings only use the LF. The :crlf enCoding can translate a newline to \r\n. e.g. open LOG,'<:crlf','file' ; this is open a only read DOS file Prel can translate crfl to newline. e.g. open LOG,'>:crlf','file'; this is open a write file,write this file each lIEn endings with crlf. (from newlne to crlf). Closeing a filehandle. close operator close it. e.g. close LOG ; Use dIE Show Fatal errors. e.g. dIE "Has a error: $!\n" ; the $! stroe a human-readable complaint error mssage form system. and it well exit the program immIDealy. Use warn show a Warning message. the warn show a message to STDERR,but don't exit the program. automaticlly dIE-ing. Starting with Perl 5.10,the autodIE pragma is part of the Standard libaray. So you can use autodIE ; with open LOG,'>>','file' ; don't write dIE message. Changing the Default STD flehandle. There are some ways to change the Default STD filehandle. one is you cat useingselect operator chanages default output filehandle. e.g select LOG ; select return currently output filehanlds. the two way is reopen a Standard filehandle e.g. open STDERR,">>","error.log" ; 总结
以上是内存溢出为你收集整理的Learning Perl 笔记全部内容,希望文章能够帮你解决Learning Perl 笔记所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)