1 – Lessons begin

Welcome to Coding-School.com old school!

Some of the tips, techniques and code freely imparted here, I’ve searched the web previously and come back empty handed. Then set about hand crafting my own solutions – which you get completely for free! 🙂

What gives me the audacity, to think I could possible teach anyone anything Technical! Well here is my 5 line bio:

  • Over 25 years IT work experience. Working with IBM, Compaq, ICL, Dunn and Bradstreet and 2 Banks.
  • Dabbling with LDAP for around 15 years.
  • Dabbling with PHP for around 15 years.
  • Dabbling with Perl for the last 20 years.
  • Over 20 years UNIX Admin. Well you might ask, what the hell has that do to with Coding. IMHO – best Admins code all the time!! 🙂

Cheers and enjoy,


Powershell Intro

In recent years it has become necessary to learn Powershell. This is for a number of reasons, but predominately because certain automations are only possible via it. More on that later.

First – how to run powershell … simply windows key + R and powershell.

Or Windows key and start typing powershell – select Windows PowerShell ISE. This provides an IDE.


$single_quoted_string = 'a '
$double_quoted_string = "a string – $x"
Write-Host $single_quote_string
Write-Host $double_quoted_string

# can enforce with [string]

## Integers
[int]$int_one = 1
$int_two = 2
$int_one + $int_two

## Arrays
[array]$arr = @(1,'str_var',5)
$arr += 'add to arr'

## Hash tables
[hashtable]$htab = @{'key1' = 'value1'; 'key2' = 'value2'}


$htab.Add('newkey',”new added variable”)

$htab.Set_Item(“key1”, “mod_val1”)


$a = 1
if ($a -eq 1) {
Write-Host 'a equals 1'
} else {
Write-Host 'a is not equal to 1'

# also possible to elseif

} elseif ($a -eq 3) {


$a = 1

switch ($a) {
1 {“Value 1”}
2 {“Value 2”}
3 {“Value 3”}
default {“Value exceeds threshold.”}

$b = “X365”

switch -wildcard ($b) {
“Z*” {“Val Z”}
“Y*” {“Val Y”}
“X*” {“Val X”}
default {“Val outside parameters.”}

Perl Intro

Introduction to Perl

Perl is an acronym, short for Practical Extraction and Report Language. It was designed by Larry Wall as a tool for writing programs in the UNIX environment (Perl is a stable, cross platform programming language). Perl has the power and flexibility of a high-level programming language such as C. In fact many of the features of the language are borrowed from C. Like shell script languages, Perl does not require a compiler – the Perl interpreter runs your programs. This means that Perl is ideal for producing quick solutions to small programming problems and creating prototypes to test potential solutions to larger problems.

  • Perl is a stable, cross platform programming language.
  • It is used for mission critical projects in the public and private sectors.
  • Perl is Open Source software, licensed under the GNU General Public License (GPL).
  • Perl was created by Larry Wall.
  • Perl is listed in the Oxford English Dictionary

mod_perl allows the Apache web server to embed a Perl interpreter.

Perl’s DBI package makes web-database integration easy.

@INC is also printed as part of the output of
% perl -V

Example on how to read a url/web page in perl

use LWP::Simple;
my $url="http://www.gnulamp.com/perl.html";
my $webdata=get $url;
print $webdata;

Shiny DashBoard

"Having studied Data Science since April 2014, felt it a good time to get to know Rstudio’s Shiny Server! So sourced data.gov.au’s Disaster Events and built an interactive dashboard. Writing up my experiences as a way of introduction, in the hope it might help others to learn Shiny Server."

If you just want to see the demo first » CLICK HERE «

  1. First requirement is to have a fair understanding of R. There are a ton of courses online, such as on Rstudio, EDX and Coursera. My favourite course is MIT’s Analytics Edge. As a currently archived course you can complete it self paced, although assessments are disabled.
  2. Next to complete the Shiny Tutorial. It is very comprehensive and the only requirement is Rstudio, naturally. Previously the tutorial comprised 3 parts, but now are presented in the single video lasting a bit of 2 hours 25 minutes. I highly recommend it.
  3. Study the Shiny Dashboard Instructions – my source code might help too
  4. Reference the slides:
Now onto my demo!

The Australian government provides public access to numerous data sets and encourages re-use. Therefore chose data from data.gov.au to build my demo, as it is freely available.

This is what the data looked like: raw-data

Quick preview of what I achieved thanks to Shiny Dashboards:
ausgov-homes-destroyed ausgov-world

The example I created converts a CSV file to data tables, graphs and map of Australia using uses RStudio’s Shiny Dashboard =>

Australia Government Disaster Events Dashboard

And the Source code

Lastly this tutorial helped with the maps.

Enjoy and feel free to comment below – would be good to receive feedback.

Perl Signals

Perl Signals

Perl allows you to trap signals using the %SIG associative array. Using the signals you want to trap as the key, you can assign a subroutine to that signal. The %SIG array will only contain those values which the programmer defines. Therefore, you do not have to assign all signals. For example, to exit cleanly from a ^C:


    print "Caught Interrupt (^C), Aborting\n";

There are two special “routines” for signals called DEFAULT and IGNORE. DEFAULT erases the current assignment, restoring the default value of the signal. IGNORE causes the signal to be ignored. In general, you don’t need to remember these as you can emulate their functionality with standard programming features. DEFAULT can be emulated by deleting the signal from the array and IGNORE can be emulated by any undeclared subroutine.

In 5.001, the $SIG{__WARN__} and $SIG{__DIE__} handlers may be used to intercept die() and warn(). For example, here’s how you could promote unitialized variables to trigger a fatal rather merely complaining:

#!/usr/bin/perl -w 

require 5.001;

$SIG{__WARN__} = sub {
    if ($_[0] =~ /uninit/) {
        die $@;
    } else {
        warn $@;

Perl DBI

Perl DBI Example

The DBI module enables your Perl applications to access

multiple database types transparently. You can connect to

MySQL, MSSQL, Oracle, Informix, Sybase, ODBC etc. without

having to know the different underlying interfaces of each.

The API defined by DBI will work on all these database types

and many more.

What is DBI and DBD::Oracle and where can one get it from?

DBI (previously called DBperl) is a database independent interface module for Perl. It defines a set of methods, variables and conventions that provide a consistent database interface independent of the actual database being used.

DBD::Oracle is the Oracle specific module for DBI. It can be downloaded from CPAN.

What DBI drivers have I got?

In DBI you can programmatically discover what DBI drivers are installed.

#!/usr/bin/perl -w

require DBI;

my @drivers = DBI->available_drivers;

print join(", ", @drivers), "n";


use strict;

use DBI;

my $dbh = DBI->connect( 'dbi:Oracle:orcl',




                          RaiseError => 1,

                          AutoCommit => 0


                      ) || die "Database connection not made: $DBI::errstr";

my $sql = qq{ CREATE TABLE employees ( id INTEGER NOT NULL,

                                       name VARCHAR2(128),

                                       position VARCHAR2(128),

                                     ) };

$dbh->do( $sql );



Sybperl implements three Sybase extension modules to perl (version 5.002 or higher). Sybase::DBlib adds a subset of the Sybase DB-Library API. Sybase::CTlib adds a subset of the Sybase CT-Library (aka the Client Library) API. Sybase::Sybperl is a backwards compatibility module (implemented on top of Sybase::DBlib) to enable scripts written for sybperl 1.0xx to run with Perl 5. Using both the Sybase::Sybperl and Sybase::DBlib modules explicitly in a single script is not garanteed to work correctly.

The general usage format for both Sybase::DBlib and Sybase::CTlib is this:

use Sybase::DBlib;

# Allocate a new connection, usually refered to as a database handle

$dbh = new Sybase::DBlib username, password;

# Set an attribute for this dbh:

$dbh->{UseDateTime} = TRUE;

# Call a method with this dbh:

$dbh->dbcmd(sql code);

The DBPROCESS or CS_CONNECTION that is opened with the call to new() is automatically closed when the $dbh goes out of scope:

sub run_a_query {

my $dbh = new Sybase::CTlib $user, $passwd;

my @dat = $dbh->ct_sql("select * from sysusers");

return @dat;


# The $dbh is automatically closed when we exit the subroutine.


A generic perl script using Sybase::DBlib would look like this:

use Sybase::DBlib;

$dbh = new Sybase::DBlib 'sa', $pwd, $server, 'test_app';

$dbh->dbcmd("select * from sysprocessesn");



while(@data = $dbh->dbnextrow)


 .... do something with @data ....


$dbh = Sybase::DBlib->dbopen([$server [, $appname, [{attributes}] ]])

Open an additional connection, using the current LOGINREC information.

$status = $dbh->dbuse($database)

Executes “use database $database” for the connection $dbh.

$status = $dbh->dbcmd($sql_cmd)

Appends the string $sql_cmd to the current command buffer of this connection.

$status = $dbh->dbsqlexec

Sends the content of the current command buffer to the dataserver for execution. See the DB-library documentation for a discussion of return values.

$status = $dbh->dbresults

Retrieves result information from the dataserver after having executed dbsqlexec().

$status = $dbh->dbcancel

Cancels the current command batch.

$status = $dbh->dbcanquery

Cancels the current query within the currently executing command batch.


Free the command buffer (required only in special cases – if you don’t know what this is you probably don’t need it 🙂


Force the closing of a connection. Note that connections are automatically closed when the $dbh goes out of scope.


Returns TRUE if the DBPROCESS has been marked DEAD by DBlibrary.

$status = $dbh->DBCURCMD

Returns the number of the currently executing command in the command batch. The first command is number 1.

$status = $dbh->DBMORECMDS

Returns TRUE if there are additional commands to be executed in the current command batch.

$status = $dbh->DBCMDROW

Returns SUCCEED if the current command can return rows.

$status = $dbh->DBROWS

Returns SUCCEED if the current command did return rows

$status = $dbh->DBCOUNT

Returns the number of rows that the current command affected.

$row_num = $dbh->DBCURROW

Returns the number (counting from 1) of the currently retrieved row in the current result set.

$status = $dbh->dbhasretstat

Did the last executed stored procedure return a status value? dbhasretstats must only be called after dbresults returns NO_MORE_RESULTS, ie after all the selet, insert, update operations of he sored procedure have been processed.

$status = $dbh->dbretstatus

Retrieve the return status of a stored procedure. As with dbhasretstat, call this function after all the result sets of the stored procedure have been processed.

$status = $dbh->dbnumcols

How many columns are in the current result set.

$status = $dbh->dbcoltype($colid)

What is the column type of column $colid in the current result set.

$status = $dbh->dbcollen($colid)

What is the length (in bytes) of column $colid in the current result set.

$string = $dbh->dbcolname($colid)

What is the name of column $colid in the current result set.

@dat = $dbh->dbretdata[$doAssoc])

Retrieve the value of the parameters marked as ‘OUTPUT’ in a stored procedure. If $doAssoc is non-0, then retrieve the data as an associative array with parameter name/value pairs.

Perl OOP

Perl OOP

Why Object Oriented approach?

A major factor in the invention of Object-Oriented approach is to remove some of the flaws encountered with the procedural approach. In OOP, data is treated as a critical element and does not allow it to flow freely. It bounds data closely to the functions that operate on it and protects it from accidental modification from outside functions. OOP allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects. A major advantage of OOP is code re-usability.

Some important features of Object Oriented programming are as follows:

  • Emphasis on data rather than procedure
  • Programs are divided into Objects
  • Data is hidden and cannot be accessed by external functions
  • Objects can communicate with each other through functions
  • New data and functions can be easily added whenever necessary
  • Follows bottom-up approach

Concepts of OOP:

  • Objects
  • Classes
  • Data Abstraction and Encapsulation
  • Inheritance
  • Polymorphism

    Briefly on Concepts:


    Objects are the basic run-time entities in an object-oriented system. Programming problem is analyzed in terms of objects and nature of communication between them. When a program is executed, objects interact with each other by sending messages. Different objects can also interact with each other without knowing the details of their data or code.


    A class is a collection of objects of similar type. Once a class is defined, any number of objects can be created which belong to that class.

    Data Abstraction and Encapsulation

    Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes.

    Storing data and functions in a single unit (class) is encapsulation. Data cannot be accessible to the outside world and only those functions which are stored in the class can access it.


    Inheritance is the process by which objects can acquire the properties of objects of other class. In OOP, inheritance provides reusability, like, adding additional features to an existing class without modifying it. This is achieved by deriving a new class from the existing one. The new class will have combined features of both the classes.


    Polymorphism means the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends on the data types used in the operation. Polymorphism is extensively used in implementing Inheritance.

    Advantages of OOP

    Object-Oriented Programming has the following advantages over conventional approaches:

    • OOP provides a clear modular structure for programs which makes it good for defining abstract datatypes where implementation details are hidden and the unit has a clearly defined interface.
    • OOP makes it easy to maintain and modify existing code as new objects can be created with small differences to existing ones.
    • OOP provides a good framework for code libraries where supplied software components can be easily adapted and modified by the programmer. This is particularly useful for developing graphical user interfaces.

    Perl Package

  • To create a class in Perl, we first create a


  • A package (module) is a module of variables and

    subroutines, which can be re-used over and over again.

  • They provide a separate namespace within a Perl

    program that keeps subroutines and variables from

    conflicting with those in other packages.

    To declare a class named Person in Perl we do:

    package Person;

  • The scope of the package definition extends to the

    end of the file

  • or until another package keyword is encountered.
    Perl Methods

    A method (subroutine) is a way of accessing objects. In

    Perl, a method is just a subroutine defined within a

    particular package. So to define a method to print our

    Person object, we do:

    sub print {
        my ($self) = @_; ## creating reference
        #print Person info
        printf( "Name:%s %snn", $self->firstName, $self->lastName );

    The subroutine print is now associated with the package

    Person. To call the method print on a Person object, we

    use the Perl “arrow” notation. If the variable $mike

    contains a Person object, we would call print on that

    object by writing:


    When the object method is invoked, a reference to the

    object is passed in along with any other arguments

    (including class name). This is important since the

    method now has access to the object on which it is to


    How do we create object?
  • To create an instance of a class (an object) we need

    an object constructor.

  • This constructor is a method defined within the


  • Most programmers choose to name this object

    constructor method new, but in Perl one can use any


  • One can use any kind of Perl variable as an object

    in Perl.

  • Most Perl programmers choose either

    references to arrays or hashes.

    Let’s create our constructor for our Person class using

    a Perl hash reference;

    package Person;
    sub new {
        my $self = {
            _firstName => undef,
            _lastName  => undef,
            _ssn       => undef,
            _address   => undef,
            _salary    => undef,
            _dept      => undef
        bless $self, 'Person';
        return $self;
    sub print {
        my ($self) = @_; ## creating reference
        #print Person info
        printf( "Name:%s %snn", $self->firstName, $self->lastName );
  • We created a subroutine (method) called new

    associatedwith the package Person.

  • Other method is print (as discussed earlier).
  • The entries of the hash reference $self become the

    attributes of our object. We then use the bless function

    on the hash reference.

  • The bless function takes two arguments: a reference

    to the variable to be marked and a string containing the

    name of the class. This indicates that the variable now

    belongs to the class Person.

    To create an instance of our Person object:

    my $mike = new Person();

    We have not defined accessor methods or done any error

    checking on the input values or keys or the anonymous

    hash reference, but we have the start of a Perl Person

    OO framework. To make our constructor more flexible and

    to make our class inheritable (more on that later), we

    can define it to use the $class variable to bless the

    hash reference.

    sub new {
        my ($class) = @_;
        my $self = {
            _firstName => undef,
            _lastName  => undef,
            _ssn       => undef,
            _address   => undef,
            _salary    => undef,
            _dept      => undef
        bless $self, $class;
        return $self;

    Other object-oriented languages have the concept of

    security of data to prevent a programmer from changing

    an object data directly and so provide accessor methods

    to modify object data. Perl does not have private

    variables but we can still use the concept of accessor

    methods and ask programmers to not mess with our object


    For our Person class, we should provides accessor

    methods for our object attributes; name, address, title,


    package Person;
    use strict;
    use Address;  #Person class will contain an Address
    sub new {
        my ($class) = @_;
        my $self = {
            _firstName => undef,
            _lastName  => undef,
            _ssn       => undef,
            _address   => undef
            _salary    => undef,
            _dept      => undef
        bless $self, $class;
        return $self;
    #accessor method for Person first name
    sub firstName {
        my ( $self, $firstName ) = @_;
        $self->{_firstName} = $firstName if defined ($firstName);
        return $self->{_firstName};
    #accessor method for Person last name
    sub lastName {
        my ( $self, $lastName ) = @_;
        $self->{_lastName} = $lastName if defined($lastName);
        return $self->{_lastName};
    #accessor method for Person address
    sub address {
        my ( $self, $address ) = @_;
        $self->{_address} = $address if defined($address);
        return $self->{_address};
    #accessor method for Person social security number
    sub ssn {
        my ( $self, $ssn ) = @_;
        $self->{_ssn} = $ssn if defined($ssn);
        return $self->{_ssn};
    sub salary {
        my ( $self, $salary ) = @_;
        $self->{_salary} = $salary if defined($salary);
        return $self->{_salary};
    sub dept {
        my ( $self, $dept ) = @_;
        $self->{_dept} = $dept if defined($dept);
        return $self->{_dept};
    sub print {
        my ($self) = @_;
        #print Person info
        printf( "Name:%s %snn", $self->firstName, $self->lastName );
    Making Objects

    Object-oriented programming sometimes involves

    inheritance. Inheritance simply means allowing one class

    called the Child to inherit methods and attributes from

    another, called the Parent, so you don’t have to write

    the same code again and again. For example, we can have

    a class Employee which inherits from Person. This is

    referred to as an “isa” relationship because an employee

    is a person. Perl has a special variable, @ISA, to help

    with this.

    @ISA governs (method) inheritance. So to create a new

    Employee class that will inherit methods and attributes

    from our Person class, we simply code:

    # class Employee
    package Employee;
    use Person;
    use strict;
    our @ISA = qw(Person);    # inherits from Person

    What we have done is load the Person class and declare

    that Employee class inherits methods from it. We have

    declared no methods for Employee but an Employee object

    will behave just like a Person object. We should be able

    to write code:

    #create Employee class instance
    my $mike =  new Employee();
    #set object attributes

    without any other changes.

    Now let’s add some methods.

    # class Employee
    package Employee;
    use Person;
    use strict;
    our @ISA = qw(Person);    # inherits from Person
    sub new {
        my ($class) = @_;
        #call the constructor of the parent class, Person.
        my $self = $class->SUPER::new();
        $self->{_id}   = undef;
        $self->{_title} = undef;
        bless $self, $class;
        return $self;
    #accessor method for  id
    sub id {
        my ( $self, $id ) = @_;
        $self->{_id} = $id if defined($id);
        return ( $self->{_id} );
    #accessor method for  title
    sub title {
        my ( $self, $title ) = @_;
        $self->{_title} = $title if defined($title);
        return ( $self->{_title} );
    sub print {
        my ($self) = @_;
        # we will call the print method of the parent class

    Looking at the code, you will notice that we have a new

    method and a print method. Both the child class and its

    parent class have the same method defined. We have

    overridden the parent class’ methods with the ones from

    the child. When those methods are called on an Employee

    object, we will get the Employee class’ version of the

    method. This concept of using the methods of an existing

    object and modifying them is known as polymorphism.

    Putting it together

    So now that we have a complete set of classes, we can

    write a small program to test them.

    use strict;
    use warnings;
    use diagnostics;
    use Employee;
    #create Employee class instance
    my $mike =  eval { new Employee(); }  or die ($@);
    #set object attributes
    $mike->title('Perl Programmer');
    $mike->address( new Address() );
    $mike->address->street('30 Hudson court');
    $mike->address->city('Jersey City');
    #diplay Employee info

    Let’s execute our code and see the output:

    $ ./test.pl

    Name:mike Weis

    Address:30 Hudson court

    Jersey City, NJ 665030

  • Perl File Operations

    Perl File operations

    Variables which represent files are called “file handles”, and they are handled differently from other variables. They do not begin with any special character — they are just plain words. By convention, file handle variables are written in all upper case, like FILE_OUT or SOCK. The file handles are all in a global namespace, so you cannot allocate them locally like other variables. File handles can be passed from one routine to another like strings (detailed below).

    The standard file handles STDIN, STDOUT, and STDERR are automatically opened before the program runs. Surrounding a file handle with <> is an expression that returns one line from the file including the “n” character, so returns one line from standard input. The <> operator returns undef when there is no more input. The “chop” operator removes the last character from a string, so it can be used just after an input operation to remove the trailing “n”. The “chomp” operator is similar, but only removes the character if it is the end-of-line character.

    $line = ; ## read one line from the STDIN file handle 
    chomp($line); ## remove the trailing "n" if present 

    File Open and Close

    The “open” and “close” operators operate as in C to connect a file handle to a filename in the file system.

    open(F1, "filename"); ## open "filename" for reading as file handle F1 
    open(F2, ">filename"); ## open "filename" for writing as file handle F2 
    open(F3, ">>appendtome") ## open "appendtome" for appending 
    close(F1); ## close a file handle 

    Open can also be used to establish a reading or writing connection to a separate process launched by the OS. This works best on Unix.

    open(F4, "ls -l |"); ## open a pipe to read from an ls process 
    open(F5, "| mail $addr"); ## open a pipe to write to a mail process 

    Passing commands to the shell to launch an OS process in this way can be very convenient, but it’s also a famous source of security problems in CGI programs. When writing a CGI, do not pass a string from the client side as a filename in a call to open().

    Open returns undef on failure, so the following phrase is often to exit if a file can’t be opened. The die operator prints an error message and terminates the program.

    open(FILE, $fname) || die "Could not open $fnamen"; 

    In this example, the logical-or operator || essentially builds an if statement, since it only evaluates the second expression if the first if false. This construct is a little strange, but it is a common code pattern for Perl error handling.

    Input Variants

    In a scalar context the input operator reads one line at a time. In an array context, the input operator reads the entire file into memory as an array of its lines… @a = ; ## read the whole file in as an array of lines

    This syntax can be dangerous. The following statement looks like it reads just a single line, but actually the left hand side is an array context, so it reads the whole file and then discards all but the first line….

    my($line) = ;

    The behavior of also depends on the special global variable $/ which is the current the end-of-line marker (usually “n”). Setting $/ to undef causes to read the whole file into a single string.

    $/ = undef; 
    $all = ; ## read the whole file into one string 

    You can remember that $/ is the end-of-line marker because “/” is used to designate separate lines of poetry. I thought this mnemonic was silly when I first saw it, but sure enough, I now remember that $/ is the end-of-line marker.

    Print Output

    Print takes a series of things to print separated by commas. By default, print writes to the STDOUT file handle.

    print "Woo Hoon"; ## print a string to STDOUT 
    $num = 42; 
    $str = " Hoo"; 
    print "Woo", $a, " bbb $num", "n"; ## print several things 

    An optional first argument to print can specify the destination file handle. There is no comma after the file handle, but I always forget to omit it.

    print FILE "Here", " there", " everywhere!", "n";  

    File Processing Example

    As an example, here’s some code that opens each of the files listed in the @ARGV array, and reads in and prints out their contents to standard output…

    #!/usr/bin/perl -w 
    require 5.004; 
    ## Open each command line file and print its contents to standard out 
    foreach $fname (@ARGV) { 
    open(FILE, $fname) || die("Could not open $fnamen"); 
    while($line = ) { 
    print $line; 

    The above uses “die” to abort the program if one of the files cannot be opened. We could use a more flexible strategy where we print an error message for that file but continue to try to process the other files. Alternately we could use the function call exit(-1) to exit the program with an error code. Also, the following shift pattern is a common alternative way to iterate through an array…

    while($fname = shift(@ARGV)) {...