|
Prev: Statistics Extraction
Next: help with mime head
From: LHradowy on 25 Jan 2007 09:56 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::oraLoginId <<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
From: anno4000 on 25 Jan 2007 10:43 LHradowy <lhradowy(a)gmail.com> wrote in comp.lang.perl.misc: > 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::oraLoginId <<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 > > 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 >
From: Mumia W. (NOSPAM) on 25 Jan 2007 11:04 On 01/25/2007 08:56 AM, LHradowy wrote: > 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::oraLoginId <<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::oraLoginId" "$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. -- Windows Vista and your freedom in conflict: http://www.securityfocus.com/columnists/420/2
From: Michele Dondi on 25 Jan 2007 13:09 On 25 Jan 2007 15:43:34 GMT, anno4000(a)radom.zrz.tu-berlin.de wrote: >> My module is used for logging into a database. >> #!/usr/bin/perl > >A module normally doesn't need a shebang line. In fact, I usually put # -*- Perl -*- at the top, so that my editor recognizes it and enters in Perl mode. Well, actually I've also set up an association for .pm so it's not really necessary in this sense, but just like the shebang line is much like a 'hello' in actual scripts, I feel at ease with it as a 'hello' for modules. Michele -- {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB=' ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_, 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
|
Pages: 1 Prev: Statistics Extraction Next: help with mime head |