J
Jorge
Using Perl v5.8.3 built for sun4-solaris-64
I'm putting together a handshaking front-end for a script that will
read a spreadsheet (OLE) and use the results as DBI/DBD directly into
an Oracle table.
Because this will be (someday) a production script, I need to keep a
solid record of all the warnings, errors and such in a log as well as
splash that same information to the terminal for the user.
Therefore, I have 2 filehandles that I want to toggle between hot and
cold -- STDOUT and LOG ($fh in subroutine).
A snippet (very pruned down) is below to show how I am doing the
toggling. As already said, it is working but I can't believe it's the
correct way -- it's downright ugly.
Is there a more elegant approach to this.
FWIW: I've been through the FAQ's, the 'suffering from buffering'
article and many others and couldn't find any reference to this
particular situation.
All help is greatly appreciated.
Jorge
#!/usr/local/bin/perl -w
use strict;
use Cwd;
my $log = "log";
my $log_date_time = scalar(localtime);
local $0 = get_pathname_leaf($0);
my $curWorkDir = getcwd();
my $user_name = $ENV{'USER'};
open LOG, ">$log" or die "cannot open $log $!\n";
# make filehandle hot print header lines to log
select( ( select(LOG), $| = 1 )[0] );
print LOG " Created by user ".$user_name." [".$log_date_time."]\n";
print LOG " ==================================================\n";
if(&check_args($#ARGV + 1, \@ARGV, \*LOG) == 1){
select( ( select(LOG), $| = 0 )[0] );
print "Exiting due to errors ... [from calling script]\n";
select( ( select(LOG), $| = 1 )[0] );
print LOG "Exiting due to errors ... [from calling script]\n";
close(LOG);
exit();
}
sub check_args(){
my $numargs = shift;
my $files = shift;
my $fh = shift;
if($numargs != 1){
select( ( select($fh), $| = 0 )[0] );
print "\n\nUsage: $0 input_file <Enter>\n\n";
select( ( select($fh), $| = 1 )[0] );
print $fh "\n\nUsage: $0 input_file <Enter>\n\n";
exit();
}
my $flag = 0;
my $arg;
my $i = 0;
foreach $arg (@$files) {
++$i;
if($i == 1 && (!( -f $arg))){
select( ( select($fh), $| = 0 )[0] );
print "\n\n",$arg," not found \n";
select( ( select($fh), $| = 1 )[0] );
print $fh "\n\n",$arg," not found \n";
$flag = 1;
last;
}
elsif($i == 1 && (!( -s $arg))){
select( ( select($fh), $| = 0 )[0] );
print "\n\n",$arg," is an empty file \n";
select( ( select($fh), $| = 1 )[0] );
print $fh "\n\n",$arg," is an empty file \n";
$flag = 1;
last;
}
elsif($i == 1 && (!( -r $arg))){
select( ( select($fh), $| = 0 )[0] );
print "\n\n",$arg," is not a readable file \n";
select( ( select($fh), $| = 1 )[0] );
print $fh "\n\n",$arg," is not a readable file \n";
$flag = 1;
last;
}
elsif($arg !~ /\.xls$/i){
select( ( select($fh), $| = 0 )[0] );
print "\n\n",$arg," does not appear to be an Excel file\n";
select( ( select($fh), $| = 1 )[0] );
print $fh "\n\n",$arg," does not appear to be an Excel file\n";
$flag = 1;
last;
}
}
if($flag == 1){return 1;}else{return 0;
}
}
sub get_pathname_leaf{
my ($pathname) = @_ ;
my @split_pathname = split( /[\\\/]/, $pathname ) ;
my $leaf = pop( @split_pathname ) ;
return( $leaf ) ;
}
I'm putting together a handshaking front-end for a script that will
read a spreadsheet (OLE) and use the results as DBI/DBD directly into
an Oracle table.
Because this will be (someday) a production script, I need to keep a
solid record of all the warnings, errors and such in a log as well as
splash that same information to the terminal for the user.
Therefore, I have 2 filehandles that I want to toggle between hot and
cold -- STDOUT and LOG ($fh in subroutine).
A snippet (very pruned down) is below to show how I am doing the
toggling. As already said, it is working but I can't believe it's the
correct way -- it's downright ugly.
Is there a more elegant approach to this.
FWIW: I've been through the FAQ's, the 'suffering from buffering'
article and many others and couldn't find any reference to this
particular situation.
All help is greatly appreciated.
Jorge
#!/usr/local/bin/perl -w
use strict;
use Cwd;
my $log = "log";
my $log_date_time = scalar(localtime);
local $0 = get_pathname_leaf($0);
my $curWorkDir = getcwd();
my $user_name = $ENV{'USER'};
open LOG, ">$log" or die "cannot open $log $!\n";
# make filehandle hot print header lines to log
select( ( select(LOG), $| = 1 )[0] );
print LOG " Created by user ".$user_name." [".$log_date_time."]\n";
print LOG " ==================================================\n";
if(&check_args($#ARGV + 1, \@ARGV, \*LOG) == 1){
select( ( select(LOG), $| = 0 )[0] );
print "Exiting due to errors ... [from calling script]\n";
select( ( select(LOG), $| = 1 )[0] );
print LOG "Exiting due to errors ... [from calling script]\n";
close(LOG);
exit();
}
sub check_args(){
my $numargs = shift;
my $files = shift;
my $fh = shift;
if($numargs != 1){
select( ( select($fh), $| = 0 )[0] );
print "\n\nUsage: $0 input_file <Enter>\n\n";
select( ( select($fh), $| = 1 )[0] );
print $fh "\n\nUsage: $0 input_file <Enter>\n\n";
exit();
}
my $flag = 0;
my $arg;
my $i = 0;
foreach $arg (@$files) {
++$i;
if($i == 1 && (!( -f $arg))){
select( ( select($fh), $| = 0 )[0] );
print "\n\n",$arg," not found \n";
select( ( select($fh), $| = 1 )[0] );
print $fh "\n\n",$arg," not found \n";
$flag = 1;
last;
}
elsif($i == 1 && (!( -s $arg))){
select( ( select($fh), $| = 0 )[0] );
print "\n\n",$arg," is an empty file \n";
select( ( select($fh), $| = 1 )[0] );
print $fh "\n\n",$arg," is an empty file \n";
$flag = 1;
last;
}
elsif($i == 1 && (!( -r $arg))){
select( ( select($fh), $| = 0 )[0] );
print "\n\n",$arg," is not a readable file \n";
select( ( select($fh), $| = 1 )[0] );
print $fh "\n\n",$arg," is not a readable file \n";
$flag = 1;
last;
}
elsif($arg !~ /\.xls$/i){
select( ( select($fh), $| = 0 )[0] );
print "\n\n",$arg," does not appear to be an Excel file\n";
select( ( select($fh), $| = 1 )[0] );
print $fh "\n\n",$arg," does not appear to be an Excel file\n";
$flag = 1;
last;
}
}
if($flag == 1){return 1;}else{return 0;
}
}
sub get_pathname_leaf{
my ($pathname) = @_ ;
my @split_pathname = split( /[\\\/]/, $pathname ) ;
my $leaf = pop( @split_pathname ) ;
return( $leaf ) ;
}