Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Mac > Perl on OSX > Re: "my variabl...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 5 Topic 997 of 1076
Post > Topic >>

Re: "my variable" at global level not being passed

by jeremiah@[EMAIL PROTECTED] (Jeremiah Foster) Oct 5, 2007 at 12:54 PM

The more package-oriented way is probably to use the DBI package at  
the top of your script. Perhaps you could do something like this:

#!/path/to/perl

use strict;
use warnings;
use DBI;

     # connect to database
     my $dbh = DBI->connect('DBI:mysql:db_name','user','password')
         or die "Could not connect to database! :  " . DBI->errstr;

Now you can use the $dbh variable locally. This would make your life  
pretty easy and it works with 5.8.6. It makes you life easy because  
you can diagnose database connections in the script you are writing  
and not in some other far off script you didn't write. It also allows  
you to specify very granular variables like a specific user who may  
only have "SELECT" rights to the database which is one way to  
increase security. Or you may have to change the password or even  
change the database driver. All those things are doable with DBI. Do  
a `perldoc DBI` to find out more.

The latest (stable) version of perl is 5.8.8 and soon 5.10 is due to  
appear soon, I don't understand why Apple ****ps such old versions of  
perl. Bad Apple.

	Jeremiah

On Oct 5, 2007, at 4:06 AM, Sherm Pendley wrote:

> On Oct 4, 2007, at 6:57 PM, Michael Barto wrote:
>
>> I am working with an old Perl Library (program module) written in  
>> Perl4 and Perl  5 depending who was "hacking the code". My program  
>> that calls it, uses -w and strict and has identified many syntax  
>> errors and so forth in the old library which I fixed. My problem  
>> is that this library needs two variables passed as globals to it  
>> to use in one of the subroutines, One of them is the $dbh variable  
>> created when the database is open. Another is a hash. Both these  
>> variables were initiated at the Main top most level of the program  
>> with "my". Unfortunately, this causes an error and the libraries  
>> does not see the global.
>
> My() isn't package scoped, it's file scoped.
>
> Our() is package scoped, so add a package declaration at the top of  
> each of your files, and in each of them declare $dbh with our.  
> Basically, like this, at the top of each file that uses the shared  
> variable:
>
>     package SuperNifty;
>     our $dbh;
>
> Alternatively, you could define $dbh in a module, and include it in  
> the module's @[EMAIL PROTECTED]
 or @[EMAIL PROTECTED]
 so that an alias to it is  
> ex****ted into the module user's namespace.
>
> sherm--
>
> Web Hosting by West Virginians, for West Virginians: http://wv-www.net
> Cocoa programming in Perl: http://camelbones.sourceforge.net
>
>
 




 5 Posts in Topic:
"my variable" at global level not being passed
mbarto@[EMAIL PROTECTED]   2007-10-04 15:57:31 
Re: "my variable" at global level not being passed
bva@[EMAIL PROTECTED] (B  2007-10-04 16:19:33 
Re: "my variable" at global level not being passed
mbarto@[EMAIL PROTECTED]   2007-10-04 18:56:53 
Re: "my variable" at global level not being passed
sherm@[EMAIL PROTECTED]   2007-10-04 22:06:56 
Re: "my variable" at global level not being passed
jeremiah@[EMAIL PROTECTED  2007-10-05 12:54:20 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Dec 5 0:17:53 CST 2008.