(R)?ex Deployment & Configuration Management

Search

News

2016-09-08

Work with (R)?ex at adjust! Check out their job posting for details, or contact our fellow core developer, FErki.

2016-07-16

(R)?ex 1.4.1 released, fixing many bugs. See the release notes for more details.

2016-03-07

(R)?ex 1.4.0 released, containing lots of goodies. See the release notes for more details.

2015-09-04

(R)?ex 1.3.3 released, fixing a bunch of bugs. See the release notes for more details.

2015-06-22

adjust GmbH is sponsoring the (R)?ex project with a new build server. Thank you for your support!

2015-06-17

(R)?ex 1.3.2 released, fixing issues related to file manipulation when run on BSDs. See the release notes for more details.

2015-06-08

(R)?ex 1.3.1 released, fixing Rex::Commands::DB related tests.

2015-05-27

Read the second part of "Rex in practice" series about Test-driven infrastructure.

2015-05-09

repositor.io 1.1.0 released. repositor.io is a repository management tool for apt, yum, yast and docker. This is a bugfix release with fixes for ubuntu and centos7 installation media mirroring.

2015-05-03

(R)?ex 1.2.0 released. See the release notes for more details.

2015-03-27

View the slides of the talk An introduction to Rex from Andy Beverley.

Conferences

2016-06-21

Training

Need Help?

Rex is a pure open source project, you can find community support in the following places:

Professional support is also available.

» Home » Docs » Guides » Just enough Perl for Rex

Just enough Perl for Rex

Perl is a scripting language designed to keep easy things easy, and make hard things possible. In this tutorial you will learn just enough Perl to write your own Rex tasks.

If you have suggestions or wishes, please post a comment, tell us in our #rex channel on freenode, or just send a pull request against the GitHub repository.

Variables

Scalar variables

Scalars can contain single items, like strings, numbers, objects or 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 of scalars. Like a grocery list.

my @names = ( "John", "Fred", "Charley" );
my @to_buy = qw( Cheese Butter Salt Lemons Oranges Apples );

To access an array element you have to use its index, which starts at zero:

say "First name: " . $names[0];
say "Last name: " . $name[2];
say "Also last name: " . $name[-1];

Split a string into an array:

my $string = "John,Fred,Carl,Lewis";
my @names = split( /,/, $string );

Join the items of an array into 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

Hashes are like arrays, but with named indexes, called keys.

my %person = (
name => "John",
  age  => 28,
  city => "New York"
);

To access a hash element you have to use its 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:

for my $key ( keys %person ) {
  say "key: $key -> value: " . $person{$key};
}

But remember an important note: hashes are always unsorted.

Conditional statements

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.";
}

Loops

for my $num (1..5) {
  say "> $num";
}

# looping over an array
for my $item (@array) {
  say "> $item";
}

Regular expressions

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-insensitive matching
}

$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 matches of "john" (regardless of its case) with "Fred"

Functions

sub my_function {      # define the function called "my_function"
}

sub my_function {      # define the function called "my_function"
  my $param1 = $_[0]; # get the 1st parameter and save it in $param1
  my $param2 = $_[1]; # get the 2nd parameter and save it in $param2
  my $param3 = $_[2]; # get the 3rd parameter and save it in $param3
}

sub my_function {
  my ($param1, $param2, $param3) = @_; # the same as above
}

my_function();     # call the function "my_function"
my_function;       # also calls "my_function"
&my_function;      # also calls "my_function"
my_function("john", 28);    # call "my_function" with 2 parameters
my_function "john", 28;     # also calls "my_function" with 2 parameters: the brackets are not needed

Useful helpers

Dump the content of a scalar, array or hash

use Data::Dumper;
say Dumper($scalar);
say Dumper(@array);
say Dumper(%hash);

 

More Documentation

If you want to learn more Perl you can find a great online tutorial on Perl Maven.

 

Fork me on GitHub
Google Group / Twitter / GitHub / Mailinglist / irc.freenode.net #rex   -.รด.-   Disclaimer