On Feb 21, 2006, at 1:08 AM, Mary Anderson wrote:
I have come to the conclusion that passing perl parameters from one
perl
subroutine to another just doesn't work in CGI. It looks like
everything
is passed by value, and the language is not set up to take refs. This
being the case, how best to handle the following piece of DBI code:
my $dbh = DBI:connect($connectionString); # connect to the
database
my $sth = $dbh->prepare($cmd); # prepare a command
Using my declares the listed variables to be local (lexically) the
variable to the enclosing block, file, or "eval".
perldoc -f my
References can be passed.
perldoc perlref
perldoc perlreftut
What I think that I have found is that
my $dbh = DBI:connect($connectionString) # dbh is a global
variable set
in a global context
I am going to assume that you do this at the beginning of your cgi
script. You would now have a database handle to use for the remainder
of the script.
..
sub fubar{
my $sth = $dbh->prepare($cmd);
}
seems to work, but it does not work to have the global variable set
inside
a subroutine and accessed from another. If it were a character
string, I
would use hidden() to pass the values from one subroutine to
another, but
the cgi macros are not set up to handle ref variables!
It sounds like you are trying to pass the handle to a separate
request to the server. Since it is scoped to the first script you
can't do that. You can transfer values in hidden fields of a form or
in a session that can be accessed by the next process. There are ways
to handle persistent database connections, although I have not worked
with them at this point. If I know I will need a handle I grab it at
the beginning of the script and then close it when I am done.
If I am right, that I can set $dbh globally and access it from some
subroutine, can I do this
if (something is true) {
my $dbh = DBI:connect($connectionString);
}
That will get you a database handle that is valid inside the
(something is true) block.
sub fubar{
my $sth = $dbh->prepare($cmd)
$dbh does not exist inside the fubar block.
Ed Pigg
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>