This is a work-in-progress guide. If you have suggestions or whishes, just post a comment or join our irc channel on freenode -> #rex.
Perl is a scripting language designed to keep easy things easy and hard things possible. In this tutorial you will learn just enough Perl to write your own Rex tasks.
Scalar variables. Scalars can contain Strings, Numbers, Objects, References.
my $name = "John"; # this is a string my $age = 28; # this is a number (integer) my $float = 28.5; # also a number, but a float my $car = Car->new(); # this is an object from the class Car
Array variables. Arrays are lists. Like a grocery list. They contains lot of scalars.
my @names = ("John", "Fred", "Charley");
my @to_buy = qw(Cheese Butter Salt Lemons Oranges Apples);
To access an array element you had to use the index. It starts a zero.
say "First name: " . $names[0]; say "Last name: " . $name[2]; say "Also last name: " . $name[-1];
Split a string to an array
my $string = "John,Fred,Carl,Lewis"; my @names = split(/,/, $string);
Join an array to a String
my @names = ("John", "Fred", "Carl", "Lewis");
my $string = join(",", @names); # -> John,Fred,Carl,Lewis
If you want to iterate over an array do it like this
for my $name (@names) {
say "Current name: $name";
}
Hash variables. Hashs are like arrays, but with named indexes.
my %person = ( name => "John", age => 28, city => "New York" );
To access an hash element you had to use the key.
say "Name: " . $person{"name"};
say "Age: " . $person{"age"};
say "City: " . $person{"city"};
If you want to iterate over a hash do it like this. But always remember. Hashs are always unsorted.
for my $key (keys %person) {
say "key: $key -> value: " . $person{$key};
}
if($name eq "John") {
say "Hello, my name is John!";
}
else {
say "Well, my name is not John...";
}
if($name ne "John") {
say "Yes, my name is NOT John...";
}
else {
say "Hello, my name is John!";
}
if($age < 30) {
say "I'm younger than 30";
}
elsif($age >= 30 && $age <= 50) {
say "Well, i'm between 30 and 50.";
}
else {
say "I'm older than 50...";
}
for my $num (1..5) {
say "> $num";
}
# looping over an array
for my $item (@array) {
say "> $item";
}
my $name = "John";
if($name =~ m/john/) { # will not match, because the J in $name is uppercase.
}
if($name =~ m/john/i) { # _will_ match, because we use the "i" modifier (for case insensitiv matches)
}
$name =~ s/john/Fred/i; # this will replace the first match of john (regardless of its case) with "Fred"
$name =~ s/john/Fred/ig; # this will replace all matchs of john (regardless of its case) with "Fred"
sub my_function { # define the function "my_function"
}
sub my_function { # define the function "my_function"
my $param1 = $_[0]; # get 1st parameter and safe it in $param1
my $param2 = $_[1]; # get 2nd parameter and safe it in $param2
my $param3 = $_[2]; # get 3rd parameter and safe it in $param3
}
sub my_function { # the same as above.
my ($param1, $param2, $param3) = @_;
}
my_function(); # call the function "my_function"
my_function; # also calls "my_function"
my_function("john", 28); # call the function "my_function" with 2 parameters.
my_function "john", 28; # also calls "my_function" with 2 parameters. the brackets are not needed.
Dump the content of a scalar, array or hash
use Data::Dumper; say Dumper($scalar); say Dumper(@array); say Dumper(%hash);