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



$ cat SOC/Math.pm
package SOC::Math;

# Copyright 2013, Siddesh BG (siddesh.bg@gmail.com) .
# This library is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.

use strict;
use POSIX;  # For floor function

# use the Exporter module to export our functions from the SOC::Math namespace
# into the main:: namespace to make them available to scripts that call 'use SOC::Math'

use Exporter;

#pacify strict with the use vars declaration of some variables
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
my (@binary); #Global variable

$VERSION     = 1.00;
@ISA         = qw(Exporter);
@EXPORT      = ();  #list of functions that we export by default, in this case nothing
@EXPORT_OK   = qw(binary2decimal decimal2binary); #list of functions that we export on demand

#we define two sets of export tags. This hash stores labels pointing to
#anonymous array references
%EXPORT_TAGS = ( DEFAULT => [qw(&binary2decimal)],
                ALL    => [qw(&binary2decimal &decimal2binary)]);

#Module to convert a binary number to decimal
sub binary2decimal {
    my ($number)=@_;
    my $result=0;

    #Convert number to array
    my @numberList = ( $number =~ m/./g );

    #Staring index
    my $index=$#numberList;

    foreach my $num ( @numberList) {
        $result=$result + ( $num * (2 ** $index) );
        $index--;
    }
    return $result;
}

sub decimal2binary {
    my ($number)=@_;
    my ($rem,$quot,$binary_string);
    $rem= $number%2;
    unshift(@binary,$rem);
    $quot=floor($number/2);
    if ($quot == 1) {
        unshift(@binary, "1");
        $binary_string=join("",@binary);
        @binary=();
        return($binary_string);
    } else {
        decimal2binary($quot);
    }
}

1;

$ cat converter.pl
#!/usr/bin/perl -w
use strict;
use SOC::Math qw(:ALL);

#add directory containing your module to the module search path in @INC

BEGIN {
    push @INC, '/home/guruss1/perl'
}

sub get_input {
    my ($type) = @_;
    print "\nEnter $type number \n";
    chomp(my $input=<STDIN>);
    return($input);
}

sub binary_converter {
    my $input=get_input("Binary");
    my $output=binary2decimal($input); #This function is coming from SOC::Math module
    print "Decimal value of Binary number $input = $output \n";

}

sub decimal_converter {
    my $input=get_input("Decimal");
    my $output=decimal2binary($input); #This function is coming from SOC::Math module
    print "Binary value of Decimal number $input = $output \n";
}


### MAIN CODE ####
print "\t Welcome to Slice Off Codes number converter\n";
print "\t A Utility to convert Binary to Decimal number\n";
print "\t\tand Decimal to Binary number\n\n";

while (1) {
    print "\nPress 1:  to select Binary to Decimal Converter\n";
    print "Press 2:  to select Decimal to Binary Converter\n";
    print "Press 3:  to quit\n";
    print "Choice : ";
    chomp(my $choice=<STDIN>);
    if ( $choice == 1) {
        binary_converter;
    } elsif($choice == 2) {
        decimal_converter;
    } elsif($choice == 3) {
        exit(0);
    } else {
        print "[WARNING] Wrong choice. Please enter a valide choice \n";
    }
}


No comments:

Post a Comment