Learning Perl 笔记

Learning Perl 笔记,第1张

概述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. $ale
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:

A List or array may hold numbers,string,undef value,or any mixture of different scalar values. in other words,can hold different type scalar value in List or array. If the subscript indicates an element that would be beyond the ene of the array,the corresponding value will be undef. How to get the last element index in an array? $#array_name is the last element index.  OR -1 beause,negative array indices conut from the end of the array. push,pop and shift,unshift. end & begin. splice @array_name,start,length,replacement ; the third and fourth arguments is not essential. and length allow is zero. foreach: foreach $var (List) {...}. It not same as Java the $var is not a copy of the List element-it  actually is the List element. That is,if you modify the control variable($var) insIDe the loop,you modify the element itself. Deault:$_ revers,sort

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

DeFinition. use the keyword sub define your own subroutine. e.g. sub fuckk {...} Invoking. using the subroutine name that you want invoked with the ampersand&. e.g.&fuckk ; Return Values. all Perl subroutines have a return value--Whatever calcuation is last performed in a subroutine is automatically also the return value. Arguments. Perl has subroutine arguments,to pass an argument List to  the subroutne,after the subroutine invocation. e.g.$n = &fuckk(arg1,arg2...).  Perl automatically stores the paramenter List in the special array variable named @_. The @_ is private to the subroutine. Overwrite. if you define two same-name subroutines,the last will overwrite the first. Private Variable in Subrouines. Private variables called lexical variables at any time with the my operator. e.g. my($n1 $n2) ; by the way,By default,in Perl all variables are global variable. attenionmy has lexical scope. like in C local variable.
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 笔记所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1286767.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存