1754020 Members
7074 Online
108811 Solutions
New Discussion

using perl modules

 
Ratzie
Super Advisor

using perl modules

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 = ;
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::oraLoginId < 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
1 REPLY 1
Ralph Grothe
Honored Contributor

Re: using perl modules

Hi,

The problem seems to be that the variable $oraLoginID that you defined in your package login_acdb was declared as lexical (by my).
You can never access lexical variables once they are out of scope (especially from a different package, viz. the importing one).
Although this is usually the wished behavior in order to prevent name collisions, in you case it isn't.
Thus, you must declare it as a package global.
Only then does it appear in the symbol table,
and can always be accessed by its full qualified name (i.e. prepending the package that defined it).
This takes usually place for *every* variable that you introduce without explicit declaration unless you use the strict pragma.
But we usually want to remain under strict vars (to avoid name collisions).
Therefore declare it as "our" instead of "my".
In the importing package you could then access it by $login_acdb::ora_LoginId.
If this gets too clumsy you could also "alias" the variable by using the glob reference notation whithin a short block where you relinquish the strict refs pragma by "no strict 'refs';".
This should be the only circumstance where the use of the so called symbolic refs is "approved" by style guides.
Please, see the examples in "perldoc perlref" for details.
Alternatively you could use the Exporter module and declare the variables that you want to "alias" in the @EXPORT array (which causes automatic, but mostly deprecated import into the using name space), or better yet in the @EXPORT_OK array (where the importer has to explicitly declare what he wishes to be imported, usually in an appended qw() list).
More or less the Exporter module is doing the messing with the symbol table behind the scenes for the user.
See "perldoc Exporter" for details.


Madness, thy name is system administration