Learning Perl notes

Source: Internet
Author: User
Tags perl interpreter

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: minute t 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 wocould 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. UseScalarFunction.
  • <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 keywordSubDefine 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 completed MED 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 variableIn subrouines. Private variables called lexical variables at any time withMyOperator. 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 shocould process
    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 isStdin, stdout, stderr, argv, Data, argvout. Perl already uses for its own purposes.
  • Filehandles name. Before Perl 5.6, all filehandle names wereBarewords, 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 ";
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.