1. Starting from the basics
Unlike Java,perl, you do not need the "main" method as the entry point. To run a simple Perl program is as follows:
# comment starts with "#"
# The name is hello.pl
Print "Hello perl!";
Just perform:
Perl hello.pl
2. Date Type
The date type in Perl is very simple, it has 3 types: scalar, array, and hash.
The mark is a single value, and it can basically be any other than an array or hash.
An array is an array that can contain different types of elements, such as integers, strings.
Hashes are basically like HashMap in Java.
Combine the following code with all the usage scenarios.
#claim a hash and assign some values
My%ahash;
$aHash {' A '}=0;
$aHash {' B '}=1;
$aHash {' C '}=2;
$aHash {' d '}=3;
$aHash {' E '}=4;
#put all keys to a array
My @anArray = keys (%ahash);
#loop array and output each scalar
foreach my $aScalar (@anArray) {
Print $aScalar. " \ n ";
}
Output results:
If you want to sort the array, you can simply use a sort function similar to the following:
foreach my $aScalar (sort @anArray) {
Print $aScalar. " \ n ";
}
3. Condition, cyclic expression
Perl prepares the IF, while, for, foreach keywords for conditional and circular statements, which is very similar to Java (except for switch).
For more information, see the following code:
#if my $condition = 0;
if ($condition = = 0) {
print "=0\n";
}
elsif ($condition = = 1) {
print "=1\n";
}
else{
print "others\n";
}
#while while ($condition < 5) {
Print $condition;
$condition + +;
}
For (my $i =0; $i < 5; $i + +) {
Print $i;
}
#foreach My @anArray = ("A", 1, ' C ');
foreach my $aScalar (sort @anArray) {
Print $aScalar. " \ n ";
}
4. Reading and writing of documents
The following example shows us how to read and write a file. Notice the difference between ">" and ">>", ">>" appends to the end of the file, and ">" Creates a new file store information.
#read from a file
My $file = "input.txt";
Open (My $fh, "<", $file) or die "Cannot open < $file!";
while (My $aline = < $fh >) {
#chomp no new line character
Chomp ($aline);
Print $aline;
}
Close $fh;
# Write to a file
My $output = "output.txt";
Open (My $fhOutput, ">", $output) or Die ("Error:cannot open $output file!");
Print $fhOutput "something";
Close $fhOutput;
5. Regular expressions
There are two ways to use regular expressions in Perl: M and S.
The following code applies a regular expression on the $str.
$str =~ m/program<span> (</span>creek|river)/
If the content of the $STR is "Programcreek", the expression returns TRUE. This can also be used for conditional judgments or loops.
6. The grammar of the Pass value/reference
There is no need to define methods/functions in Perl, but if you do, that will greatly improve the modularity and availability of the code. But we need to be very careful with the transfer of parameters.
You can pass a scalar directly, but you need to be particularly careful if you pass an array or a hash class.
Array:
My @testArray = (1, 3, 2);
#In Sub Sub Processarraybyreference ($) {
my $arrayref = shift;
My @array = @ $arrayref;
#...
}
#In Sub Processarray:sub processarraybyvalue ($) {
my @array = @_;
#...
}
Processarraybyvalue (@testArray);
Processarraybyreference (\ @testArray);
HA series:
Sub Printhash ($) {
My%hash =%{shift ()};
For my $key (sort keys%hash) {
My $value = $hash {$key};
Print "$key => $value \ n";
}
}
Printhash (\%twoletterscount);
7. Some examples
1). Iterate through each character in the string.
My @lineCharArray = Split (", $aline);
foreach my $character (@lineCharArray) {
Print $character. " \ n ";
}
2. Create an array that contains 26 letters.
You can simply implement this function and do not need to cycle 26 times.
My @charArray = (' a '.. ' Z ');
My @twoCharArray = (' AA ' ... ' ZZ ');
The above is the first version of "10 minutes", I will continue to update this article based on comments.
See the original: http://www.programcreek.com/2012/09/10-minutes-perl-tutorial-for-java-developer/