Archive forPerl

Sourcing a Shell script into perl

A lot of times we require to source the shell variables or shell script into the perl environment. One way to do this is you add very variable into the perl script something like :

$ENV{”SOME_VAR”} = “SOME_VAL”;

The other best way to do this task is parse the shell file and source all the environment variables in to perl script environment. This way we don’t need to modify your perl script every time the shell script is modified. :)

my ($envar, $enval);
open IN, “. ./3rdParty.env; env |”
or die “Could not run shell: $!\n”;
while ( <IN> ) {
print $_;
chomp;
($envar,$enval) = split /=/,$_,2;
$ENV{$envar} = $enval;
}
close IN;

This is very useful if the shell script is changed very frequently by the users.

Comments

Ways to add library path in perl

Run a perl script using libraries in nonstandard locations.
======================================

Use perl -V to see the include paths @INC array
I will be something like :
/usr/lib/perl5/5.8.0/i386-linux-thread-multi
/usr/lib/perl5/5.8.0
/usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi
/usr/lib/perl5/site_perl/5.8.0
/usr/lib/perl5/site_perl
/usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi
/usr/lib/perl5/vendor_perl/5.8.0
/usr/lib/perl5/vendor_perl


1. Using the module lib
===============

The standard module lib can be used to specify an explicit path to include. It must be stated at the
top of the script:
#!/usr/bin/perl
#
use lib “/opt/special/plib”;
use strict;
use warnings;

2. Using the switch -I at the command line
============================

The switch I can be used to specify additional library locations when invoking the interpreter.
perl -I /opt/special/plib script.pl

3. Using the switch -I in the first line of the script
=================================
The same I switch can be added to the interpreter specification.
#!/usr/bin/perl -I /opt/special/plib
#
use strict;
use warnings;
..

This works when invoking the script via the shell (which will run the interpreter with full
options and arguments as specified in the first line) and also when invoking the interpreter
directly: It apparently scans the first line for options.

4. Manipulating @INC directly
====================
The array @INC can be manipulated directly using array operations :

#!/usr/bin/perl
#
BEGIN {
unshift(@INC, “/opt/special/plib”);
}
use strict;
use warnings;

5. Using the environment variable PERL5LIB
==============================
The environment variable PERL5LIB can be used to specify additional include directories whe
running a perl script.
> export PERL5LIB=/opt/special/plib

6. Changing @INC at compile time
=======================

When running Configure to compile the perl interpreter itself, there are several possibilities to add
additional library path elements:
· Using the variable vendorprefix
· Using the variable otherlibdirs
Both must be specified when calling Configure as a define, eg
> sh Configure -Dotherlibdirs=/opt/special/plib
The variable otherlibdirs is preferred, as it can hold mutliple values separated by a colon just like
the familiar PATH environment variable.
Details about compiling perl can be found on the CPAN network :
http://search.cpan.org/~nwclark/perl5.8.3/INSTALL.

Comments

Replace text in multiple files with perl just in one line…

How do I do a mass search and replace in a directory?

How can I search for a string in all my files and replace it with a text?

Are there any spiffy shortcuts for search/replace in a directory using command line Perl?

How do I load a list of replace patterns from a file in a one-liner?

Use this my all time favorite one line scripts ….:)

Assuming you are in the directory where you want to effect this search and replace

perl -pi -e ’s/lookFor/replaceWith/’ *.fileExtension

perl -pi -e ’s/lookFor/replaceWith/g’ *.fileExtension

perl -pi -e ’s/lookFor/replaceWith/gi’ *.fileExtension

Explanation :

- lookFor is the word you wish to look for.

- replaceWith is the word you wish to replace your original word with

- g stands for global, it will basically replace all the occurences of lookFor with replaceWith in all your files in a directory

- i stands for case insensitive

To load a series of replace expressions from a text file:

perl -pi -e "`/path/to/replace-patterns.txt`" *.fileExtension

s/lookForPatternOne/replaceWithOne/;

s/lookForPatTwo/replaceWithTwo/g;

s/lookForPatThree/replaceWithThree/gi;

Comments

How to get a date or timestamp of a file in perl.

Once i was trying to get the date when the file is written in perl. I found the following solutions all of them are giving the complete timestamp of the file but i needed only the date.
You can get the timestamp of a file in following ways in perl :

First Method :

$write_secs = (stat($file))[9];
print “file $file updated at “, scalar(localtime($file)), “\n”;

Second Method :
Using standard module File::stat and Time::locatime(bundled with all 5.x perl installations)

use File::stat;
use Time::localtime;
$date_string = ctime(stat($file)->mtime);
print “file $file updated at $date_string\n”;

To get only the data of file I used this solution :

$tm = localtime(stat($path)->mtime);
#get only the data from time object
$date_string = $tm->mday.”-”.(($tm->mon)+1).”-”.(($tm->year)+1900);
print “file $file updated at $date_string\n”;

Comments

Simple redirection in Java Script

Once I am trying to redirect a cgi script with cookies or after writing the cookies header. I searched a lot of stuff and found that in cgi redirection can be done only before writing the header information. Digging in to the problem I found this simple solution….

Comments

"Quote of the Day"