Showing posts with label Understanding Perl. Show all posts
Showing posts with label Understanding Perl. Show all posts

Thursday, 27 September 2012

Linux: Convert File Types ( MIME)

Working on difference between .txt files :

vimdiff     httpserver_directory.txt Entire_productsxml.txt       

Commands used for Comparision:

1.vimdiff     httpserver_directory.txt  Entire_productsxml.txt   

 not  working as  MIME types are different. One is xml  and the other is text file.

2.I needed  iconv  command here  to conver the MIME types   from  text/xml   to ascii/xml
a. Do these files have  same character sets?

No.
Hint from google: http://www.google.co.in/url?sa=t&rct=j&q=iconv%3A%20conversion%20from%20%60text%2Fxml%27%20and%20to%20%60text%2Fplain%27%20are%20not%20supported%20%3F&source=web&cd=6&cad=rja&ved=0CEEQFjAF&url=http%3A%2F%2Funix.stackexchange.com%2Fquestions%2F11602%2Fhow-can-i-test-the-encoding-of-a-text-file-is-it-valid-and-what-is-it&ei=xvRjUK3_LoqHrAeD6IHYAQ&usg=AFQjCNELrZ1L4ptxCXQckmAXG67VBHdboQ
 file1: text/plain; charset=utf-8 file2: text/plain; charset=iso-8859-1

b.First see the character sets  of each file :
Command :

file -i *

 [root@blr-elorhel4x86 ndtrack]# file -i *

Entire_productsxml.txt:   text/plain; charset=us-ascii

Entire_productsxml.txt:   application/x-empty

httpserver_directory.txt:

httpsever_ouionly.txt:    text/plain; charset=us-ascii

ndtrack.ini:              text/plain; charset=utf-8

ndtrack.sh:               application/x-shellscript

run.sh:                   application/x-shellscript

c.To convert from one  format  to other format:
Command: I will use it later, because I need to  some   text parsing.

iconv -f FROM-ENCODING -t TO-ENCODING file.txt

d.How  am I doing text parsing?

I am using perl  command line to wipe off beginning spaces in the line to act on to file:

Re-learn : perl -nle '$_ =~ s/^(\w+)$/$&/g ; print  $_' Entire_productsxml.txt   

Beginning spaces in the line are not replaced. Simple, but I need to remember it.



Linux: Converting File Types ( MIME)


Working on difference between   files :
vimdiff     httpserver_directory.txt  Entire_productsxml.txt
Commands used for Comparision:
1.vimdiff     httpserver_directory.ndi  Entire_productsxml.txt   
 not  working as  MIME types are different. One is xml  and the other is text file.

2.I needed  iconv  command here  to conver the MIME types   from  text/xml   to ascii/xml
a. Do these files have  same character sets?
b.First see the character sets  of each file :
Command :
file -i *
 [root@blr-elorhel4x86 ndtrack]# file -i *
Entire_productsxml.ndi:   text/plain; charset=us-ascii
Entire_productsxml.txt:   application/x-empty
httpserver_directory.ndi:
httpsever_ouionly.ndi:    text/plain; charset=us-ascii
ndtrack.ini:              text/plain; charset=utf-8
ndtrack.sh:               application/x-shellscript
run.sh:                   application/x-shellscript
c.To convert from one  format  to other format:
Command: I will use it later, because I need to  some   text parsing.
iconv -f FROM-ENCODING -t TO-ENCODING file.txt
d.How  am I doing text parsing?
I am using perl  command line  to act on to file:
Re-learn : perl -nle '$_ =~ s/^(\w+)$/$&/g ; print  $_' Entire_productsxml.ndi    
Beginning spaces in the line are not replaced. Simple, but I don’t know.

Wednesday, 25 January 2012

Active perl installation on Windows and Unix

AIX
  • GNU tar is required to extract the ActivePerl tarball into the filesystem. A precompiled version of GNU tar for AIX 5L can be found here:
        http://www-03.ibm.com/systems/p/os/aix/linux/toolbox/download.html
    You can also get the source package for GNU tar from:
        http://www.gnu.org/
  • The reloc_perl script does not correctly update the perl-dynamic executable. The workaround is to install directly from the tarball into the new location.

Solaris

  • GNU tar is required to extract the ActivePerl tarball into the filesystem. A precompiled version of GNU tar for Solaris is available from:
        http://www.sunfreeware.com/
    You can also get the source package for GNU tar from:
        http://www.gnu.org/

Mac OS X

  • Perl library paths, and thus PPM Areas, defined in the shell by the PERL5LIB environment variable are not available to PPM when it is launched by clicking the PPM icon in OS X. To to make this variable available in the OS X GUI, add an entry similar to the following to ~/.MacOSX/environment.plist:
     <key>PERL5LIB</key>
     <string>/path/to/perl/lib</string>
  • Running sudo ppm or sudo ppm gui does not start up the GUI with root privileges. Use ppm as a command line tool with sudo.

Windows

  • The fork() emulation has known limitations. See the perlfork manpage for a detailed summary. In particular, fork() emulation will not work correctly with extensions that are either not thread-safe, or maintain internal state that cannot be cloned in the pseudo-child process. This caveat currently applies to extensions such as Tk and Storable.
  • It seems that some people are having problems with the ActivePerl MSI installer. The first thing to note is that you CANNOT install ActivePerl 5.14 over an older version of ActivePerl based on 5.12, 5.10, 5.8, 5.6 or 5.005, such as build 1204 or any other 1200, 1000, 800, 600 or 500 series build. We have determined that some of these problems are due to other installations of Perl that may be pointed at by something in the environment.
    The sure-fire solution is to make absolutely certain that no other installations of Perl are on the target machine. Realizing that this is not always possible, you can follow these steps to ensure the other installations will not interfere.
    1. Stop the "Windows Installer" service. This can be accomplished from the command prompt using the following command:
          c:\> net stop "Windows Installer"
    2. Temporarily remove or rename PERLLIB and PERL5LIB environment variables in the system environment.
    3. Temporarily remove or rename the following registry values:
          [\\HKEY_LOCAL_MACHINE\Software\Perl] lib = <directory> (REG_SV)
          [\\HKEY_LOCAL_MACHINE\Software\Perl] sitelib = <directory> (REG_SV)
          [\\HKEY_LOCAL_MACHINE\Software\Perl] lib-<PerlVersion> = <directory> (REG_SV)
          [\\HKEY_LOCAL_MACHINE\Software\Perl] sitelib-<PerlVersion> = <directory>(REG_SV)
    4. Proceed with the installation.
    Once the installation has completed successfully, the above actions may be undone although restoring the environment variables or the registry values may interfere with the proper operation of your new ActivePerl installation. In order to perform all of the above steps, you will need to have Administrative privileges on the target machine. If you do not have the required privileges you should contact you Administrator.

Further Information

The Perl distribution comes with extensive documentation. On Unix platforms, all the standard documentation is installed as man pages under the Perl install location. The location of the man pages may need to be added to the MANPATH environment variable in order to access them. For example, in the C shell:
    % setenv MANPATH /opt/ActivePerl-5.14/man:$MANPATH
The documentation is installed in HTML format on all platforms. If ActivePerl was installed in /opt/ActivePerl-5.14 then the HTML documentation would be located in /opt/ActivePerl-5.14/html.
On Windows, the standard documentation along with Windows-specific Perl documentation is installed in HTML format, and is accessible from the "Start" menu.
Updated versions of the HTML documentation will always be available at the ActiveState website:
    http://www.activestate.com/ActivePerl

Tuesday, 17 January 2012

OOPerl Terminology

In the object-oriented world, many words describe only a few concepts. If you've programmed in another object-oriented language, you might like to know how familiar terms and concepts map onto Perl.
For example, it's common to call objects instances of a class and those objects' methods instance methods . Data fields peculiar to each object are often called instance data or object attributes , and data fields common to all members of that class are class data , class attributes , or static data members .
Also, base class , generic class , and superclass all describe the same notion (a parent or similar ancestor in the inheritance hierarchy), whereas derived class , specific class , and subclass describe the opposite relationship (a child or descendent in the inheritance hierarchy).
C++ programmers have static methods , virtual methods , and instance methods , but Perl only has class methods and object methods . Actually, Perl only has methods. Whether a method acts as a class or object method is determined solely by actual usage. You could call a class method (one expecting a string argument) on an object (one expecting a reference), or vice versa, but you shouldn't expect reasonable results if you do.
A C++ programmer thinks about global (class) constructors and destructors. These correspond to module initialization code and per-module END{} blocks respectively.
From the C++ perspective, all methods in Perl are virtual. This is why their arguments are never checked for function prototypes as regular built-in and user-defined functions can be. Prototypes are checked by the compiler at compile time. You can't determine until run time the function that a method has called
In the object-oriented world, many words describe only a few concepts. If you've programmed in another object-oriented language, you might like to know how familiar terms and concepts map onto Perl.
For example, it's common to call objects instances of a class and those objects' methods instance methods . Data fields peculiar to each object are often called instance data or object attributes , and data fields common to all members of that class are class data , class attributes , or static data members .
Also, base class , generic class , and superclass all describe the same notion (a parent or similar ancestor in the inheritance hierarchy), whereas derived class , specific class , and subclass describe the opposite relationship (a child or descendent in the inheritance hierarchy).
C++ programmers have static methods , virtual methods , and instance methods , but Perl only has class methods and object methods . Actually, Perl only has methods. Whether a method acts as a class or object method is determined solely by actual usage. You could call a class method (one expecting a string argument) on an object (one expecting a reference), or vice versa, but you shouldn't expect reasonable results if you do.
A C++ programmer thinks about global (class) constructors and destructors. These correspond to module initialization code and per-module END{} blocks respectively.
From the C++ perspective, all methods in Perl are virtual. This is why their arguments are never checked for function prototypes as regular built-in and user-defined functions can be. Prototypes are checked by the compiler at compile time. You can't determine until run time the function that a method has called

OOPerl Basics

Thanks to Perl Cookbook  writers for wisdom sharing:

If you ask ten people what object orientation is, you'll get ten different answers. People bandy about terms like abstraction and encapsulation, trying to isolate the basic units of object-oriented programming languages and give them big names to write papers and books about. Not all object-oriented languages offer the same features, yet they are still deemed object-oriented. This, of course, produces more papers and books.
We'll follow the nomenclature used in Perl's documentation, the perlobj (1) manpage, and Chapter 5 of Programming Perl , "Libraries, Modules, and Classes." An object is a variable that belongs to a class . Methods are functions associated with a class or object. In Perl, a class is a package  - and usually a module. An object is a reference to something that's been blessed into a class. Blessing associates a referent with a class. This is done with the bless function, which takes one or two arguments. The first is a reference to the thing to bless, and the optional second argument is the package to bless it into.
$object = {};                       # hash reference
bless($object, "Data::Encoder");    # bless $object into Data::Encoder class
bless($object);                     # bless $object into current package
The class name is the package name ( Data::Encoder in the example above). Because classes are modules (usually), the code for the Data::Encoder class resides in the file Data/Encoder.pm . As with traditional modules, the directory structure is purely for convenience; it implies nothing about inheritance, variable sharing, or anything else. Unlike a traditional module, though, an object module seldom if ever uses the Exporter. Access should be through method calls only, not imported functions or variables.
Once an object has been blessed, calling the ref function on its reference returns the name of its class instead of the fundamental type of referent:
$obj = [3,5];
print ref($obj), " ", $obj->[1], "\n";
bless($obj, "Human::Cannibal");
print ref($obj), " ", $obj->[1], "\n";


ARRAY 5


Human::Cannibal 5
As you can see, you can still dereference a reference once it has been blessed. Most frequently, objects are implemented as blessed hash references. You may use any kind of reference you want, but hash references are the most flexible. They let you have arbitrarily named data fields in an object.
$obj->{Stomach} = "Empty";   # directly accessing an object's contents
$obj->{NAME}    = "Thag";        # uppercase field name to make it stand out (optional)
Although Perl permits it, it's considered poor form for any code outside the class to directly access the contents of an object. The point of objects, everyone agrees, is to give you a nominally opaque handle to something that you access through designated methods only. This lets the maintainer of the class change its implementation without needing to change all application code that uses the class.

Methods

To call a method, use -> . Here, we call the encode() method of $object with the argument "data" and store the return value in $encoded :
$encoded = $object->encode("data");
This is an object method , because we call the method on an object. We can also have class methods , methods called on class names.
$encoded = Data::Encoder->encode("data");
Invoking a method calls the function in the corresponding class, implicitly passing as the initial argument either a reference for object methods or a string for class methods. Recipe 13.7 shows how to make method calls where the method is determined at runtime.
Most classes provide constructor methods, which return new objects. Unlike some object-oriented languages, constructor methods in Perl are not specially named. In fact, you can name them anything you like. C++ programmers have a penchant for calling their constructors in Perl new . We recommend that you name your constructors whatever makes sense in the context of the problem you're solving. For example, constructors in the Tk extension to Perl are named after the widgets they create. A less common approach is to export a function with the same name as the class; see "Example: Overloaded StrNum Class " in Recipe 13.14 for an example.
A typical constructor looks like this:
sub new {
    my $class = shift;
    my $self  = {};         # allocate new hash for object
    bless($self, $class);
    return $self;
}
Call the constructor with:
$object = Class->new();
If there isn't any inheritance or other monkey business working behind the scenes, this is effectively the same as:
$object = Class::new("Class");
The new() function's first argument here is the class name to bless the new reference into. A constructor should pass that string as the second argument to bless() .
Recipe 13.1 also talks about functions that return blessed references. Constructors don't have to be class methods, and writing object methods that return new objects have a number of uses, as discussed in Recipe 13.6 .
A destructor is a subroutine that runs when an object's referent is garbage collected. Unlike constructors, you have no choice in naming it. You must name your destructor method DESTROY . This method, if it exists, will be called for all objects immediately prior to memory deallocation. Destructors, described in Recipe 13.2 , are optional.
Some languages syntactically allow the compiler to restrict access to a class's methods. Perl does not  - it allows code to call any method of an object. The author of a class should document clearly the public methods (those which may be used), and the user of a class should avoid undocumented (implicitly private ) methods.

For more:
http://docstore.mik.ua/orelly/perl/cookbook/ch13_01.htm 

Monday, 26 December 2011

Install perl module from CPAN

To install DBI module from CPAN site directly do this:

perl -MCPAN -e 'install Bundle::DBD::DB2 '

For more installation methods look into:

http://savage.net.au/Perl-modules/html/installing-a-module.html 

Perl DBI and DB2 modules installation on AIX 6.1

My AIX6.1 natively didnot have c compiler.So, I have downloaded xlc compiler to compile and install perl DBI and DB2 modules.
Also, DB2 module installation using perl5.8.8  failed with Usage: Cwd::fastcwd()  error.

# Here are the debugging steps I have performed to install DB2 module using perl 5.14.2 #
AIX Tools for linux:
http://www-03.ibm.com/systems/power/software/aix/linux/toolbox/ezinstall.html 

Install xlc_r compiler as follows:
http://www-01.ibm.com/software/awdtools/xlcpp/aix/   - xlcpp  compiler (aix c/c++ compiler)
xlcpp installation guide:https://www-304.ibm.com/support/docview.wss?uid=swg27017988&aid=1

Command to install:
installp -aXYgd ./xlcpp/usr/sys/inst.images -e /tmp/install.log all

export PATH=$PATH:/usr/vac/bin:/usr/vacpp/bin


## Install perl 5.14.2 in threaded 32-bit mode ##
####################################################
 rm config.sh
    ./Configure \
    -d \
    -Dcc=cc_r \
    -Duseshrplib \
    -Dusethreads \
    -Dprefix=/usr/opt/perl5_32
####################################################

Using [:upper:] and [:lower:] to convert case.
./Configure[3141]: cc_r:  not found.
You need to find a working C compiler.
Either (purchase and) install the C compiler supplied by your OS vendor,
or for a free C compiler try
http://gcc.gnu.org/
I cannot continue any further, aborting.
####################################################
Trouble shooting:
export PATH=$PATH:/usr/vac/bin  (for xlc_r compiler in PATH)

use -Dcc=xlc_r (xlc_r installed manually)
####################################################

####################################################
./Configure -d -Dcc=xlc_r -Duseshrplib -Dusethreads -Dprefix=/usr/opt/perl5_32

Looking for extensions...
A number of extensions are supplied with perl5.  You may choose to
compile these extensions for dynamic loading (the default), compile
them into the perl5 executable (static loading), or not include
them at all.  Answer "none" to include no extensions.
Note that DynaLoader is always built and need not be mentioned here.

perl5.14 dynamically loaded modules :
What extensions do you wish to load dynamically?
[B Compress/Raw/Bzip2 Compress/Raw/Zlib Cwd Data/Dumper Devel/DProf Devel/PPPort Devel/Peek Digest/MD5 Digest/SHA

Encode Fcntl File/Glob Filter/Util/Call Hash/Util Hash/Util/FieldHash I18N/Langinfo IO IPC/SysV List/Util
MIME/Base64 Math/BigInt/FastCalc NDBM_File ODBM_File Opcode POSIX PerlIO/encoding PerlIO/scalar PerlIO/via SDBM_File
Socket Storable Sys/Hostname Sys/Syslog Text/Soundex Tie/Hash/NamedCapture Time/HiRes Time/Piece Unicode/Collate
Unicode/Normalize XS/APItest XS/Typemap attributes mro re threads threads/shared]
What extensions do you wish to load statically? [none]

Stripping down cppstdin path name
End of configuration questions.

Stripping down executable paths...
Creating config.sh...
If you'd like to make any changes to the config.sh file before I begin
to configure things, do it as a shell escape now (e.g. !vi config.sh).


make
make test
make install


/usr/opt/perl5_32/lib/5.14.2/aix-thread-multi/Config.pm   file now contains  xlc_r and proper archlib path to perl
5.14.2.
####################################################

####################################################
bash-3.2# export PATH=/usr/opt/perl5_32/bin/:$PATH
echo $PATH
/usr/opt/perl5_32/bin/:/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin:/usr/java5/jre/bin:/usr/java5/bin:/usr/va

c/bin:/home/db2inst1/sqllib/bin:/home/db2inst1/sqllib/adm:/home/db2inst1/sqllib/misc
Install Perl DBI using /usr/opt/perl5_32/bin/perl Makefile.PL
make
make test
make install

Before Installing DBD::DB2 module:
do : 
bash-3.2# source /home/db2inst2/sqllib/db2profile
bash-3.2# export PATH=/usr/opt/perl5_32/bin/:$PATH  (inlcude perl5.14.2 binary first in the PATH)
bash-3.2# export DB2_HOME=/home/db2inst2/sqllib


Note that $DB2_HOME/include should contain db2 sqlclient related header files.

Now do:
bash-3.2# /usr/opt/perl5_32/bin/perl Makefile.PL

Configuring DBD::DB2...
Remember to actually read the README and CAVEATS files!

Using DB2 in "/home/db2inst2/sqllib"
System: perl5.014002 DBI1.616 aix app4 1 6 00f6fa664c00  aix-thread-multi dl_aix.xs
Compiler: xlc_r -q32 -O -D_ALL_SOURCE -D_ANSI_C_SOURCE -D_POSIX_SOURCE -qmaxmem=-1 -qnoansialias -DUSE_NATIVE_DLOPEN

-DNEED_PTHREAD_INIT -qlanglvl=extended -q32 -D_LARGE_FILES -qlonglong
Includes:  -I"/home/db2inst2/sqllib/include" -I"/usr/opt/perl5_32/lib/site_perl/5.14.2/aix-thread-multi/auto/DBI"

-I"/usr/opt/perl5_32/lib/5.14.2/aix-thread-multi/auto/DBI"
-I"/usr/opt/perl5_32/lib/site_perl/5.14.2/aix-thread-multi/auto/DBI"
Libraries: -L/home/db2inst2/sqllib/lib64 -ldb2

Checking if your kit is complete...
Looks good
Writing Makefile for DBD::DB2::Constants
Writing MYMETA.yml
Writing Makefile for DBD::DB2
Writing MYMETA.yml


make:
bash-3.2# make
bash-3.2# make test
bash-3.2# make install
Files found in blib/arch: installing files in blib/lib into architecture dependent library tree
Installing /usr/opt/perl5_32/lib/site_perl/5.14.2/aix-thread-multi/auto/DBD/DB2/DB2.bs
Installing /usr/opt/perl5_32/lib/site_perl/5.14.2/aix-thread-multi/auto/DBD/DB2/DB2.so
Installing /usr/opt/perl5_32/lib/site_perl/5.14.2/aix-thread-multi/auto/DBD/DB2/Constants/Constants.bs
Installing /usr/opt/perl5_32/lib/site_perl/5.14.2/aix-thread-multi/auto/DBD/DB2/Constants/Constants.so
Installing /usr/opt/perl5_32/lib/site_perl/5.14.2/aix-thread-multi/Bundle/DBD/DB2.pm
Installing /usr/opt/perl5_32/lib/site_perl/5.14.2/aix-thread-multi/DBD/DB2.pm
Installing /usr/opt/perl5_32/lib/site_perl/5.14.2/aix-thread-multi/DBD/DB2.pod
Installing /usr/opt/perl5_32/lib/site_perl/5.14.2/aix-thread-multi/DBD/DB2/Constants.pm
Installing /usr/opt/perl5_32/lib/site_perl/5.14.2/aix-thread-multi/auto/DBD/DB2/Constants/autosplit.ix
Installing /usr/opt/perl5_32/man/man3/Bundle::DBD::DB2.3
Installing /usr/opt/perl5_32/man/man3/DBD::DB2.3
Appending installation info to /usr/opt/perl5_32/lib/5.14.2/aix-thread-multi/perllocal.pod

####################################################
Now, DB2 module is ready for connecting perl scripts to db2 database.

Thursday, 17 November 2011

Download and Install RHEL packages

Instead of installing individual rpms which take a long time, there is a command called yum which installs the package and it's dependencies at one go.

yum is the most commonly used command on Linux.

Task: Display list of updated software (security fix)

Type the following command at shell prompt:
# yum list updates
Task: Patch up system by applying all updates

To download and install all updates type the following command:
# yum update
Task: List all installed packages

List all installed packages, enter:
# rpm -qa
# yum list installed
Find out if httpd package installed or not, enter:
# rpm -qa | grep httpd*
# yum list installed httpd
Task: Check for and update specified packages

# yum update {package-name-1}
To check for and update httpd package, enter:
# yum update httpd
Task: Search for packages by name

Search httpd and all matching perl packages, enter:
# yum list {package-name}
# yum list {regex}
# yum list httpd
# yum list perl*
Sample output:

Loading "installonlyn" plugin
Loading "security" plugin
Setting up repositories
Reading repository metadata in from local files
Installed Packages
perl.i386                                4:5.8.8-10.el5_0.2     installed
perl-Archive-Tar.noarch                  1.30-1.fc6             installed
perl-BSD-Resource.i386                   1.28-1.fc6.1           installed
perl-Compress-Zlib.i386                  1.42-1.fc6             installed
perl-DBD-MySQL.i386                      3.0007-1.fc6           installed
perl-DBI.i386                            1.52-1.fc6             installed
perl-Digest-HMAC.noarch                  1.01-15                installed
perl-Digest-SHA1.i386                    2.11-1.2.1             installed
perl-HTML-Parser.i386                    3.55-1.fc6             installed
.....
.......
..
perl-libxml-perl.noarch                  0.08-1.2.1             base
perl-suidperl.i386                       4:5.8.8-10.el5_0.2     updates

Task: Install the specified packages [ RPM(s) ]

Install package called httpd:
# yum install {package-name-1} {package-name-2}
# yum install httpd
Task: Remove / Uninstall the specified packages [ RPM(s) ]

Remove package called httpd, enter:
# yum remove {package-name-1} {package-name-2}
# yum remove httpd
Task: Display the list of available packages

# yum list all
Task: Display list of group software

Type the following command:
# yum grouplist
Output:

Installed Groups:
   Engineering and Scientific
   MySQL Database
   Editors
   System Tools
   Text-based Internet
   Legacy Network Server
   DNS Name Server
   Dialup Networking Support
   FTP Server
   Network Servers
   Legacy Software Development
   Legacy Software Support
   Development Libraries
   Graphics
   Web Server
   Ruby
   Printing Support
   Mail Server
   Server Configuration Tools
   PostgreSQL Database
Available Groups:
   Office/Productivity
   Administration Tools
   Beagle
   Development Tools
   GNOME Software Development
   X Software Development
   Virtualization
   GNOME Desktop Environment
   Authoring and Publishing
   Mono
   Games and Entertainment
   XFCE-4.4
   Tomboy
   Java
   Java Development
   Emacs
   X Window System
   Windows File Server
   KDE Software Development
   KDE (K Desktop Environment)
   Horde
   Sound and Video
   FreeNX and NX
   News Server
   Yum Utilities
   Graphical Internet
Done

Task: Install all the default packages by group

Install all 'Development Tools' group packages, enter:
# yum groupinstall "Development Tools"
Task: Update all the default packages by group

Update all 'Development Tools' group packages, enter:
# yum groupupdate "Development Tools"
Task: Remove all packages in a group

Remove all 'Development Tools' group packages, enter:
# yum groupremove "Development Tools"
Task: Install particular architecture package

If you are using 64 bit RHEL version it is possible to install 32 packages:
# yum install {package-name}.{architecture}
# yum install mysql.i386
Task: Display packages not installed via official RHN subscribed repos

Show all packages not available via subscribed channels or repositories i.e show packages installed via other repos:
# yum list extras
Sample output:

Loading "installonlyn" plugin
Loading "security" plugin
Setting up repositories
Reading repository metadata in from local files
Extra Packages
DenyHosts.noarch                         2.6-python2.4          installed
VMwareTools.i386                         6532-44356             installed
john.i386                                1.7.0.2-3.el5.rf       installed
kernel.i686                              2.6.18-8.1.15.el5      installed
kernel-devel.i686                        2.6.18-8.1.15.el5      installed
lighttpd.i386                            1.4.18-1.el5.rf        installed
lighttpd-fastcgi.i386                    1.4.18-1.el5.rf        installed
psad.i386                                2.1-1                  installed
rssh.i386                                2.3.2-1.2.el5.rf       installed

Task: Display what package provides the file

You can easily find out what RPM package provides the file. For example find out what provides the /etc/passwd file:
# yum whatprovides /etc/passwd
Sample output:

Loading "installonlyn" plugin
Loading "security" plugin
Setting up repositories
Reading repository metadata in from local files
setup.noarch                             2.5.58-1.el5           base
Matched from:
/etc/passwd
setup.noarch                             2.5.58-1.el5           installed
Matched from:
/etc/passwd

You can use same command to list packages that satisfy dependencies:
# yum whatprovides {dependency-1} {dependency-2}
Refer yum command man page for more information:
# man yum














Friday, 19 August 2011

Difference between my $var and $main::var

#!/usr/bin/perl
use strict; 
use warnings; 
use diagnostics; package main; my $foo = "hello"; $main::foo = "world"; print "$foo $main::foo\n";
my $foo under the package main would manipulate the symbol table such that $foo were equal to $main::foo.
If $foo and $main::foo were the same variable, then the program would print world world,which it does not.
If you fully qualify your variables in package main, then you don't need to worry with declaring them with my, nor with our, nor with use vars, as can also be seen in the program above (see that $main::foo is not declared; yet, the compiler doesn't complain about it, even though the program is running under use strict).

Thursday, 18 August 2011

getopts for long options in perl

Sample Program i wrote:

use Getopt::Long;
my $delay;
my $action_script;
my $action_task;
my $customer_id;
my $resource_id;
my $event_state;
my $situation_name;
my $NOARGS=78;

 GetOptions (
            "d|delay=i"         => \$delay,
            "scriptname=s"     => \$action_script,
            "a|action=i"       => \$action_task,
            "c|cus|customer=s"  => \$customer_id,
            "r|resource=s"      => \$resource_id,
            "s|state=s"         => \$event_state,
            "sit|situation=s"   => \$situation_name
           );

if(!(defined $delay) || !(defined $action_script) || !(defined $action_task) || !(defined $customer_id) || !(defined $resource_id) || !(defined $event_state) || !(defined $situation_name))
{
    usage();
    exit $NOARGS;

}
 
sub usage
{
   print("usage : $0 -delay [delay]-scriptname [action_script] -action [action_task]  -customer [customer_id]  -resource [resource_id] -state [event_state] -situation [situation_name]\n") ;

}

waitpid($childpid,0)

process is running: waitpid returns 0; $? is -1
process is exiting: waitpid returns pid; $? is actual exit code
process doesn't exist: waitpid returns -1; $? is -1

so we need $? when waitpid returns childpid
we need to grab $? separately from $ret=waitpid($childpid,0)
$ret will return 0, pid or -1

Passing Files as Arguments

The Glob method of passing files is very Perlistic, and as such appears incredibly inobvious to general purpose programmers not using Perl on a regular basis. The Glob method is useful when retrofitting file passing in programs using Perl's syntax. If you're starting fresh, consider filehandles.

Here's the Glob method:

sub printFile($)
{
my $fileHandle = $_[0];
while (<$fileHandle>)
{
my $line = $_;
chomp($line);
print "$line\n";
}
}

open(MYINPUTFILE,"<filename");

printFile(\*MYINPUTFILE);
close(MYINPUTFILE);


Output files work similarly.

If you need to assign the glob to an actual variable, you can do that also. The code in the subroutine remains the same, and the following is the code doing the passing:

open(MYINPUTFILE, "<filename");
my $fileGlob = \*MYINPUTFILE;
printFile($fileGlob);
close(MYINPUTFILE);

IPC

Another approach to IPC is to make your program talk to itself, in a manner of speaking. Actually, your process talks over pipes to a forked copy of itself. It works much like the piped open we talked about in the last section, except that the child process continues executing your script instead of some other command.
To represent this to the open function, you use a pseudocommand consisting of a minus. So the second argument to open looks like either "-|" or "|-", depending on whether you want to pipe from yourself or to yourself. As with an ordinary fork command, the open function returns the child's process ID in the parent process but 0 in the child process. Another asymmetry is that the filehandle named by the open is used only in the parent process. The child's end of the pipe is hooked to either STDIN or STDOUT as appropriate. That is, if you open a pipe to minus with |-, you can write to the filehandle you opened, and your kid will find this in STDIN:
if (open(TO, "|-")) { print TO $fromparent; } else { $tochild = <STDIN>; exit; }
< If you open a pipe from minus with -|, you can read from the filehandle you opened, which will return whatever your kid writes to STDOUT:
if (open(FROM, "-|")) { $toparent = <FROM>; } else { print STDOUT $fromchild; exit; }
One common application of this construct is to bypass the shell when you want to open a pipe from a command. You might want to do this because you don't want the shell to interpret any possible metacharacters in the filenames you're trying to pass to the command. If you're running release 5.6.1 or greater of Perl, you can use the multi-argument form of open to get the same result.

Tuesday, 9 August 2011

Handling timeouts in perl scripts

Reference link: http://larsmichelsen.com/perl/handing-timeouts-in-perl-scripts/



These timeouts can occur for several reasons. Mostly some network connections via SSH, SNMP or HTTP-Gets which run longer than the default check timeout in Nagios. I usually use a global check timeout of 15 seconds in Nagios. This means a check script which runs more than 15 seconds will be killed. In this case Nagios returns a CRITICAL state with a “(Service check timed out)” message. This is bad cause I don’t want those timeouts to result in CRITICAL state and some CRITICAL notifications.

Now read how to catch such timeouts…

I use the alarm(13) function in my Perl script. This functions sets a timer to the given number of seconds – in this case 13 seconds. When the timer reached zero it will throw a SIGALRM. Thankfully you can customize the event on SIGALRM. This gives my script the possibility to print some output and exit the script regular with a UNKNOWN exit code.



Here is my snippet to catch timeouts in perl scripts:



# Handle SIGALRM (timeout triggered by alarm() call)

$SIG{ALRM} = sub {

print "UNKNOWN: Script timed out\n";

exit -1;

};



# Start the timer to script timeout

alarm(13);



Other good source: http://www.trevorbowen.com/tag/perlstuff/

The Perl system() function is very powerful and elegant. It forks a child process, suspends the parent process, executes the provided command (potentially calling your shell to help parse the arguments), while seamlessly redirecting all IO between the parent and child process! The usage is simple, but what happens behind the scenes is amazing!

Unfortunately, there is no way to interrupt the system() function in Perl. Sure, you can kill the main Perl program, but that’s not what I want. I want to call system() with 2 additional arguments: timeout and maxattempts. This subroutine would operate just like the traditional system() function, unless operation time exceeded the timeout value, in which case, the command would be killed and restarted, until the maximum number of attempts was exceeded.

You can find many resources that detail how to timeout a long Perl operation, like so:

eval {

local $SIG{ALRM} = sub { die "alarm clock restart" };

alarm 10;

flock(FH, 2); # blocking write lock

alarm 0;

};

if ($@ and $@ !~ /alarm clock restart/) { die }

Unfortunately, there is a little footnote that says you should not try this with system calls; otherwise, you get zombies. Sure enough, if you substitute a system() function for the above flock, the parent Perl script is alarmed by the timeout and exits the eval. Normally, this would kill the flock or any other function. But the system function persits. The parent may even complete the remainder of its program and exit, but the child will keep on ticking – not what I wanted. The second problem is that there is no way to get, or access the process id of the command executed by the system() function; therefore, there is no way to kill a system function call by the parent Perl process – at least, no way that I have found.

The above link suggests using fork and exec to create your own function, which is ultimately what I did. So, let’s jump straight to the chase scene, shall we? Here’s my final solution.

Preferred Solution

#!/usr/bin/perl -w

use strict 'refs';

use POSIX "sys_wait_h";



sub timedSystemCall {



my ($cmd, $timeout, $maxattempts, $attempt, $origmax) = @_;



# degenerate into system() call - infinite timeout, if timeout is undefined or negative

$timeout = 0 unless defined($timeout) && ($timeout > 0);

# degenerate into system() call - 1 attempt, if max attempts is undefined or negative

$maxattempts = 1 unless defined($maxattempts) && ($maxattempts > 0);

$attempt = 1 unless defined($attempt) && ($attempt > 0);

$origmax = $maxattempts unless defined $origmax;



local ($rc, $pid);



eval {

local $SIG{ALRM} = sub { die "TIMEOUT" };



# Fork child, system process

FORK: {

if ($pid = fork) {

# parent picks up here, with $pid = process id of child; however...

# NO-OP - Parent does nothing in this case, except avoid branches below...

} elsif (defined $pid) { # $pid is zero if here defined

# child process picks up here - parent process available with getppid()

# execute provided command, or die if fails

exec($cmd) || die("(E) timedSystemCall: Couldn't run $cmd: $!\n");

# child never progresses past here, because of (exec-or-die) combo

} elsif ($! =~ /No more processes/) {

# Still in parent: EAGAIN, supposedly recoverable fork error

print STDERR "(W) timedSystemCall: fork failed. Retrying in 5-seconds. ($!)\n";

sleep 5;

redo FORK;

} else {

# unknown fork error

die "(E) timedSystemCall: Cannot fork: $!\n";

}

}



# set alarm to go off in "timeout" seconds, and call $SIG{ALRM} at that time

alarm($timeout);

# hang (block) until program is finished

waitpid($pid, 0);



# program is finished - disable alarm

alarm(0);

# grab output of waitpid

$rc = $?;

}; # end of eval



# Did eval exit from an alarm timeout?

if (($@ =~ "^TIMEOUT") || !defined($rc)) {

# Yes - kill process

kill(KILL => $pid) || die "Unable to kill $pid - $!";

# Collect child's remains

($ret = waitpid($pid,0)) || die "Unable to reap child $pid (ret=$ret) - $!";

# grab exit output of child process

if ($rc = $?) {

# exit code is lower byte: shift out exit code, leave top byte in $rc

my $exit_value = $rc >> 8;

# killing signal is lower 7-bits of top byte, which was shifted to lower byte

my $signal_num = $rc & 127;

# core-dump flag is top bit

my $dumped_core = $rc & 128;

# Notify grandparent of obituary

print STDERR "(I) timedSystemCall: Child $pid obituary: exit=$exit_value, kill_signal=$signal_num, dumped_core=$dumped_core\n";

}

# Can we try again?

if ($maxattempts > 1) {

# Yes! Increment counter, for print messages

$attempt++;

print STDERR "(W) timedSystemCall: Command timed-out after $timeout seconds. Restarting ($attempt of $origmax)...\n";

# Recurse into self, while decrementing number of attempts. Return value from deepest recursion

return timedSystemCall($cmd, $timeout, $maxattempts-1, $attempt, $origmax);

} else {

# No! Out of attempts...

print STDERR "(E) timedSystemCall: Exhausted maximum attempts ($origmax) for command: $cmd\nExiting!\n";

# Return error code of killed process - will require interpretation by parent

return $rc;

}

} else {

# No - process completed successfully! Hooray!!! Return success code (should be zero).

return $rc;

}

}



exit timedSystemCall("inf.pl", 5, 3);


Tweets by @sriramperumalla