I will give an example of how to write IF ELSE statement in short. This will be simply useful to read others code if you know that this can also be an option. This kind of code normally comes from seasoned PERL programers.
#!/opt/local/bin/perl
use warnings;
use strict;
print &max(5, 3);
print &max(7, 10);
sub max (@)
{
my ($x, $y) = @_;
return $x > $y ? $x : $y;
}
Explanation: In function max, 2nd lines means that
if $x is greater than $y, return $x or return $y. That's it.....
Thursday, November 6, 2008
Wednesday, November 5, 2008
How to open a write into an already zipped file in perl:
How to open a write into an already zipped file in perl:
--------------------------------------------------------
my $temp_file = "file_name.gz";
open(HWRITE, "|gzip -c > $temp_file") or die "cannot open file $temp_file\n";
print HWRITE "Something_1|Something_1|Something_1|Something_1\n"
or die "File write failed : $!\n";
--------------------------------------------------------
my $temp_file = "file_name.gz";
open(HWRITE, "|gzip -c > $temp_file") or die "cannot open file $temp_file\n";
print HWRITE "Something_1|Something_1|Something_1|Something_1\n"
or die "File write failed : $!\n";
Friday, October 10, 2008
How to pass an object in a function
It is really simple concept but can confuse novice to perl. An object can be passed to a function just like an ordinary variable. File handler need special syntax to be
passed inside a function but not an object.
Follow this simple example here. Code is incomplete but is tested when complete.
Here it goes:
#!/opt/local/bin/perl
use lib "$ENV{HOME}/perl/lib";
use convert_time;
use dir_pool;
use db_connect;
use warnings;
use strict;
my $obj = db_connect->new();
&doConnectMIR($obj);
print " \n";
&doConnectCIR($obj);
sub doConnectMIR($)
{
my $tObj = shift;
my $dbh;
my $sql;
my $sth;
my $str;
my @row;
$tObj = db_connect->new();
$dbh = $obj->connect_qora007_mir("qora007_mir");
$sql = "select sysdate from dual";
$sth = $dbh->prepare($sql);
$sth->execute;
while((@row) = $sth->fetchrow_array())
{
$str = join(" " , map {defined $_ ? $_ : "(null)"} @row);
print "INSTANCE: $str\n";
}
more code follows.....but concept is clear.....
passed inside a function but not an object.
Follow this simple example here. Code is incomplete but is tested when complete.
Here it goes:
#!/opt/local/bin/perl
use lib "$ENV{HOME}/perl/lib";
use convert_time;
use dir_pool;
use db_connect;
use warnings;
use strict;
my $obj = db_connect->new();
&doConnectMIR($obj);
print " \n";
&doConnectCIR($obj);
sub doConnectMIR($)
{
my $tObj = shift;
my $dbh;
my $sql;
my $sth;
my $str;
my @row;
$tObj = db_connect->new();
$dbh = $obj->connect_qora007_mir("qora007_mir");
$sql = "select sysdate from dual";
$sth = $dbh->prepare($sql);
$sth->execute;
while((@row) = $sth->fetchrow_array())
{
$str = join(" " , map {defined $_ ? $_ : "(null)"} @row);
print "INSTANCE: $str\n";
}
more code follows.....but concept is clear.....
Wednesday, August 20, 2008
Typical Oracle profile file on Unix
# .oracle10g
### Oracle specific setup
ORACLE_HOME=/opt/oracle10g/instantclient_10_2
TNS_ADMIN=/opt/oracle10g/instantclient_10_2
NLS_LANG=_.WE8ISO8859P1
export ORACLE_HOME TNS_ADMIN NLS_LANG
LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
PATH=$ORACLE_HOME:$PATH
export PATH
### End Oracle specific setup
### Oracle specific setup
ORACLE_HOME=/opt/oracle10g/instantclient_10_2
TNS_ADMIN=/opt/oracle10g/instantclient_10_2
NLS_LANG=_.WE8ISO8859P1
export ORACLE_HOME TNS_ADMIN NLS_LANG
LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
PATH=$ORACLE_HOME:$PATH
export PATH
### End Oracle specific setup
Typical Oracle profile file on Unix
# .oracle10g
### Oracle specific setup
ORACLE_HOME=/opt/oracle10g/instantclient_10_2
TNS_ADMIN=/opt/oracle10g/instantclient_10_2
NLS_LANG=_.WE8ISO8859P1
export ORACLE_HOME TNS_ADMIN NLS_LANG
LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
PATH=$ORACLE_HOME:$PATH
export PATH
### End Oracle specific setup
### Oracle specific setup
ORACLE_HOME=/opt/oracle10g/instantclient_10_2
TNS_ADMIN=/opt/oracle10g/instantclient_10_2
NLS_LANG=_.WE8ISO8859P1
export ORACLE_HOME TNS_ADMIN NLS_LANG
LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
PATH=$ORACLE_HOME:$PATH
export PATH
### End Oracle specific setup
Friday, August 1, 2008
Subscribe to:
Posts (Atom)