10-minute Perl tutorial for Java Programmers _perl

Source: Internet
Author: User
Tags scalar


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:





 code 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.





 code as follows:

#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:





 code as follows:

E
C
A

D





If you want to sort the array, you can simply use a sort function similar to the following:





 code as follows:

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:





 code as follows:

#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.





 code as follows:

#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.





 code as follows:
$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:


 code as follows:

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:





 code as follows:

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.





 code as follows:

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.





 code as follows:

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/


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.