SoFunction
Updated on 2025-04-08

Perl program with only one line page 1/3

1 line perl
Let’s talk about perl today, there is only one line of perl program.
Among various programming languages, perl is famous for its perverted and quirky. For example, I often write perl programs that I can't even understand.
For example, although the program in this document is short, I guess I will feel 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 flexibility is its biggest advantage.
You can write this way, "or, the trick is the same."
1. hello world!
===========================================================
Start with the most classic hello, world!...
##
# perl -e 'print "hello,world!\n"'
hello,world!
##
Not surprisingly, he did not have any discrimination and finally we were willing to output "hello, world!", which was naturally like I used echo or
printf() is the same.
If this program is developed in the traditional form, it looks like this:
==
#!/usr/bin/perl
print "hello,world!\n";
==
We used the -e parameter of perl on the command line, which will make perl automatically built-in the string followed by -e as a perl program that automatically takes perl's string
Expand execution to eliminate the pain of rewriting files with vi or cat.
The function of -e parameter is to automatically execute the subsequent string as the program theme, and the subsequent parameters will be used as the command line of the perl script
Parameters ($ARGC and @ARGV) are passed to the program statement.
2. I want cat.
===========================================================
hello, world! It seems to have no effect at all, but in fact we can enrich it and use it to accomplish any work - as long as your imagination is rich enough.
A very famous project in the perl world is ppt(pOWER pERL tOOLS), which uses pure perl to re-implement all commonly used unix commands.
Let's take a look at the simplest cat.
##
# perl -pe 1 /etc/hosts
127.0.0.1 localhost
192.168.0.3 vi
##
Just like cat, this line of command outputs the /etc/hosts file as it is. Let's see what it actually does:
==
#!/usr/bin/perl
for(@ARGV){
open($F,$_);
while(<>){
&Call the command specified by the call execution-e parameter
&In this case, 1
return 1;
}
}
==
Here I will explain the second perl command line parameter -p, -p process involved in it is actually automatically providing us with a source code like the above
The double-layer loop seen in the first layer is to read the file name specified on the command line in turn, as @ARGV, open the file and process it;
The second layer loop receives the file handle from open($ARGV[*]) and performs a perl <> operation, reading each line of the file at once.
And process it, the process is to use the program statement specified by -e. If no program statement is specified, then the default situation
Will be to read and echo each line.
So the process of this program is as follows:
1. Loop to read several files specified in @ARGV in turn;
2. Open the current file;
3. Loop, use the <> operator to read each row in turn, and store it in the temporary variable $_;
4. The program statement that calls -e is used for processing; in this example, -e has only one parameter, which represents retuen 1 in perl, and directly returns the true value.
No treatment.
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, which prints the last 10 lines of the specified file -- and you can also modify it to what you want.
3. kick sed。