I always had this question until one day I decided to find the solution and understand the reason.
Monday, August 29, 2011
How to interpolate hashes in double quotes under Perl
Just like we can interpolate scalar variables and arrays in double quotes, why can't we interpolate hashes in Perl.
Wednesday, December 29, 2010
Add Custom Header to scripts using vim
:insert## $Id: $## Copyright (c) 2011 Parag Kalra# All rights reserved.### @script#### @summary#### @description#### @usage#### @synopsis#### @param#### @keywords#### @status review#### @author#### @creation-date#### @last-modified#### @burts#### see-also.
:insert should be present on first line and . should be present on last line
2. Add following lines to ~/.vimrc
autocmd bufnewfile *.pl so ~/.vim_header.plautocmd bufnewfile *.pl exe "1," . 30 . "g/@script.*/s//@script " .expand("%")autocmd bufnewfile *.pl exe "1," . 30 . "g/@creation-date/s//@creation-date " .strftime("%d-%m-%Y")autocmd Bufwritepre,filewritepre *.pl execute "normal ma"autocmd Bufwritepre,filewritepre *.pl exe "1," . 30 . "g/last-modified/s/last-modified .*/last-modified " .strftime("%c")autocmd bufwritepost,filewritepost *.pl execute "normal `a"
3. Now create a new file having .pl extension and see the magic.
Tuesday, December 14, 2010
Locking Hashes - How to Lock Hashes in Perl
One drawback of hashes is that their keys are barewords which offer little typo protection (especially compared to the function and variable name protection offered by the strict pragma). The core module Hash::Util provides mechanisms to restrict the modification of a hash or the keys allowed in the hash.
For examples have a look at my sample scripts here.
Monday, December 13, 2010
How to find if a Perl module is installed on the System
Quick and Dirty way to find if a Perl module is installed on the System:
perl -MModule_Name -e 1
Monday, January 18, 2010
How to break from if - else in Perl
One of the ways to break from if - else in Perl is to use goto
if ($num =~ /^\d+$/){
if($num == 13){
goto ENDOFIF;
}
} else {
print "Number not found.\n";
ENDOFIF:
}
So if the number is 13 then it will break from the if and place the control right at the end of outer if.
if ($num =~ /^\d+$/){
if($num == 13){
goto ENDOFIF;
}
} else {
print "Number not found.\n";
ENDOFIF:
}
So if the number is 13 then it will break from the if and place the control right at the end of outer if.
Sunday, January 10, 2010
How to calculate the time of execution of a Perl script
Just add the following line any where in your Perl script and it will print its time of execution right at the end:
# This calculates the time of execution of the Perl script.
END {warn "\nTime of execution - ", time - $^T, " second(s)\n"}
# This calculates the time of execution of the Perl script.
END {warn "\nTime of execution - ", time - $^T, " second(s)\n"}
Thursday, April 16, 2009
Connecting to MySQL Database through PERL
FAQ: How to connect to MySQL database through PERL.
SOLUTION:
1. Install MySQL database. MySQL community server comes free of cost and is distributed under GNU General Public License.
2. I would instead recommend installing a package called WAMP server which provides MySQL community server, Apache server and PHP. It also provides many effective tools like PHPMyAdmin which can be used for database administration. Also WAMP server comes free of cost and is distributed under GNU General Public License.
3. To check if the MySQL server is running, execute:
telnet 3306
E.G: # telnet 127.0.0.1 3306
4. I would also recommend you to also install SQLYog. It is a very light and effective tool to monitor and manage MySQL database. Again it also comes free of cost and is distributed under GNU General Public License.
5. Create a database in MySQL, a non-root user and give it a password.
6. Install ‘DBI’ perl module through ‘PPM’
7. Install ‘DBD::Mysqlpp” perl module through ‘PPM’
8. You are all set to connect to MySQL database. While connecting make sure that database is already created and you are connecting through a valid user and with correct credentials. To see a sample script execute following on command prompt:
# perldoc DBD::Mysqlpp
REFERENCES:
1. http://dbi.perl.org/
2. # perldoc DBD::Mysqlpp
AUTHOR: Parag Kalra
Email-ID: paragkalra@gmail.com
SOLUTION:
1. Install MySQL database. MySQL community server comes free of cost and is distributed under GNU General Public License.
2. I would instead recommend installing a package called WAMP server which provides MySQL community server, Apache server and PHP. It also provides many effective tools like PHPMyAdmin which can be used for database administration. Also WAMP server comes free of cost and is distributed under GNU General Public License.
3. To check if the MySQL server is running, execute:
telnet
E.G: # telnet 127.0.0.1 3306
4. I would also recommend you to also install SQLYog. It is a very light and effective tool to monitor and manage MySQL database. Again it also comes free of cost and is distributed under GNU General Public License.
5. Create a database in MySQL, a non-root user and give it a password.
6. Install ‘DBI’ perl module through ‘PPM’
7. Install ‘DBD::Mysqlpp” perl module through ‘PPM’
8. You are all set to connect to MySQL database. While connecting make sure that database is already created and you are connecting through a valid user and with correct credentials. To see a sample script execute following on command prompt:
# perldoc DBD::Mysqlpp
REFERENCES:
1. http://dbi.perl.org/
2. # perldoc DBD::Mysqlpp
AUTHOR: Parag Kalra
Email-ID: paragkalra@gmail.com
Subscribe to:
Comments (Atom)
