Helloworld of Perl

Source: Internet
Author: User
Tags array definition perl interpreter

Perl tutorial

Http://www.cbi.pku.edu.cn/chinese/documents/perl/index.htm

Getting started with Perl

Http://developer.51cto.com/art/201009/224638.htm

Learning Perl 1 helloworld
**************************************** ******
Brief Introduction:
Perl is a scripting language. It pursues simplicity and solves a general problem. It can be completed with several lines of code. A slightly more complex problem will not cause code to exceed a screen! It is very powerful and has many advantages.

Disadvantages: too flexible (advantages and disadvantages), excessive redundant syntax, the code of many Perl programs is hard to read, and the code length of programs that implement the same function can be 10 times or a hundred times different. However, Perl can also write code as elegantly as Python or Ruby.

To enable Perl, you need a Perl interpreter. On any computer with Unix installed, Perl interpreter is installed in 99.99%. On a Windows or Mac computer, you need to download the latest language version and install it on the computer. You can easily download Perl from the Internet, and it is free of charge. In Linux, Perl is installed with most UNIX-like systems (including Mac OS X). You can enter the command Perl-V on the command line terminal to view the version, two versions of Windows are available: Strawberry Perl and ActivePerl.
**************************************** ******
Write the first helloword:
Create a new text file hello. pl and write a line of statements in it:
Print "Hello world! ";
Then run Perl hello. pl on the command line.
For programming in UNIX, it is more convenient:
Create a new text file hello. pl and write it in it:
#! /Usr/local/bin/perl
Print "Hello world! ";
Then execute hello. pl and then OK.
Preliminary understanding:
Escape characters \ t \ n .. are the same as C and Java
# Line feed after printing
Print "Hello world \ n ";
Comment line starting #

The characters in hellonworld \ n' are printed as they are.
Print 'hellonworld \ n ';

**************************************** ******
Fragmented knowledge

A pair of quotation marks indicates that the characters in the quotation marks should be recognized as an operating system command, which is executed together with the entered command.
Print 'COMMAND/C dir ';

Variable definition, starting with $
$ Num = 1;

Array definition, starting @
@ Array = (1, 2, 3 );
The array element calls $ array [Index], where index indicates the array subscript.
The value of $ array [0] is 1.

Hash definition, starting with %
% Hash = ("A", 1, "B", 2 );
Hash call % hash, where keys indicates the key value, expressed by strings. Note that the hash key must be unique, but the value can be unique, for this reason, the hash key is often used for unique processing. In the example above, "A", "B", values is the value corresponding to keys, for example, 1, 2. The value of $ hash {"B"} is 2.

= Can indicate whether two variables or values are equal <=! ==<> Same as C
Print "input a number (number between {1, 0 }):";
# GETC reads a character or uses read (stdin, $ I, 1). 1 In the third parameter is the length of the input data to be read.
$ X = GETC (stdin );
Print "\ Nyou have input a number:", $ X;
If ($ x> 1 | $ x <0 ){
Print "\ nerror ";
}

Data Type: integer floating-point string

Integer, which is basically the same as other languages. Perl actually stores integers in floating-point registers, so it is actually treated as a floating-point number. In most computers, floating-point registers can store about 16 digits, which are discarded. An integer is a special case of a floating point number.
$ A = 123456789123456789123456789;
Print $;
Print 1.23456789123457e + 026

In hexadecimal notation, the headers are 0, and in hexadecimal notation, the headers are 0x.
$ Var1 = 047; (39 in decimal format)
$ Var2 = 0x1f; (31 in decimal format)

Floating point numbers such as 11.4,-0.3,. 3, 3., 54.1e + 02, and 5.41e03
Floating-point registers usually do not store floating-point numbers accurately, resulting in errors. Pay special attention to them in operations and comparisons. The index ranges from-309 to + 308.

Programmers who are familiar with C strings should note that in Perl, the end of the string does not contain any implicit null character. The null character can appear anywhere in the string.
There are two differences between a single quotation mark string and a double quotation mark string. One is that there is no variable replacement function, and the other is that the backslash does not support escape characters, but only takes effect when it contains single quotation marks and backslash. Another feature of single quotes is that it can span multiple rows, for example:
$ Text = 'this is two
Lines of text
';
It is equivalent to the following sentence:
$ Text = "this is two \ nlines of text \ n ";
. Mutual conversion between string and Value
Example 1:
$ String = "43 ";
$ Number = 28;
$ Result = $ string + $ number; # $ result = 71
If a string contains non-numeric characters, it starts from the left to the first non-numeric character, for example:
$ Result = "hello" * 5; # $ result = 0
$ Result = "12a34" + 1; # $ result = 13

. Initial variable value
In Perl, all simple variables have the default initial value: "", that is, null characters. However, it is recommended to assign initial values to all variables. Otherwise, when the program becomes large and complex, unexpected and difficult debugging errors may easily occur. Copy search

Copy search
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.