加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Learning Perl 笔记

发布时间:2020-12-16 00:33:45 所属栏目:大数据 来源:网络整理
导读:All numbers have the same format internally in Prel. in fact the Perl? use double-precision floating-point value to store numbers. Creating charater s by code point: chr(),and ord() metheds. e.g. $alef = chr(0x05df) ;$code_point = ord('d')

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 "rn"). Unix line endings only use the LF. The :crlf encoding can translate a newline to rn. 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" ;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读