1 Line Perl
Perl, for today, has only one line of Perl programs.
Perl is famous in a variety of programming languages, such as I, who often write Perl programs that I can't read.
For example, the program in this document, although short, estimated one weeks later I think it will be dizzy. But this does not affect Perl as a powerful system management tool and system control language existence and promotion, and flexibility is precisely its biggest advantage, you can
Write this, or you can write, "or, the trick is different way." ”
1. Hello world!
===========================================================
Or from the most classic hello,world!. Begin......
##
# perl-e ' print ' hello,world!\n '
hello,world!
##
Not surprisingly, he did not have any differences in the end we are willing to output the "hello,world!", naturally like me to use echo or
printf ().
This procedure is carried out in the traditional form:
==
#!/usr/bin/perl
print "hello,world!\n";
==
We used the-e parameter of Perl on the command line, which would allow Perl to automatically build a Perl program with a string directly following the-E
Expand execution, eliminating the pain of rewriting files with VI or cat.
The function of the-e parameter is to automatically follow the string as a program subject, and then the parameters will be used as the command line for the Perl script
Parameters ($ARGC and @ARGV) are passed to the program statement.
2. I want cat.
===========================================================
Hello,world! seems completely useless, and we can actually enrich it and use it to do whatever it takes--as long as you have plenty of imagination.
One of the most famous projects in the PERL World is the PPT (Power PERL TOOLS), which has been implemented in pure Perl with all the common UNIX commands.
Let's look at one of the simplest cat.
##
# Perl-pe 1/etc/hosts
127.0.0.1 localhost.localdomain localhost
192.168.0.3 VI
##
Like cat, this line of command outputs the/etc/hosts file as it is. Let's take a look at what it actually does:
==
#!/usr/bin/perl
for (@ARGV) {
Open ($F, $_);
while (<>) {
& invoke the instruction specified by the-e parameter
& In this case, 1
return 1;
}
}
==
This explains the second Perl command-line argument involved. The process of-p,-p is to automatically provide us with a source code such as the above
The first layer is to read the name of the file specified on the command line, and as a @argv, open the file and process it;
The second-tier loop receives the file handle from open ($ARGV [*]) and executes a Perl <> operation, reading each line of the file one at a time.
And the process of processing is the program statement that we specify with-E. If you do not specify a program statement, the default condition
will be read and echo each row.
So the process of this program is as follows:
1, the circulation, sequentially reads in the @argv specified several files;
2, open the current file;
3, loop, use <> operator to read each row sequentially, and store in the temporary variable $_;
4, call-E program statement for processing; In this case,-e has only 11 parameters, representing Retuen 1 in Perl, directly returning the truth value
No processing.
Write two more useful commands:
# Perl-pe ' exit if $. > ' FILENAME
This corresponds 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, which prints the last 10 lines of the specified file--and you can also change it to what you want.
3. Kick sed.
Current 1/3 page
123 Next read the full text