10 minutes Perl tutorial for Java developer

This 10 minutes tutorial is not meant to compare Java with Perl. The purpose is to explore how to quickly learn Perl as a Java developer. The following are some key notes from my perspective.

1. Basics to Start

Unlike Java, there is no need to claim a “main” method as entry point. To run a simple Perl program like the following:

# comment starts with "#"
# the name is hello.pl
print "Hello Perl!";

Just do:

perl hello.pl

2. Data Types

Date types in Perl is very simple, it has only 3 types: Scalar, Array, and Hash.

Scalar is a single value, it basically can be anything other than Array or Hash.
Array is an array that can contain different types of elements such as integer and string.
Hash is basically like the HashMap in Java.

The following code combine the usage of all of them.

#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 an array
my @anArray = keys (%aHash);
 
#loop array and output each scalar
foreach my $aScalar (@anArray){
	print $aScalar."\n";
}

Output would be:

e
c
a
b
d

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

foreach my $aScalar (sort @anArray){
	print $aScalar."\n";
}

3. Conditional & Loop Statements

Perl has if, while, for, foreach keywords for conditionals and loops, which is very similar with Java except switch.

The following example uses all of them.

#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. File Read/Write

The following example shows how to read from and write to a file. Note that the difference between “>” and “>>” is that “>>” appends content to the file while “>” create a new one.

#read from a file
my $file = "input.txt";
open(my $fh, "<", $file) or die "cannot open < $file!";
while ( my $aline = <$fh> ) {
	#chomp so 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 Expression

There are mainly two functions you use regular expression: m and s.

The following code match the string $str with regular expression.

$str =~ m/program(creek|river)/

If $str is “programcreek”, the expression returns true and can be used as a condition for conditionals and loops.

6. Parse by Value/Reference

It is not necessary to define methods/functions for a Perl program, but doing so would greatly improve code modularity and reusability. Then we need to be careful about parameter passing.

It is straightforward to pass a scalar, but when it comes to array and hash. It needs extra attention.

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 );

Hash

sub printHash($) {
	my %hash = %{ shift() };
	for my $key ( sort keys %hash ) {
		my $value = $hash{$key};
		print "$key => $value\n";
	}
}
 
printHash(\%twoLettersCount);

7. Some Cool/Tricky Examples

1). Loop through each character of a string

my @lineCharArray = split('',$aline);
 
foreach my $character (@lineCharArray){
	print $character."\n";
}

2). Create an array holding all 26 letters

Instead of loop 26 times, you can simply do the following:

my @charArray = ('a'..'z' );
my @twoCharArray = ('aa'..'zz');

This is the first edition of the 10 minutes. I will update some part based on comments.

2 thoughts on “10 minutes Perl tutorial for Java developer”

Leave a Comment