Programming/Coding

Which programming language to use when solving a problem in specific domain?

Perl


Unix/Linux scripting, system administration, one liners and command line utilities.

Python


Science, mathematics, machine learning, Raspberry Pi, web development, desktop applications, and command line utilities.

PHP


Simple to medium-sized and agile web development.

Bash


Bourne shell replacement, simple unix scripting, and used when no better language implementations available.

Clojure


Exploratory programming and practical JVM language.

Advertisements
Programming/Coding

How to make commenting code fun?

Comment, comment comment. It must been a hell of a side job as a programmer or similar to do. We are programmers, we writes code, not documents, yada yada..

Well, that is what I thought in the first place.

WRONG!

Commenting is very very important in a long term or growing project. No matter how beautiful and readable your ‘self-documenting code’ will be. Why? The key is again, why?

Your code already does what it does, tell the computer to do things, and tell you what it does. However, why the code is even there in the first place? Why?

The first step to make documenting and reading it as a fun thing is, make it tell you why the code is there.

For instance, you have a function that writes to a configuration file on a Linux hosting server with only FTP access without SSH (I know it sucks, consider alternatives including DigitalOcean for VPS-like hosting or Bluehost also Dreamhost for SSH access).

An example comment for this scenario is – the server doesn’t have SSH access to execute Linux commands, so we implement this to write the code invoked from the execution of the web app.

And there ladies and gentleman, will make your life easier by commenting why instead of what.

Don’t repeat the code, assume the audience already understand the language and algorithm, and it is their fault for not taking enough time and effort to understand it (unless you writing clever code and cryptic algorithms, you are to blame instead).

Other keys like enforcing the use of good grammar and simplicity of the commenting language is also important too, but don’t let that stopping you from commenting your code if you are not fluent at the target language.

You are an awesome person for reading this. Why? because you have the passion to look the answer, to be a better developer, thus making your life and others easier to make reading codes and documentations an enjoyable experience.

Protect it, live it, love it, soon it is nothing for you, but a good habit.

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 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.

Uncategorized

Perl module: App::Pastebin::Create

What is App::Pastebin::Create?

It’s a pastebin creator + uploader for pastebin.com, a tool written in Perl.

How to use it?

pastebin-create --text "Your text" --format 'perl' --expiry 10m --private 0 --desc "My perl mod title"

This will result a link to the public paste, with “My perl mod title” as the paste description, that will expiry in 10 minutes, with perl syntax highlighting, and “Your text” as the content.

How to install?

make sure you already configured CPAN first!

I recommend using cpanm instead of cpan for installation. To install cpanm, you need to run cpan App::cpanminus first.

From my forked distribution on Github (maintained by me):

bash <(curl -L https://gist.githubusercontent.com/faraco/b6acb4626da6962ad46aa7e250d1ac12/raw/d433116845cb0a3ce849edb75789a6053ef4e2ce/install.bash)

With cpanm (official CPAN): > `cpanm App::Pastebin::Create`

with cpan:

cpan App::Pastebin::Create


Read More

Tags: perl, programming, linux, unix, module, library, pastebin