Home > Archive > PERL Beginners > August 2005 > creating my own include file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
creating my own include file
|
|
| Shimon Ravid 2005-08-07, 3:59 am |
| Hello,
I have created the following include file located at /kpi/bin/KPIDB.pl
package KPIDB;
%config;
sub readDBConfigFile {
my $dbConfigFileName = '/kpi/cfg/databases.cfg';
my $cfgf;
my $db;
my $found = 0;
my $alias;
my $user;
my $fh = new FileHandle("$dbConfigFileName", "r");
if (!defined($fh)) {
print "Error: Can't open $dbConfigFileName file for
reading: $!\n" if ($debug);
exit(1);
}
while ($_ = $fh->getline) {
next if (/\s*#/); # skip comments
next unless (/\S/); # skip empty lines
chomp;
if (/^\s*$hostName:/) {
$found = 1;
($host, $db, $user, $alias) = split(/:/);
if (exists($config{$host})) {
if (exists($config{$host}{$db})) {
print "Error: duplicate lines
for $host/$db\n" if ($debug);
} else {
$config{$host}{$db}{user} =
$user;
$config{$host}{$db}{alias} =
$alias;
}
} else {
$config{$host}{$db}{user} = $user;
$config{$host}{$db}{alias} = $alias;
}
}
}
$fh->close;
return($found);
}
1;
The following is the main program using the include file above.
The problem is that when I call the function ' readDBConfigFile' it
seems that it is not executing the function and does not
Print any error.
When inserting the code in the package to the main program, it works
well.
What am I doing wrong?
#!/usr/local/bin/perl5.8.5
use Getopt::Long;
use Sys::Hostname;
use FileHandle;
require "/kpi/bin/KPIDB.pl";
my $SU = '/bin/su';
sub usage {
print "Usage: $0 [-h] [-d]\n";
}
sub parseCommandLine {
my $retCode = GetOptions('h' => \$help,
'd' => \$debug);
usage and exit(1) if (!$retCode);
usage and exit(0) if ($help);
}
sub main {
my $status;
my $command;
my $db;
parseCommandLine;
$status = readDBConfigFile;
if ($status) {
foreach $db (keys %{$config{$host}}) {
$command = "$SU - $config{$host}{$db}{user} -c
\"$config{$host}{$db}{alias}; sqlplus -s /\@$db
@/kpi/bin/KPI1010-DB_SORTS.sql\"";
system($command);
}
}
}
main;
---------------------------------------------------------------------------------------------------------------
orange mail - Connect to your e-mail anywhere with any handset
http://www.orange.co.il/web/corpora...r/webform1.aspx
---------------------------------------------------------------------------------------------------------------
This message contains information that may be confidential or privileged.
If you are not the intended recipient, you may not use, copy or disclose
to anyone any of the information in this message. If you have received
this message and are not the intended recipient, kindly notify the sender
and delete this message from your computer.
| |
| Jeff 'japhy' Pinyan 2005-08-07, 9:59 pm |
| On Aug 7, Shimon Ravid said:
> package KPIDB;
>
> %config;
That's doing nothing. Nothing at all.
> sub readDBConfigFile {
....
> }
>
> 1;
That function is defined in the KPIDB package; therefore, to call it from
your main program, you'd have to call it as KPIDB::readDBConfigFile().
I haven't looked at your function body at all, though, so I won't comment
on what your function does and how it might possible do a better job.
But your KPIDB.pl file should be using 'strict' and 'warnings'. It looks
like you're declaring lexicals and all, so the only thing to change would
be your "definition" of %config:
package KPIDB;
use strict;
use warnings;
our %config;
...
1;
> The problem is that when I call the function ' readDBConfigFile' it
> seems that it is not executing the function and does not print any
> error.
If you had strict OR warnings turned on, you'd have a better idea why.
It's generally a poor idea to call a function with no arguments AND omit
its parentheses. Perl sees a "bareword" and doesn't always know if it's a
function or not.
If you'd written it as readDBConfigFile(), you'd have gotten an error
telling you that the function doesn't exist. That's because it's defined
in a separate namespace. Do what I said above, and change
> $status = readDBConfigFile;
to
$status = KPIDB::readDBConfigFile();
Now you have another problem, since that function populates the %config
hash in the KPIDB package. To access it from main, you'll have to use
$KPIDB::config{...} instead of $config{...}.
> main;
Perl is not C. Please do not define a main() function and then call it as
the body of your program. This is unnecessary.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
|
|
|
|
|