Perl, Perl 5, Programming/Coding, Science

DevOps in Perl? Consider Rex.

Rex is a ‘Deployment & Configuration Management’ also knows as DevOps framework. Interestingly, it is in Perl, for Perl language. There is Ruby’s Chef & Puppet, Python’s Ansible, and Perl also has those frameworks too, but here I’m pointing to Rex because it will be much more familiar to Puppet, Chef and Ansible users in syntax and workflow perspectives.

Why do this in Perl?

Perl itself has been designed and well known for its powerful features on sysadmin tasks. Combine with this kind of framework, you are just gonna be much happier on getting the job done. Fortunately, if you are trying to opt to write ‘maintainable and long-term modules/scripts’ without using Rex, Modern Perl as a guideline should help you doing so, and CPAN with much lesser code in general.

Perl, Perl 6

Perl 6: Rakudo Linux Native Packages

Installing Rakudo on Linux can be a dissapointment. Either from compiling from source, or install the old ‘rakudo’ package from our Linux distribution repository.

Not anymore.

nxadm from Github has packaged the recent if not the most latest Rakudo Linux natives packages for Debian/Ubuntu and RPM based system (centos/fedora/opensuse).

A retweet –

Download your native linux packages here

Perl, Perl 5

Perl: Subroutine signatures

Perl 5.20 experimental feature

Simplest example:

use strict;
use warnings;
use v5.20;
use features 'signatures';

sub add($a, $b) {
    return $a + $b;
}

say add(2,2); # 4

Convenient right? Compared to this –

use strict;
use warnings;
use v5.20;

sub add {
    my ($a, $b) = @_;
    return $a + b;
}

say add(2,2); # 4

How about default arguments?

This can be done with –

use strict;
use warnings;
use v5.20;
use feature 'signatures';

sub add($a, $b = 20) {
    return $a + $b;
}

say add(10, 90); # 100
say add(10); # 30

Neat huh? 🙂

Perl 6, Programming/Coding

Building Perl 6 Module

DISCLAIMER: This is just my ‘diary’ or ‘journal’ for daily routines I do in my development process. Please go to https://docs.perl6.org/language/modules for a much better and cleaner steps to build Perl 6 modules.

Building modules can be a bit hacky in Perl 6 environment. However, let’s made things easy by using tools for building modules that has been made by other Perl 6 developers/authors.

Make sure git is installed in your system.

Using Rakudo implementation. Perl 6.c version.

Diving in

Today we gonna use mi6 as the build tool.

So let’s start by downloading the tool.

zef install App::Mi6

Now, whenever your App::Mi6 installation finished successfully, proceed to ‘init’ and build the module skeleton.

For this post, let’s use $HOME or ~/ directory.

Build module skeleton based on your module name, eg Acme::HelloWorld

The Acme-HelloWorld will be automatically setup as a git repository.

Make sure you configured you git config –global user.* first!

mi6 new Acme::HelloWorld

Above will create a new directory called, Acme-HelloWorld

Now cd into ~/Acme-HelloWorld


Bob is building…

Now, we gonna edit the META6.json

{
"authors" : [
"John Smith"
],
"build-depends" : [ ],
"depends" : [ ],
"description" : "blah blah blah",
"name" : "Acme::HelloWorld",
"perl" : "6.c",
"provides" : {
"Acme::HelloWorld" : "lib/Acme/HelloWorld.pm6"
},
"resources" : [ ],
"source-url" : "",
"test-depends" : [ ],
"version" : "*"
}

change above to

{
"authors" : [
"Smith"
],
"build-depends" : [ ],
"depends" : [ ],
"description" : "This prints "hello camelriders"",
"name" : "Acme::HelloWorld",
"perl" : "6.c",
"provides" : {
"Acme::HelloWorld" : "lib/Acme/HelloWorld.pm6"
},
"resources" : [ ],
"source-url" : "git://github.com/smith/Acme-HelloWorld.git",
"test-depends" : [
"Test",
"Test::META"],
"version" : "0.0.1"
}

Explanation to above

  1. “authors” is the authors of the module, eg “faraco”.
  2. “build-depends” is the dependencies needed to build to module.
  3. “depends” is the runtime dependencies for the module.
  4. “description” is your module subscription.
  5. “name” is your module name.
  6. “perl” is where you put the target implementation for the module to be installed.
  7. “provides” is your modules that came with the distribution. Can have more than one file.
  8. “source” is where  extra files that will get installed along with your
    module that you can access via a special variable.
  9. “test-depends” is where testing dependencies is needed, for example, Test::META for checking META6.json or legacy META.info.
  10. “version” is your module version

You can skip the subroutine and testing part, and continue to distributing ‘blank’ or ’empty’ module, but I highly recommending you to do all of the parts in this post.

Adding a subroutine into lib/Acme/HelloWorld.pm6

module Acme::HelloWorld<0.0.1>
{
    sub sayHello {
        say "Hello, camelriders!";
    }
}

Making tests.

Create a test file called 00_meta.t in t/ and edit the content

use Test;
use Test::META;
plan 1;

meta-ok();

done-testing;

and a test file called 01_use.t in t/ and edit the content

use Test;
use Acme::HelloWorld;
plan 1;

is sayHello, 'Hi, camelriders!', 'Passed';

done-testing;

Let’s test

Using zef: zef test .

-o-

using prove: mi6 test or PERL6LIB=lib prove -v -r –exec=perl6 t/

(Highly Recommended)

Activating Travis.ci integration
mi6 tool automatically build .travis.yml inside your ~/Acme-HelloWorld.

It is recommended to activate the repository in travis-ci.org for automated testing whenever a new version of your module uploaded to your repository.

Distributing

  1. Create a repository on github named, Acme-HelloWorld.
  2. Add and commit your edits first: git add . ; git commit -m “v0.0.1”
  3. Add your git remote origin, eg git remote add origin git@github.com:smith/Acme-HelloWorld.git
  4. Go to https://github.com/perl6/ecosystem/blob/master/META.list and edit the file by adding your META6.json (raw) link, eg  https://raw.githubusercontent.com/smith/Acme-HelloWorld/master/META6.json
  5. Submit the file as a pull request and let’s drink a cup of coffee while waiting for our pull request to be merged into the repository.

Let’s wait your distribution to be indexed in https://modules.perl6.org/ (usually one to two hours) after your pull request are merged.

Installing your module and testing
zef update && zef install Acme::HelloWorld

open ‘perl6’ interpreter and enter


use Acme::HelloWorld;

sayHello; # => “Hello, camelriders!”

 


It looks so hacky and tedious to make and distribute the module from my article post. It is not actually, and please visit https://docs.perl6.org/language/modules before you use this post as your primary reference to build the module.

 

Well, this is end for this article. I hope this post benefits you and aid you in journey in using Perl 6. ~~ faraco roger and out.

Perl 6

Perl 6: Poking classes

Perl 6 came with class keyword.
For example on declaring class:
class Animal {}

Setting attributes in the class:

class Animal {
    has $.species;
    has $.family;
    has $.kingdom;
}

It is boring if we can’t do anything with the attributes right? So let’s take a look at creating method.

class Animal {
    has $.species;
    has $.kingdom;
    has $.family;
    has $.aka;

    method sayInfo() {
        say "The animal normally called as, $!aka. It is $!species species.";
        say "It is also belong to $!kingdom kingdom, $!family family.";
    }
}
# instantiating

my $lion = Animal.new(species => 'P. leo', family => 'Felidae', kingdom => 'Animalia', aka => 'Lion');
$lion.sayInfo();

Well, thats it. I will cover much more topics in the future posts. Peace out.

BONUS

Subroutine dispatching or overloading.

#`(take note at the same subroutine name)

multi sub saySomething(Int $arg) {
    say "Hey, you inserted a number! $arg";
}

multi sub saySomething(Str $arg) {
    say "Hey, you inserted a string! $arg";
}

saySomething(20);   # => Hey, you inserted a number! 20
saySomething("hi");  # => Hey, you inserted a string! hi