Friday, August 1, 2014

Perl Modular Programming .. Tutorial and short notes

This post describes Perl Modules in short or kind of notes taken from "Advance Perl Programming" book.

PERL Modules

        * Modules are nothing but library packages, that can be
          reused efficiently and provides a means for Modular
          Programming

        * Perl uses keywork 'package' to define modules and loads
          predefined modules using 'use' and 'required' keywords.
          To access package specific variables and function it
          uses '::' notation.

        * required keyword is similar like C #include, which loads
          the perl file. e.g. required "myfunction.pl";

        * 'use' statement is executed as soon as it is parsed. So
          at compile time it is guarantee that all required modules
          have been successfully loaded at compile time.

        * Perl modules files using stored with extension '.pm'

Perl Package/Module example


simple example of perl package includes variable and function 
definitions. E.g. mypackage.pm

        #!/usr/bin/perl

        package mypackage; # same as filename

        # global variables
        my $space_count;

        sub readspaces
        {
                my ($string) = (@_);
                $space_count = map $_, $string =~ /\s/gs;
                return $space_count;
        }

        sub replacespaces
        {
                my ($string) = (@_);
                $string =~ s/\s/\#/g;
                return $string;
        }

        1; # to avoid error "mypackage.pm did not return a true value"
           # also when it is included with 'use' or 'required' module
           # is expected to return a boolen success value


Main package and variables

In absence of explicit package declaration, perl assumes a package 'main'. The built-in variables such as $!, $_, $@, @ARGV and %ENV are belongs to package main and these are only truly  global variables in Perl.

 

Symbolic References

Used more in Object orientation. Calling a function or variable using symbols.    E.g.  Calling function from package mypackage,
 
             $mypackage::myfunction(10,20);
    
The symbolic reference for this above function calling is:
           
             &{"mypackage::myfunction"}(10,20);


Package Initialization & Destruction

Use of BEGIN block does a initialization work for perl modules. The perl when it seens BEGIN block it executes right away and does further compilation later on. Simple requirement when we want to include a library before compilation, we can add below line in BEGIN block:

          sub BEGIN {
              use lib qw(../include); #prepends the directory to @INC
          }
 

Similarly END block is used to do clean-up before the program exits.

Enforcing privacy

Use 'my' operator at file scope.
          e.g.
                package A;
                my $a = 10;

                package B;
                print $A::a; # error, no such variable in package A
                print $a # print 10
 
Define a function with 'my'
          e.g.
                my $rs_func  = sub {
                        };
                #call as
                &$rs_func;

 

Importing Symbols

Creating namespaces and using function names or variables directly without using '::' operator. While defining the package use Exporter module insert all the package function names and variables to Exporter array. Which this inherits to the calling program. e.g.

          package BankAccount;
          use Exporter;
          @ISA = ('Exporter');
          @Exporter_OK = qw/withdraw deposite/; # or use below
          # Exporter_OK states only these symbols are ok to export
          @Exporter = qw/withdraw/; # this gets all the exported symbols

          sub withdraw { ... }
          sub deposite { ... }
          1;
 
          # myfile.pl
          #!/usr/bin/perl
          use BankAccount ('deposite'); # this is analogus to
             # BEGIN { require BankAccount;
             #         BankAccount::import('deposite');
             # }

 

Package nesting

Since all packages are global in scope nesting is not permitted. but one can use a directory, the '::' gets replaced with a filename separator. e.g.
 
          use Math::Poisson; # Math/Poisson.pm
          Math::Poisson::Calculate_Probability($lamda, $a);

 

Autoloading

If you invoke a function called Test::func(), for example, and if func() has not been defined in module Test, Perl automatically looks for a subroutine called Test::AUTOLOAD(). If such a subroutine exists, Perl calls it with the arguments
that were passed to func(). In addition, a variable called  $AUTOLOAD is set to the full name of the function that was  called ("Test::func").

Usefull example is calling system with linux command
        #-----------------------------------------------
        package Shell;
        #------------------------------------------
        sub AUTOLOAD {
            my($program) = $AUTOLOAD;
            # We are just interested in the command name, not in
            # the package name
            $program =~ s/^.*:://;
            system ("$program @_");
        }
 
        #------------------------------------------------
        use Shell;
        ls ('-lR'); # Triggers a call to AUTOLOAD since sub ls()
        # doesn't exist
        mail ('-s "This is a test" joe@foo.com < letter.txt');


Accessing Symbol table


Dumping a package symbol table using stashing the function  e.g. function name foo can be accessed by a hash %foo
                TODO
 


Questions on perl modules/packages


1. How do you make variables public, private, or protected in Perl?

        use 'my' for private and 'our' keyword for public
        use 'local' might bring protected version

2. use vs require keyword. How to create module

        Use and Require are most common methods used to load modules.
        Use:
          1) It is evaluated at compile time only.
          2) it is used only to load modules.
          3)No need to give file extensions when we are using "use"
        Require:
          1)This is evaluated at Run time
          2)This method is used for both libraries and modules.
          3)Need to give file extensions like pl,pm etc..

3. List the programming guidelines that must be followed when programming
   with Perl modules

        * The Module and the package name must be the same.
        * The only allowable package name needs to have the extension “.pm”
        * The package must be derived from the exporter class when no
          object-oriented technique is used


Friday, July 18, 2014

GDB Tutorial (In short)

Run GDB with argument
-----------------

gdb
..
(gdb) run arg1 arg2

Breakpoints
-----------
before run
(gdb) break
(gdb) break
(gdb) break e.g. break hello.c:5

To clear breakpoints use clear command or for breakpoint number
        with delete command 'enable' and 'disable' breakpoints

Examining variable
-----------------
Use 'display' command e.g. (gdb) disp var1
And 'undisplay' to forget variable display

Use 'print' command e.g. (gdb) print var1

Use 'printf' command e.g. (gdb) printf "%d\n", var1


Stack Manipulation
------------------
Use command 'backtrace' or 'bt' this will show you current function
        call stack

more info:

backtrace -- Print backtrace of all stack frames
bt         -- Print backtrace of all stack frames
down -- Select and print stack frame called by this one
frame -- Select and print a stack frame
return -- Make selected stack frame return to its caller
select-frame -- Select a stack frame without printing anything
up         -- Select and print stack frame that called this one
list           -- list the ten lines before previous ten line listing.
                                    list

Stepping methods
----------------
Use 'finish' command to exit from current function and return to 
        calling function
        Use 'stepi' to step for a single assembly instruction
        Use 'advance' to continue to specified location e.g. advance 
       

Use 'jump' and 'continue' with location specified

Changing variable and values at run time
----------------------------------------
Use 'set variable' e.g. (gdb) set (i = 20) or 
        (gdb) set variable i = 20

Hardware watchpoint
-------------------
Use 'watch' command , its a special breakpoints this will trigger 
        whenever expression changes.
e.g. (gdb) watch var1
shows 'old value' and 'new changed value'

Attach to a running process
---------------------------
Need process ID and then use 'attach' command with the PID 
        to attach and break the running program

GDB coredump
------------
set ulimit -c 'unlimited'
gdb -tui -c core
In TUI mode, you'll be greeted with a screen of information, 
        telling you why the program exited  ("signal 11, Segmentation fault"),
        and the highlight will be on the offending line. 
use bt 

Window functions
----------------
All with -tui option (terminal user interface)
       In TUI mode, you can get a list of existing windows with the 
       info win command. You can then change which window has
       focus with the focus (or fs) command. Focus takes either a 
       window name, or "prev" or "next" as an argument. 
       Valid window names are "SRC" (source window), "CMD" 
       (command window), "REGS" (registers window), and "ASM" 
       (assembly window). 
       See the next section for how to use these other windows.

Display Registers and Assembly
------------------------------

in TUI mode, use following commands
layout src    Standard layout—source on top, command 
                                window on the bottom
 
layout asm Just like the "src" layout, except it's an assembly 
                                window on top

layout split Three windows: source on top, assembly in the 
                                middle, and command at the bottom layout reg
                                Opens the register window on top of either source
                                or assembly, whichever was opened last
        
        tui reg general Show the general registers
 
tui reg float    Show the floating point registers

tui reg system    Show the "system" registers

tui reg next Show the next page of registers—this is important 
                                because there might be pages of 
        registers that aren't in the "general", "float", or "system" sets

Working with macro
--------------------
To see the definition of all macros use command 'info macro [-a]
e.g. (gdb) info macro MAXVAL
      (gdb) info macro __STDC__
macro define    -- Define a new C/C++ preprocessor macro

macro expand    -- Fully expand any C/C++ preprocessor macro
                                               invocations in EXPRESSION

macro expand-once   -- Expand C/C++ preprocessor macro
                                                invocations appearing directly in EXPRESSION

macro list     -- List all the macros defined using the `macro define'
                                                command

macro undef     -- Remove the definition of the C/C++ preprocessor
                                                macro with the given name

Specifying debugging targets
 --------------------------- 

It is possible to run GDB for different target architectures. Use following commands: 
         set architecture    
         show architecture 
         set processor
         processor

         target child        -- Win32 child process (started by the "run" command) 
         target core         -- Use a core file as a target
         target exec         -- Use an executable file as a target
         target extended-remote   -- Use a remote computer via a serial line
         target record     -- Log program while executing and replay execution 
                                        from log 
         target record-btrace    -- Collect control-flow trace and provide the 
                                                   execution history

TBD....







GCC tutorial (in short)

* GCC - GNU Compiler Collection

* Current version: 4.9.0 (for this document)

* Supporting languages: 
        C, C++, Fortran, Objective-C, Objective-C++, Java,  Ada and Go

* Front end compilation: 
       Part of compiler that is specific to programming languages is called front end

* GCC compiler names for particular languages:
C    - gcc
C++  - g++
Ada  - GNAT

* Supported C standard: Currently support three standards

  ANSI C - (ISO standard) C89 and C90 as defined in year 1989 (ANSI C) 
       and 1990 (ISO).
       
       > For selecting standards use options '-ansi, -std=c90 or -std=iso9890:1990'
       > To obtain all the diagnostics required by the standard use option '-pedantic' 
          or '-pedantic-error'
       > Amended standard (AMD1) - C94 and C95; use option '-std=c9899:199409'
>New edition of ISO std - C99; use option '-std=c99 or -std=iso9899:1999'
       >C11 - use option '-std=c11 or -std=iso9899:2011"

        By default GCC uses extensions to c standards and uses '-std=gnu90 or 
        -std=gnu99 or -std=gnu11' default is -std=gnu90

* Supported C++ standard:

> -ansi, -std=c++98, -std=c++03, -std=c++11
        >  By default g++ support '-std=gnu++98' option    

* GCC performs following steps after invoking

       >Preprocessing: option -E is used to stop after preprocessing stage
       >Compilation:    option -c is used to just compile the program, and 
                                 doesn't run the linker. The output are object files 
                                 by the assembler.
       >Assembly:        -S option is used to stop the compilation stage and 
                                should not assemble.
       >Linking

* Kind of output and compilation option (based on file name extensions)

file.c, c source code that must be preprocessed
     file.i, c source code that should not be preprocessed
     file.h, c header file to be turned into precompiled header
file.C, c source file that must be preprocessed
file.s, assembler code
file.S, assembler code that must be preprocessed

* Specifying input language

Use option -x to specify input language e.g.
        -x c : C programming (c-header cpp-output)
        -x c++ : C++ programming (c++-header, c++-cpp-output

* Common gcc command line options used

     -c : Compile or assemble the source file, but do not link. 
           The ultimate output is an object file
     
     -E : Stop after preprocessing stage, do not run compiler. 
           The output is in the form of preprocessed source code

     -o filename : Place output in a file
  
     -v : Print the compilation stages on standard output, kind of 
           verbose like output of compilation process.

    -### : Similar to '-v' but print only non-executing data like configure 
              options, compile path, library path etc

    -pipe : Use of pipes to communicate between various stages of compilation.
               Use for optimizing compile time where gcc creates lots of temporary 
               files and to avoid that pipes are used. (uses more RAM)

    -wrapper : Invoke all sub-commands under the wrapper program. e.g.
                     gcc -c a.c -wrapper gdb, --agrs
                     This invokes all subprograms of gcc under ‘gdb --args’, 
                     thus the invocation of cc1 is ‘
                     gdb --args cc1

    -fplugin=name.so : Load the plugin code in file name.so, shared object 
                                   is opened by dlopen by compiler 
                                 -fplugin-arg-name-key=value

     -fno-builtin
     -fno-builtin-function : Do not recognize build in function that do not begin 
                                        with '__builtin__' as a prefix
                                        e.g.
                                       #define abs(n) __builtin_abs ((n))
                                       #define strcpy(d, s) __builtin_strcpy ((d), (s))

     -g : Produces debugging information

     -ggdb : Produces debugging flags used by gdb debugger

* GCC diagnostic message formatting options

-fmessage-length=n : Format error message to n characters
-fdiagnostics-color[never|always|auto] : Use of color in diagnostic messages

* GCC options to request or suppress warnings

-fsyntax-only : Check the code for syntax errors

-fmax-errors=n : Limit the max error messages to n

-w : Inhibit all warnings

-Werror  : Make all warnings as errors

-Wfatal-errors : Abort compilation once a first error occurred

-Wpedantic
-pedantic   : Issue all warnings demanded by strict c standards

-Wall : Enables all the warnings

-Wextra : enables all extra warning flags like pointer comparison 
                  with int zero etc

-Wformat : Check calls to printf scanf formatting functions to make 
                     sure argument supplied have type appropriate formatting.

Avoid:

1. Grouping of multiple options e.g., -dv is different than -d -v 







Thursday, March 20, 2014

Tag list to efficiently browse source code files for different programming languages: vim plugin/ctags

Installing ctag utility


Download and install ctag utility (ctags-5.8.tar.gz ) from location: http://ctags.sourceforge.net/
Follow the instructions:
1.       Extract the tar contents by executing tar command
tar –xvzf ctags-5.8.tar.gz
2.       change directory to ctags-5.8 and execute following command to install ctags
./configure
make
make install
3.       If the ctag installation uses non-standard location, Setup .vimrc as per installed ctag binary path: set the following path variable in .vimrc
 let Tlist_Ctags_Cmd = "//bin/ctags"

Few tips to setup .vimrc for indentation, line numbers, syntax highlighting etc   

Example .vimrc
 set nocompatible | filetype indent plugin on | syn on |
 set nu
 set shiftwidth=4
 set tabstop=4
 let Tlist_Ctags_Cmd = "/opt/rhel/bin/ctags"
nnoremap   :TlistToggle

Installing vim-taglist plugin


Download and install taglist plugin for vim version 6.0 or above, download location: http://sourceforge.net/projects/vim-taglist/files/
Follow the instructions as:
1.       Copy the downloaded file to your home directory containing .vim directory
2.       Extract the zip contents
3.        The unzip contains plugin/taglist.vim file (main taglist plugin file) and doc/taglist.txt (documentation file)
4.       Open any source code file in vim and use ‘:TlistToggle’ to open-close tag list window.