need perl script explanation
669086Aug 11 2009 — edited Aug 11 2009Hi,
I have the .pl file it is not calling any where by the program,but it is transfering the oracle viewoutput file to another data_top.
how is it possible.
can any body explain me the below file how it is working.
Thanks in advance.
this is the code in the .pl file:
#!/usr/bin/perl
# Author: Stian H. Larssen (Indal Haugseth)
# Created: 2008-06-25
# Description: Removes newline characters when line ends with given string.
# Changelog
# 16.11.2008 Stian Indal Haugseth
# - Write to temp file while processing. Move/rename temp file to
# destination file when finished.
# 16.11.2008 Stian Indal Haugseth
# - Moved init of temp file variable to remove errormassege when not having
# any arguments.
use strict;
use warnings;
use Getopt::Std;
my %option = ();
getopts("dp:", \%option);
my $line = '';
my $in_file_arg = $ARGV[0];
my $out_file_arg = $ARGV[1];
my $replace_string = "¤#¤";
my $time_start = time();
my $temp_dir_name = 'temp';
my $out_dir;
my $out_file_name;
my @system_args;
if ($option{p}) {
$replace_string = $option{p};
}
if ((!$in_file_arg) || (!$out_file_arg)) {
print STDERR "Syntaks: $0 [-dp <mønster>] <kildefil> <utfil>\n";
print STDERR "Parameter -d vil slette kildefil etter kjøring.\n";
print STDERR "Parameter -p <mønster> lar endre søkemønster.\n";
exit 1;
}
print "Script startet : ". localtime($time_start) ."\n";
# If $out_file_arg contains any slash, split file name and directory name.
if ($out_file_arg =~ m/\//) {
($out_dir, $out_file_name) = $out_file_arg =~ m/(.*\/)(.*)$/; # split dir and filename
if ((!$out_dir) || (!$out_file_name)) {
print STDERR "Klarte ikke splitte fil/mappe fra $out_file_arg, script stoppet\n";
exit 1;
}
} else {
# File name does not contain any slashes, set default directory to ./ and use $out_file_arg as file name.
$out_dir = './';
$out_file_name = $out_file_arg;
}
# Create temp file variable after parameter check to remove errormessage when $out_file_arg not initialized
my $out_tmp_file = $out_file_name.'_tmp_'.$$; # Adding tmp[PID] to temp file
my $out_tmp_dir = $out_dir.$temp_dir_name; # Concatenate temp dir name to out dir
if (!-e $in_file_arg) {
print STDERR "Fant ikke filen $in_file_arg, script stoppet\n";
exit 1;
}
if (!-r $in_file_arg) {
print STDERR "Har ikke rettigheter til å lese filen $in_file_arg, script stoppet\n";
exit 1;
}
# Create temp dir.
if (-d $out_tmp_dir) {
if (!-W $out_tmp_dir) {
print STDERR "Har ikke skriverettigheter til midlertidig mappe $out_tmp_dir, script stoppet\n";
exit 1;
}
} else {
# Mode removed from mkdir command due bug in old perl version 5.005_03.
mkdir $out_tmp_dir or die "Kunne ikke opprette midlertidig mappe $out_tmp_dir, script stoppet";
}
open (INFILE, "<",$in_file_arg) or die "Kunne ikke åpne kildefilen $in_file_arg, script stoppet";
# create or replace file with +>
open (OUTFILE, "+>", $out_tmp_dir.'/'.$out_tmp_file) or die "Kunne ikke åpne midlertidig målfil ".$out_dir.$out_tmp_file.", script stoppet";
print 'Skriver midlertidig fil '.$out_tmp_dir.'/'.$out_tmp_file.".\n";
while (<INFILE>) {
$line = $_;
$line =~ s/$replace_string\n//;
print OUTFILE $line;
}
close (INFILE);
close (OUTFILE);
@system_args = ("/bin/mv", $out_tmp_dir.'/'.$out_tmp_file, $out_file_arg);
system(@system_args) == 0 or die 'Klarer ikke flytte '.$out_tmp_dir.'/'.$out_tmp_file.' til '.$out_file_arg.', returkode '.$?.', script stoppet';
print "MÃ¥lfil $out_file_arg opprettet.\n";
if ($option{d}) {
unlink($in_file_arg) or die "Klarer ikke slette kildefilen $in_file_arg, script stoppet";
print "Kildefil $in_file_arg slettet!\n";
}
print "Script utført : ". localtime(time()) . "\n";
print "Script kjøretid: ". (time - $time_start) . " sekunder.\n";