The Perl Tutorial: Running a Perl Program (2)

xiaoxiao2021-03-06  53

Running a perl program (running a Perl program)

Note: There will be many details of Perl here, and you will only teach you how to start writing a program with Perl and starting it.

Note: this will not teach you everything about perl but it it will girl you a good idea of ​​how the language works, and how to program in perl.

Running the Perl program is relatively simple. For example, after you write the Perl program (file name is filename.pl), the execution of Perl is executed, like this:

.

Perl filename.pl

If you are running under UNIX systems, you should use the chmod rx filename.pl command to make the filename.pl's permissions, and you should also join: in your Perl program:

If you are running on a unix system, you can make the file executable sale the chmod rx function call. In Order to do this, you must have the folload line as the first line in your perl program.

#! / usr / local / bin / perl

"USR / local / bin / perl" is the path to the Perl interpreter, and you can type Which Perl in the command line state:

WHERE "/ usr / local / bin / perl" is the path of Your Perl Interpreter. In ORDER TO FIND THIS PATH ON UNIX JUST TYPE:

Which perl

On the command line.

Let us try to write a small program, tapping the following code in any text editing:

Let's Practice Writing a Small Program Atur Console. Using Any Editor Type The Following Code:

#! / usr / local / bin / perl

Print "Hello World! // N /";

Saved as an extension as a .pl file, saved here as hello.pl, type: Type:

Now Save Your Program with a .pl Extension. Let's call it hello.pl. From the command line the Then Type:

Perl Hello.pl

You should be able to see the output below:

You Should Then See The Following Output:

Hello World!

Literals (Word Value)

The literal value of Perl is not changed after the performance (the translation: many dynamic languages, such as JavaScript, Python, PHP, etc.), that is, the data type is determined according to the specific performance content), and the literal value is considered further conversion with hard coding. program. The literal values ​​supported by Perl have the following: Perl Litrals Are Values ​​That Are RepreSented As-Is. The value is considered to be hard-code INTO THE PROGRAM. Perl Supports Litrals of the Following Two Types:

• Numbers (value)

• Strings (string)

Numerical Field Value is numerical, the string string value is used for name, text or message, usually enclosed with a pair of single quotes, which will mean that some variable names (below will be described) appearing in a pair of single quotes inside When the variable is not replaced with the corresponding actual value. The string field value also supports dual quotes, and the variable name inside the pair of dual quotes will be replaced by its corresponding actual value. The string field values ​​in the reverse single quotes are also supported, which will have the execution system command function (you don't have to consider which operating system runs this command).

Translation: The string of reverse single quotes has the function of executing the system command, look at the following Perl program:

PRINT `DIR * .TXT`; files in all .txt suffix names in the current directory will be listed and displayed. Numeric literals represent numbers. Strings literals are used to identify names, text, or messages that are displayed in the program. String literals are usually enclosed in single quotes, which means that any variable names (more on variables below) appearing in them are not interpolated. Double quoted string literals are also supported, and they support what is called variable interpolation, meaning that variable names are substituted for their real values. Single backquoted string literals are also supported. These normally allow you to run command line options and return the Output to your program.

Perl supports adding a comment statement, the comment statement is indicated by a # character, and the statement is to comment statements after the # character until the end of the line.

Perl Also Supports Comments in Your Code. A Perl Comment IS Signified by A # Sign. When You See A # Sign, IT Means That Everything After That Is A Comment Up to the end of the line.

Variables (variable)

All program languages ​​support variables. The variable is similar to the string string value, except that they have some special values. Perl supports three variable settings:

All Programming Languages ​​Support Variables. Variables Are Like String Litrals, Except That Thei Hold A Specific Value Or Values. Perl Supports Three Sets of Variables:

• The scalar scalar variable only saves a value, which can be a value string value, or a string string value. The scalar variable name is always started with a $ character.

• Scalars The Scalar Variables Hold Only ONE Value, Which May Be a Numeric Litral, OR A String Litral. Scalar Variable Names Always Begin with a $ sign.

• A array variable can save multiple values. The value to be saved can be a value or string, each having its unique subscript, and stores in the array variables in order. The array variable name starts with a @ character.

• Arrays An array variable can hold a list, or an array of values. The values ​​can be numbers or strings. A key or subscript value is automatically assigned, and the values ​​are kept in the order entered. Array variable names start with an @ SIGN.

• The associated array association array is the same as hash, and they are all similar to arrays, but they are different, and the programmer usually forms hash with keywords and values. Hash does not guarantee the orderly storage of the preserved value, and the hash variable name is always expressed in% symbol.

• Associative Arrays Associative arrays or hashes as they're also known, are similar to arrays. But the major difference is that you, the programmer, assign the key and value to the hash, while an array automatically assigns a key. Hashes do not .

The above mentioned scalar values ​​are often used to save a numeric message, which is often used in Perl, so you must be familiar with and use it.

As mentioned above, scalar values ​​are used to track a single pieces of information. Perl's most commonly used variable is $ _. This special variable name is called the default variable for Perl's many functions, so become very familiar with it.

The following example assigns a scalar value:

We can see a value being assigned to a scalar value in the folowing example along with interpolation.

$ VAR = "My Scalar Variable";

Print "this is $ var";

The last output of the above code: this is my scalar variablethe output of the Above code Would Be: this is my scalar Variable

The array holds multiple variable values ​​and can be processed in a variety of different methods. You can assign a value when you initialize an array variable:

Arrays Hold A List of Variables And Are Handled In a Variety of Different Ways. You can Give the VALUES TO The Array During Initialization:

@Array = ('One', 'Two', 'Three');

The above array has been assigned three values, and each numerical is separated by commas. The first subscript of the array is always 0, of course, you can also use $ [access and change it, in general, you should not do this, because this will make others feel confused when reading your code. solution. In order to read some of the value, you can access the way the number of group names and subscripts, for example, you want to access the second value:

The above array assigns three values ​​into the array. The values ​​are separated by commas. The first subscript of an array is always 0, unless you change it with special variable $ [. This is generally not good practice, because you can confuse people reading Your Code. in Order to Read The Value, you'll:

Print $ array [1];

WE SAY $ Array Because We are Referring to Only ONE VALUE, FOLLOWED by An Open Bracket, The Subscript That We Want, And The Close Bracket. This Would Return The Second Value In Your Array.

Hash is something like arrays, look at the following:

Hashes Can Be Initialized in a Similar Manner to Arrays.

% hash = ('key1', 'value1', 'key2', 'value2', 'key3', 'value3');

Hash is placed in the order of the key-value. The first value is the first key; the second value is a value associated with the first key; the third value is the second key; the fourth value is a value associated with the second key. The way to get the value from the hash is similar to the array. In addition to the [] (square bracket) and the subscriber access to a value, we can also use {} (braces) and keys to get values. For example, if we want to get 'Value 2', we should use the following way:

The major difference is that you have to place the values ​​in a key-value order So the first value you type would be the first key;. The second, the associated value; the third, the second key; and the fourth, the value associated with that key. The process of retrieving the value from a hash, is similar to an array, except that instead of using [] (square brackets) we would use {} (braces), and instead of the subscript number, we would Use the key. if we wented to retrieve 'value2', We Well Use The Following: Print $ hash {'key2'};

It should be known that the variable name in Perl is sensitive to case, $ ME, $ ME, $ ME, $ ME are four different variables. Similarly, if you have a scalar variable $ ME, a group variable @me, a hash% ME, which will distinguish between three different variables and explain.

Be aware That Perl Variables Are Case-Sensitive, So $ ME, $ ME, $ ME AND $ Me Are Four Separate Variables. Similarly, IF You Had A Scalar Variable Called $ Me, An Array Called @me, And A Hash Called% ME, They Would Be Interpreted As Three Separate Variables, Not The Same One.

转载请注明原文地址:https://www.9cbs.com/read-87950.html

New Post(0)