There is only one line of Perl program page 1/3

Source: Internet
Author: User
Tags perl script

1 line Perl
Speaking of Perl today, there is only one line of Perl Program .
Various Programming Language Perl is famous for its abnormal ancient spirit. For example, I often write Perl programs that I don't understand.
For example, although the program in this document is short, it is estimated that I will be dizzy after a week. But this does not affect the existence and promotion of Perl as a powerful system management tool and system control language, and its flexibility is precisely its biggest advantage. You can
In this way, you can also write it like that, "or, you can't do the same thing ."
1. Hello world!
========================================================== ==============================
Or from the most classic Hello, world! Start ......
##
# Perl-e 'print "Hello, world! \ N "'
Hello, world!
##
Unexpectedly, we finally wanted to output "Hello, world! ", Naturally, it's like using echo or
Same as printf.
This program is expanded in the traditional form as follows:
=
#! /Usr/bin/perl
Print "Hello, world! \ N ";
=
We use the perl-e parameter on the command line, which will enable Perl to automatically build the string followed by-E as a Perl program.
The execution removes the hassle of rewriting files with VI or cat.
The function of the-e parameter is to automatically execute the subsequent string as the program topic, and then use the subsequent parameter as the command line of the Perl script.
Parameters ($ argc and @ argv) are passed to program statements.
2. I want Cat.
========================================================== ==============================
Hello, world! It seems completely useless. In fact, we can enrich it and use it to complete any work-as long as your imagination is rich enough.
One of the most famous projects in the Perl world is power Perl tools, which re-implements all Common Unix commands with pure Perl.
Let's look at a simple cat.
##
# Perl-PE 1/etc/hosts
127.0.0.1 localhost. localdomain localhost
192.168.0.3 vi
##
Like cat, the command output the/etc/hosts file as is. Let's take a look at what it actually does:
=
#! /Usr/bin/perl
For (@ argv ){
Open ($ F, $ _);
While (<> ){
& Call the command specified by the-e Parameter
& 1 in this example
Return 1;
}
}
=
Here we will explain the second Perl Command Line Parameter-P. The-p process automatically provides us with Source code
Layer 1 reads the specified file name on the command line in sequence and acts as @ argv to open and process the file;
The second loop receives the file handle from open ($ argv [*]) and executes a perl <> operation to read each row of the file at a time,
The processing process is to use the program statement specified by-e. If no Program Statement is specified
It will read and display each row.
The procedure is as follows:
1. cyclically, read several files specified in @ argv in sequence;
2. Open the current file;
3. cyclically, read each row using the <> operator in sequence and store it in the temporary variable $ _;
4. Call the-e Program Statement for processing. In this example,-E has only one parameter, which represents retuen 1 in Perl and returns the true value directly.
Not processed.
Write two more useful commands:
# Perl-PE 'exit if $.> 10' filename
This is equivalent to the Unix Command head, which prints the first 10 lines of the specified file-you can change 10 to any number you want;
# Perl-e '@ lines = <>; print @ lines [$ # lines-10 .. $ # Lines] 'filename'
This is equivalent to the Unix Command tail, printing the last 10 lines of the specified file-you can also change it to what you want.
3. Kick sed.

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.