Monday

Getting Started with Perl programming

Perl stands for Practical Extraction and Reporting Language.
Perl is the programming language. perl is the compiler.

Typical uses of Perl are:
--- text processing
--- system administration tasks
--- cgi and web programming
--- database interaction
--- other internet programming

If you are using a unix-compatible system such as Linux Mint Mate, most chances are that Perl is already installed there.
Check you have Perl installed by typing the command below into your terminal program.
Right-click on Desktop, select "Open In Terminal".
$ perl -v
$ whereis perl
perl: /usr/bin/perl

Open a text editor.
Start Menu > Applications > Accessories > Text Editor (Pluma).
Create a new file with the following content.
/////
#!/usr/bin/perl
use strict;
use warnings;

print "Hello, World!\n";
/////
Save this file into your "perl_code" folder and name the file "hello_world.pl"

Run the script from the terminal by typing:
$ perl hello_world.pl
Hello, World!

Make hello_world.pl file as the executable program.
$ chmod +x hello_world.pl
$ ./hello_world.pl
Hello, World!

Perl comments start with a hash (#).

Types of Perl Variables
--- Scalar variables start with $
--- Array variables start with @
--- Hash variables start with %

File name: variables.pl
/////
#!/usr/bin/perl
use strict;
use warnings;

my $num1 = 1;
my $num2 = 0.1;
my $sum = $num1 + $num2;

print "$num1 plus $num2 is '$sum'.\n";
/////
Output:
$ perl variables.pl
1 plus 0.1 is '1.1'.

Array Variables
my @fruits = ('apple','orange','guava','grape');
print "$fruits[1] \n";    #orange
print "@fruits[1,3] \n";  #orange grape
print "@fruits[1..3] \n"; #orange guava grape


Hash Variables
Easier to understand using "fat comma =>". 
%german = (one=>'ein',two=>'zwei',three=>'drei');
print "$german{two} \n"; #zwei


String Operators
$name = $firstname . ' ' . $secondname; #concatenation
$line = '-' x 80; #repetition

String Functions
$len = length $a_string; #returns string length
print uc $string, "\n", lc $string; #uppercase, lowercase

Perl Functions
File name: function1.pl
File content:
/////
#by default, all variables in Perl are global variables
#$n1 = 0; #default number1 value $n1 is 0

#call function1
$num1 = &function1(10,11);
print "bigger number is $num1\n";
$num1 = &function1(13,12);
print "bigger number is $num1\n";

sub function1 {
  $n1 += 1; #global variable $n1
  print "global number \$n1 is $n1\n";
  my $n2 += 1; #private variable $n2
  print "private number \$n2 is $n2\n";
  if ($_[0] > $_[1]) {
    $_[0]; #return first argument    
  } else {
    $_[1]; #return second argument
  }
}
/////
Sample output:
$ perl function1.pl
global number $n1 is 1
private number $n2 is 1
bigger number is 11
global number $n1 is 2
private number $n2 is 1
bigger number is 13

File Operations
open(FILE, 'in.data');
$line = <FILE>; #read one line
@lines = <FILE>; #read all lines
close(FILE);

Loop: for
for ($i=1; $i<=10; $i++) {
  print "$i squared is ", $i*$i, "\n";
}

Loop: foreach
Syntax: foreach VAR (LIST) {BLOCK}
Example 1:
foreach $i (1 .. 10) {
  print "$i squared is ", $i*$i, "\n";
}
Example 2:
my %months = (Jan=>31, Feb=>28);
foreach (keys %months) {
  print "$_ has $months{$_} days\n";
}

Regular Expressions:
Patterns that match strings
Documented in perldoc perlre
The key to Perl's text processing power
m/PATTERN/ = the match operator
s/PATTERN/REPLACEMENT/ = the substitution operator
Quantifier {n} = match exactly n
Quantifier {n,} = match n or more
Quantifier {n,m} = match between n and m inclusive

References:
http://learn.perl.org/first_steps/
http://www.perlmonks.org/, Perl Monks for help and advice
http://perldoc.perl.org/, Perl documentation
http://perl-tutorial.org/
http://www.tutorialspoint.com/perl/perl_arrays.htm

If a hater attacked your age and not the goodness of you

Whether young or old, I've always been known what endures. I've known the very idea of people that were all created equal and deserv...