Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the most common programming tasks performed in Perl

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what are the most common programming tasks in Perl, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.

Perl is a very simple, widely used and responsive scripting language. It can be used for a variety of tasks (for example, you can use it to create DOS batch files or the equivalent of C shell scripts), but in a network development environment, it is used to develop CGI scripts.

Because Perl is a scripting language, one of the benefits of using it is that you can distribute the source code of the program. This gives you the opportunity to learn Perl, and you can download and modify thousands of Perl scripts for your own use. One of the drawbacks of Perl is that most free code is difficult to understand. This makes Perl itself a mysterious language type!

This article assumes that you already know how to program (this will be very easy for you if you know the C language). Once you have mastered the basics of Perl, it is very easy to use. In this article, we will first show you how to use Perl to perform the most common programming tasks. After reading this article, you will be able to write your own Perl scripts relatively easily, as well as read ambiguous scripts written by others, which would be a good start.

To enable Perl, you need a Perl interpreter. 99.99% of any computer with UNIX has an Perl interpreter installed. On a computer with Windows or Mac, you need to download the language version of * and install it on your computer. You can easily download Perl from the Internet, and it's free.

Next, be sure to check the DOCS directory that comes with Perl for information similar to the user's manual. If you have loaded Perl, make sure that the path is set correctly to contain the executable for Perl. Then, open a text editor and create a text file. In the file, enter the following line:

Print "Hello Worldwide"

Name the file "test1.pl". At the command prompt, type:

Perl test1.plPerl

The code in the text file will be run and executed. You should see the word "Hello World!" Print to standard output. As you can see, creating and running programs in Perl is very simple. (if you are using UNIX, you can enter a comment on the * * line, such as #! / usr/bin/perl, so you don't have to type the word "perl" on the command line later. )

The print command prints the content to standard output. The symbol n is a newline character. It will be clearer if you modify the test program to the following form (# for a comment):

# Print on two lines print "Hellon Worldwide"

Note that the print command knows that it should interpret "n" as a newline character rather than a literal character, not because the print command is intelligent, but because of the use of double quotes (actually called references in Perl). If you replace double quotation marks with single quotation marks, for example:

Print 'Hellon Worldwide'

The character n will not be interpreted, but will be replaced by text.

There is also the backquote character: `. A pair of backquotes indicates that the characters within the quotation marks should be recognized as an operating system command, and the command is executed together with the command you enter. If you try to enclose the command line operation of the operating system in backquotes, the command is executed. For example, in a Windows NT system, you can use:

Print 'cmd / c dir'

To run the DIR command and view a list of files from the current directory. Variables in the Perl language are very interesting. The Perl variable does not need to be declared, but can be specified with $. For example:

$s = "HellonWorldn"; $t = 'HellonWorldn'; print $s, "n", $t

Or:

$I = 5; $j = $I + 5; print $I, "t", $I + 1, "t", $j; # t = tab

Or:

A = "Hello"; $b = "Worldn"; $c = $a. $b; # note use of. To concat strings print $c

Because. Represents a string concatenation, so it can be estimated that. = has the same meaning as the "+ =" in C language. So, you can write as follows:

$a = "Hello"; $b = "Worldn"; $a. = $bterprint $a

You can also create an array:

@ a = ('cat',' dog', 'eel'); print @ a, "n"; print $# a, "n"; # The value of the highest index, zero basedprint $a [0], "n"; print $a [0], $a [1], $a [2], "n"

The symbol $# means to get the * index in the array, which is equal to the number of the elements in the array minus 1. As in C, the indexes of all arrays are zero-based. You can also create a hash:

% h = ('dog',' bark', 'cat',' meow', 'eel',' zap'); print "The dog says", $h {'dog'}

In this hash, the words' bark' 'are associated with' dog', 'meow' is associated with' cat', and so on. This hash can also be expressed more artistically:

% h = (dog = > 'bark', cat = >' meow', eel = > 'zap')

The operator = > refers to the string on the left, which is equivalent to a comma. You can create a simple for loop statement as you would in C:

For ($I = 0; $I < 10; $iTunes +) {print $I, "n";}

The While statement is simple:

$I = 0; while ($I < 10) {print $I, "n"; $iTunes;}

The If statement is also simple:

For ($I = 0; $I < 10; $iTunes +) {if ($I! = 5) {print $I, "n";}}

The Boolean operator does the same thing as in C:

& & means "and" | | means "or"! It means "not".

For numbers:

= = for "equal"! = for "not equal" = (as originally intended)

Other:

Eq ne lt le gt ge

If you have an array, you can easily loop using the foreach statement:

@ a = ('dog',' cat', 'eel'); foreach $b (@ a) {print $b, "n";}

The Foreach statement takes each element in the @ an array and places that element in $b until the end of the @ a loop. You can create a routine using the word sub. Pass all variables to an array called _ in the routine. Therefore, you need to run the following code:

Show ('cat',' dog', 'eel'); sub show {for ($I = 0; $I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report