Monday 8 December 2014

Perl Important Tips

Perl  helpfull Trics/Tips:


  1. Get all the Perl module installed via ubuntu/Linux command line

perl -MFile::Find=find -MFile::Spec::Functions -Tlw -e 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC'File::Find and File::Spec::Functions module are used to list all installed modules.
  • -M option loads the module. It executes use module before executing the script
  • -T option enables taint checking, which instructs perl to keep track of data from the user and avoid doing anything insecure with it. Here this option is used to avoid taking the current directory name from the @INC variable and listing the available .pm files from the directory recursively.
  • -l option enables automatic line-ending processing in the output. Print statements will have the new line separator (\n) added at the end of each line.
  • -w option prints any warning messages.
  • -e option indicates that the following string is to be interpreted as a perl script (i.e., sequence of commands).
More simpler Way :

>find / -name \*.pm -type f
to know the pakage related to INC :

find `perl -e 'map {print "$_ "} @INC'` -name \*.pm -type f




2. View Perl Documentation From Unix Command Line

You can use either perldoc or man command to get help about a particular perl module as shown below.
$ perldoc Regexp::Common






(or)

$ man Regexp::Common
If the perl document is not enough, use perldoc option -m,  to view both source code and unformatted pod documentation of the specified perl module.
3.Verify Perl Module Is installed or not ?
Syntax: Perl -M"Module name" 
if it gives not Output i.e indicated that the module is installed, else it show an error stating the module not installed.

4.The flip-flop operator is useful for skipping the first iteration when looping through the records (usually lines) returned by a file handle, without using a flag variable:
while(<$fh>)
{
  next if 1..1; # skip first record
  ...
}

5.The null filehandle diamond operator <> has its place in building command line tools. It acts like <FH>to read from a handle, except that it magically selects whichever is found first: command line filenames or STDIN. Taken from perlop:

while (<>) {
...   # code for each line
}

6.Map 
The map built-in function is one of the most useful tools in your toolkit. map takes a list and applies a code block to every element, returning the list. You can think of it as stream processing: you push the list in one side and get it back on the other side with some transformation applied. Inside the code block, you refer to the current element with the traditional "$_" variable.
Example below demonstrate the function for perl code using map to conver the Uppercase .
my @l = qw(perl php python);
my @uc_l = map { uc($_) } @l;
# Result: PERL PHP PYTHON
7. ||= operator
In Perl, you can use the ||= operator. Its precedence rules are such that it’ll only do an      assignment if the value is false (‘false’ generally means zero, undefined, or the empty string):
$a ||= 5;

Thursday 13 November 2014

Perl interview questions

  1. -c in perl shebang line
  2. # significance in pelr shebacnk line
  3. use strict usage and definition
  4. diff between chop/chomp
  5. . in regex in perl
  6. is perl greedy ? justify
  7. sort an array
  8. uniq element in array ?
  9. sort hash
  10. Difference between referencing /dereferncing in perl
  11. passing hashes in perl using subroutine
  12. what are hashes of hashes
  13. DB connection statement(fetch data) in perl
  14. difference bw fetchrow_array/fetchrow_arrayref/fetchrow_hashref
  15. <> ?
  16. modes in files handling in perl .

Thursday 22 May 2014

Configuring Tomcat for CGI Perl

Configuring CGI  Perl in Apache Tomcat :

Software Requirement:
  • Apache Tomcat 7.0.11 or above
  • JDK 1.6 or above 
  • Perl 5 or above
  1. Download Tomcat. Install or unzip it to some directory.
  2. Modify <TOMCAT_HOME>/conf/web.xml to uncomment the cgi servlet and its mapping
       uncomment:
 
<servlet>
 <servlet-name>cgi</servlet-name>
 <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
 <init-param>
 <param-name>debug</param-name>
 <param-value>0</param-value>
 </init-param>
 <init-param>
 <param-name>cgiPathPrefix</param-name>
 <param-value>WEB-INF/cgi</param-value>
 </init-param>
 <init-param>
 
add the following:
<init-param>
 <param-name>passShellEnvironment</param-name>
 <param-value>true</param-value>
 </init-param>
 
now the CGI blocks look like:
 
 
<servlet>
 <servlet-name>cgi</servlet-name>
 <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
 <init-param>
 <param-name>debug</param-name>
 <param-value>0</param-value>
 </init-param>
 <init-param>
 <param-name>cgiPathPrefix</param-name>
 <param-value>WEB-INF/cgi</param-value>
 </init-param>
 <init-param>
 <param-name>passShellEnvironment</param-name>
 <param-value>true</param-value>
 </init-param>
 <load-on-startup>5</load-on-startup>
</servlet>
 
3: Modify the servlet-mapping 
 
<servlet-mapping>
 <servlet-name>cgi</servlet-name>
 <url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>
 
 
4:Modify <TOMCAT_HOME>\conf\context.xml to add a property on <Context>: 
 
<Context privileged="true">
...
</Context>
 
5.Create a cgi folder under <TOMCAT_HOME>/webapps/ROOT/WEB-INF/
 
mkdir <TOMCAT_HOME>/webapps/ROOT/WEB-INF/cgi
 
6. Create the CGI or Pl file under the same directory 
 
 <TOMCAT_HOME>/webapps/ROOT/WEB-INF/cgi/test.cgi
 
#!C:\Perl\bin\perl.exe
# filename: test1.cgi
print "Content-type: text/html\n\n";
print "Hello, world!\n"
 
7. Restart the Apache server
 
 
  
 
 
 
 
 
 


Thursday 15 May 2014

Installing Cpan Module or installin perl Pakage

How to install perl pakage

There are many methods to install perl module or pakage to use in your program:

1. Using CPAN
Install cpan if not installed
to install a module say "Spreadsheet::Read" run the following command:

sudo cpan install module
Example:
sudo cpan install Spreadsheet::Read


2. With the help of the tar or zip file of the package 
follow the following steps:

download the tar of the package required from cpan directory
example for Spreadsheet::Read
go to cpan.org and search the module required.
write click on the tar of the module on the right side of the page and select coppy link location.
come to the terminal and run

1.wget http://search.cpan.org/CPAN/authors/id/H/HM/HMBRAND/Spreadsheet-Read-0.54.tgz
         this will download the package to the current location
extract the package
2.tar -xvzf pakagename
    tar -xvzf Spreadsheet-Read-0.54.tgz
3.cd Spreadsheet-Read-0.54
 on reaching inside the folder extracted run the following command
4.sudo perl Makefile.pl
5.make
6.sudo make install
and following these major steps you can easily install any pakage required.




Install Apache2 webserver with PERL,Php Support in Ubuntu Server(verified and tested)

Install Apache2 in Ubuntu
sudo aptitude install apache2
This will complete the installation.

After installation
Type the server’s IP address (or alias if you added the server to your /etc/hosts file) in your browser’s address bar or, if you are browsing on the server itself, type 127.0.0.1 or localhost. If an error occurs, then you will have to edit the apache2.conf file to ensure that Apache can fully resolve the server’s name.If you have any problem then you have to edit the apache2 configuration file using the following command

sudo nano /etc/apache2/apache2.conf
Add the following line somewhere
ServerName localhost
or
ServerName yourserverip
Save and exit the file

Restart Apache server using the following command.
sudo apache2ctl restart

Change default document root in Apache2
The main configuration file located at /etc/apache2/apche2.conf.If you want to change the default document root you need to edit the /etc/apache2/sites-available/default file and look for this line “DocumentRoot /var/www/” here you can change where ever you want to change.For example if you want to change /home/www the above line looks like this “DocumentRoot /home/www/”.
Save and exit the file

Restart Apache server using the following command.
sudo apache2ctl restart




Enable CGI and perl support for apache2 server
You need to install the following package
sudo aptitude install libapache2-mod-perl2
Configure a cgi-bin directory
You need to create a cgi-bin directory using the following command
sudo mkdir /home/www/cgi-bin
Configuring Apache to allow CGI program execution is pretty easy. Create a directory to be used for CGI programs and add the following to the site configuration file (again between the <VirtualHost> tags).
ScriptAlias /cgi-bin/ /home/www/cgi-bin/
<Directory /home/www/cgi-bin/>
Options ExecCGI
 AddHandler cgi-script cgi pl
       AddHandler default-handler .jpg .png .gif .js .txt .bat .css .html .htm
</Directory>
The first line creates an alias that points to the directory in which CGI scripts are stored. The final line tells Apache that only files that end with the *.cgi and *.pl extensions should be considered CGI programs and executed.

**AddHandler default-handler .jpg .png .gif .js .txt .bat .css .html .htm
this tells cgi to exclude some of the non perl related files like "CSS,txt,html etc"

Test your Perl Program
cd /home/www/cgi-bin
sudo nano perltest.pl
Copy and paste the following section save and exit the file.
###Start###
#!/usr/bin/perl -w
print "Content-type: text/html\r\n\r\n";
print "Hello there!<br />\nJust testing .<br />\n";
Change permissions on it

sudo chmod a+x perltest.pl
Open your web browser open
http://yourserverip/cgi-bin/perltest.pl.It should be working.








Enable PHP support for apache2 webserver

If you want to enable php5 or php4 support to your apache webserver use the following commands to install require packages
For PHP5
sudo aptitiude install php5 libapache2-mod-php5
For PHP4
sudo aptitiude install php4 libapache2-mod-php4
You also make sure the php5 and php4 modules are enabled using the following commands
sudo a2enmod php5
sudo a2enmod php4
After installing php support you need to restart apache webserver using the following command
sudo apache2ctl restart
Test your PHP Support foe apache webserver
To check the status of your PHP installation
sudo nano /var/www/testphp.php
and insert the following line
<?php phpinfo(); ?>
Save and exit the file
Now open web browser at http://yourserveripaddress/testphp.php and check.




Removing Apache2:

 cmd:
apt-get remove apache2
 

Setting Up GIT (git configuration on loca PC with the server)


Some Simple steps for GIT cloning and installation:

  • sudo apt-get install git
  • make a saparate folder for code
  • cd into that
  • git config --global user.name "XYZ" //user name of GIT account
  • git config --global user.email "XYZ@gmail.com" //mail  which is registered with  GIT account
    • NOTE:: each user have a unique user/mailid ., other wise git conflict will be there while git activities.
  • git clone gitaccountwebpage.com/git/repository_name/   //clone to repository of git directory.
  • once it done go to newly created directory synced with the server  (into your craeted directory).

GIt cloning Done


check basic commands like pull push add....

learn basics of GIT (http://rogerdudler.github.io/git-guide/)

How to install MySQL on Ubuntu

 
 
 
Follow the following steps:
 
 
sudo apt-get update
sudo apt-get dist-upgrade
 
Install the MySQL server and client packages:

sudo apt-get install mysql-server mysql-client
When done, you have a MySQL database read to rock 'n roll. However, there's more to do.
You need to set a root password, for starters. MySQL has it's own user accounts, which are not related to the user accounts on your Linux machine. By default, the root account of the MySQL Server is empty. You need to set it. Please replace 'mypassword' with your actual password and myhostname with your actual hostname.
sudo mysqladmin -u root -h localhost password 'mypassword' sudo mysqladmin -u root -h myhostname password 'mypassword' 

You can now access your MySQL server like this:

mysql -u root -p

 
 

Wednesday 26 March 2014

Important Linux Commands

lets make a sheet of all day to day commands we Use in our Linux Related Work ...

 

1. tar command examples

i.)Create a new tar archive.

$ tar cvf archive_name.tar dirname/

ii.)Extract from an existing tar archive.
 
$ tar xvf archive_name.tar
iii)View an existing tar archive.
$ tar tvf archive_name.tar
 
 

2. grep command examples

Search for a given string in a file (case in-sensitive search).

$ grep -i "the" demo_file

Search for a given string in all files recursively
$ grep -r "ramesh" *
 
 
 

3. SSH command examples

 
 
4 basic command line usage of the ssh client.
  1. Identify SSH client version
  2. Login to remote host
  3. Transfer Files to/from remote host
  4. Debug SSH client connection

1. SSH Client Version:

$ ssh -V
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003 
 
 

2. Login to remote host:

localhost$ ssh -l jsmith remotehost.example.com
 
 

3. File transfer to/from remote host:

 
Copy file from the remotehost to the localhost:
 scp user@IP:/home/related/file.txt remotehostfile.txt
 
Copy file from the localhost to the remotehos  
   scp localfile.txt user@IP:/home/path/file.txt

 

4. Debug SSH Client: 


localhost$ ssh -l user remotehost.example.com

 

 

 --------------------------------------------------------------------------

 

Touch Commands :

The touch command is the easiest way to create new, empty files. It is also used to change the timestamps (i.e., dates and times of the most recent access and modification) on existing files and directories.
touch's syntax is
touch [option] file_name(s)
Thus, for example, the following command would create three new, empty files named file1, file2 and file3:
touch file1 file2 file3
A nice feature of touch is that, in contrast to some commands such as cp (which is used to copy files and directories) and mv (which is used to move or rename files and directories), it does not automatically overwrite (i.e., erase the contents of) existing files with the same name. Rather, it merely changes the last access times for such files to the current time.