Using perl modules

L

LHradowy

I am having difficulty using perl modules, I know once I start and
figure this out, it would make my life simplier.

My module is used for logging into a database.
#!/usr/bin/perl

use strict;
use warnings;

package login_acdb;

my $file = "/etc/accesscare/$ENV{'ORACLE_SID'}/voice/ac.login";
open LOGINID, "< $file" or die "Could not open $file: $!";
my $oraLoginId = <LOGINID>;
chomp $oraLoginId;
1;

The file looks like: oracle/oracle321

I have added it to my PERL5LIB environment so it is in the @INC path.

But now I am having problems with how to call it.

Here is where I call it in my script:
sub doSqlSelect {
my @buffer = qx { sqlplus -S $login_acdb::eek:raLoginId <<EOF;
set heading off
set pagesize 0
set feedback off
SELECT feature||','||description
FROM vt_feature
ORDER by 1;
EOF
};

Yes, I know it would be really easy to use the DBI, but I have tried to
no success to have 32 bit oracle, with 32 or 64 bit perl, on HP 11.00
RISC. Spent a week on that one.
DBI goes in nice, but DBD::ORACLE cacks out every time
 
A

anno4000

LHradowy said:
I am having difficulty using perl modules, I know once I start and
figure this out, it would make my life simplier.

My module is used for logging into a database.
#!/usr/bin/perl

A module normally doesn't need a shebang line.
use strict;
use warnings;

package login_acdb;

Lower-case package names are reserved for pragmatic modules. Make
that Login_acdb or so.
my $file = "/etc/accesscare/$ENV{'ORACLE_SID'}/voice/ac.login";
open LOGINID, "< $file" or die "Could not open $file: $!";
my $oraLoginId = <LOGINID>;
chomp $oraLoginId;
1;

Well, that puts the data in an inaccessible place. The lexical
variable $oraLoginId is invisible from outside the file it is
declared in.
The file looks like: oracle/oracle321

I have added it to my PERL5LIB environment so it is in the @INC path.

But now I am having problems with how to call it.

Here is where I call it in my script:
sub doSqlSelect {
my @buffer = qx { sqlplus -S $login_acdb::eek:raLoginId <<EOF;
set heading off
set pagesize 0
set feedback off
SELECT feature||','||description
FROM vt_feature
ORDER by 1;
EOF
};

You are confusing the concepts of a module (which is invoked by
use() or require()) and a script that has to be called as a separate
executable.

To make it a module (untested):

package Login_acdb;
use strict; use warnings;

my $file = "/etc/accesscare/$ENV{'ORACLE_SID'}/voice/ac.login";

sub get_login {
open my $in, '<', $file or die "Could not open '$file': $!";
chomp( my $id = <$in>);
$id;
}
1;

Put that into Login_acdb.pm. Then in your main script do:

#!/usr/bin/perl
use strict; use warnings;

use Login_acdb;

my $oraLoginId = Login_acdb::get_login;

# use $oraLoginId to log in

Anno
 
G

Guest

I am having difficulty using perl modules, I know once I start and
figure this out, it would make my life simplier.

My module is used for logging into a database.
#!/usr/bin/perl

use strict;
use warnings;

package login_acdb;

my $file = "/etc/accesscare/$ENV{'ORACLE_SID'}/voice/ac.login";
open LOGINID, "< $file" or die "Could not open $file: $!";
my $oraLoginId = <LOGINID>;

"My" creates a variable that is constrained to the current block or
file. If you want $orgLoginId to be accessible to other parts of the
program, declare it with "our":

our $oraLoginId = <LOGINID>;

After you're finished reading the login id, you can probably close the file.
chomp $oraLoginId;
1;

The file looks like: oracle/oracle321

Huh?

I have added it to my PERL5LIB environment so it is in the @INC path.

But now I am having problems with how to call it.

Here is where I call it in my script:
sub doSqlSelect {
my @buffer = qx { sqlplus -S $login_acdb::eek:raLoginId <<EOF;
set heading off
set pagesize 0
set feedback off
SELECT feature||','||description
FROM vt_feature
ORDER by 1;
EOF
};
[...]

The commands probably need to be within quotes, and I would create the
commands string separately:

sub doSqlSelect {

my $sql = <<' EOF';
set heading off
set pagesize 0
set feedback off
SELECT feature||','||description
FROM vt_feature
ORDER by 1;
EOF

$sql =~ s/(["\$])/\\$1/g;

my @buffer = qx { echo "$login_acdb::eek:raLoginId" "$sql" };
}

The indentation helps keeps things more readable, and the quotes around
$sql are needed to prevent the shell from misinterpreting the contents.

Since you are passing a lot of data through the shell, you must be very
careful about properly quoting and escaping your data, or your program
will be exploitable to hackers who want to get shell access on your machine.

I doubt that my little substitution above is adequate, but I hope you
get the idea.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top