Home > Archive > PERL CGI Beginners > February 2006 > Passing Perl parameters in a cgi-script
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Passing Perl parameters in a cgi-script
|
|
| Mary Anderson 2006-02-21, 3:55 am |
|
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
What I think that I have found is that
my $dbh = DBI:connect($connectionString) # dbh is a global variable set
in a global context
...
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!
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);
}
sub fubar{
my $sth = $dbh->prepare($cmd)
I've been wrestling with this problem for several hours, now, and would
appreciate hearing from someone who knows more about this!
Thanks.
Mary Anderson
| |
| Ed Pigg 2006-02-21, 6:55 pm |
|
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
|
|
|
|
|