Wednesday, July 31, 2013

Writing distributive Perl Modules - Binary/Decimal Converter

You can write a Perl script in a single file. If your script is very simple and it doesn't have any re-usable code, then it is the right choice. But if you have some piece of code, which can be re-used in other scripts or by other persons and still it is single file, then it is called a naive script.

Write the reusable code as modules and 'use' that module in your script and encourage others to use it. In this post I'll demonstrate how to create Perl module with a simple example and how to package it, such a way that, it can be installed like any CPAN perl modules.

Problem Statement:
Write a script which either
  1) Accepts a Binary number and converts it into Decimal number
  2) Accepts a Decimal number and converts it into Binary number

Write Decimal and Binary converter functions in a separate module and 'use' it in a controller script.

Know-how: Perl modules
*) How to create Perl modules? Learn about EXPORT, EXPORT_OK, etc

Sample invocation
$ ./converter.pl
         Welcome to Slice Off Codes number converter
         A Utility to convert Binary to Decimal number
                and Decimal to Binary number


Press 1:  to select Binary to Decimal Converter
Press 2:  to select Decimal to Binary Converter
Press 3:  to quit
Choice : 1

Enter Binary number
101
Decimal value of Binary number 101 = 5

Press 1:  to select Binary to Decimal Converter
Press 2:  to select Decimal to Binary Converter
Press 3:  to quit
Choice : 2

Enter Decimal number
6
Binary value of Decimal number 6 = 110

Press 1:  to select Binary to Decimal Converter
Press 2:  to select Decimal to Binary Converter
Press 3:  to quit

Choice : 3

Source code

Saturday, July 27, 2013

Illustrating Perl threads with simple Maths script

Why do we use threads? 
   One of the reason is to perform tasks in parallel and get better turn-around-time.
   Also a server program which accepts data from user's and assign the task to a thread. Once the task is assigned, server goes back to accept new requests.

Problem statement
Write a script which accepts 2 numbers from user and performs addition, subtraction, multiplication and division operations on it in parallel. The main scripts performs all the 4 arithmetic operations using threads and it returns back waiting for next input.

Sample invocation
   ./math2.pl
Output
Welcome to Slice Off Codes Math Calculator
Enter Number 1
89
Enter Number 2
37
soc_add thread started ...
soc_sub thread started ...
soc_mul thread started ...
soc_div thread started ...
Main thread ....
Press 1 to Continue
Press 2 to Quit
89 / 37 = 2.40540540540541
89 - 37 = 52
89 * 37 = 3293
89 + 37 = 126

Know-how: Threads
use threads;  #Perl thread module
#Creating threads
my $thr1 = threads->create(\&soc_add, $num1, $num2);
# Collecting return data from threads. It also waits for thread to exit
$my @return_data=$thr1->join();
#Don't wait for thread return, don't have return data, just detach
$thr1->detach();

Source code
Method 1: Using detach