You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Tomáš Bažant <tb...@suse.cz> on 2009/07/21 09:29:36 UTC

mod_perl / CGI.pm and 'our' variables

Hi 

i'm new here and have a problem i have not been able to solve for 2 days
now:

i'm writing a web application using %SUBJ% and would like to set 'our
$dbh = ...' so that i do not need to pass db handler as an argument to
functions in different packages every time i need. but this 'global'
variable is not seen inside imported package as $::dbh (as it is in
normal non-mod_perl application).

Any idea?



-- 
Tomáš Bažant <tb...@suse.cz>
Novell, SUSE Linux s.r.o.


Re: mod_perl / CGI.pm and 'our' variables

Posted by Perrin Harkins <ph...@gmail.com>.
2009/8/5 Tomáš Bažant <tb...@suse.cz>:
> yest that works for me if all the packages are in the same file, but if
> i put the Bar package into a separate file and import it with 'use'
> directive, $::dbh always returns undef. or could it be that i am using a
> closure? i'll try to avoid closures...

It's not a closure.  You just need to understand Perl's variable
naming and package rules a little better.  If there's something
specific you want to show us, we could probably spot the scoping
problem.

- Perrin

Re: mod_perl / CGI.pm and 'our' variables

Posted by Perrin Harkins <ph...@gmail.com>.
2009/7/21 Tomáš Bažant <tb...@suse.cz>:
> i'm writing a web application using %SUBJ% and would like to set 'our
> $dbh = ...' so that i do not need to pass db handler as an argument to
> functions in different packages every time i need. but this 'global'
> variable is not seen inside imported package as $::dbh (as it is in
> normal non-mod_perl application).

The scoping of variables is not changed by mod_perl.  When you declare
a package variable, you should be able to see it from other packages,
but you won't be able to refer to it as $dbh except in the package
where you declared it.

Example:

package Foo;
our $dbh;

$dbh->ping; # works

package Bar;
$dbh->ping; # fails

You can still use the fully-qualified name to get at it: $Foo::dbh.

- Perrin