Passing an object to a module

G

gmills

Hello -

I am creating a module that does some work for various CGI in my
system. I would like to pass the CGI object to subroutines in my
module, but I can't figure out how to do it. Right now, I'm taking a
reference to the variable that holds the object in my CGI, like this:

use CGI;
use MYMODULE;

$q = new CGI;

MYMODULE::subroutine(\$q);

But if, for example, in MYMODULE, I do this:

use CGI;

sub subroutine {

$qref = shift;
$q = $$qref;

foreach $name ($q->params) {


I get the message that CGI::params is an undefined subroutine, when it
should be the names of the parameters passed to the CGI script.

Can anyone tell me what I am doing wrong?

Thanks;

Garey Mills
 
S

Scott Bryce

gmills said:
Hello -

I am creating a module that does some work for various CGI in my
system. I would like to pass the CGI object to subroutines in my
module, but I can't figure out how to do it. Right now, I'm taking a
reference to the variable that holds the object in my CGI, like this:

<code snipped>

That variable is a reference to a hash.

use strict;
use warnings;
use CGI;

my $query = new CGI;

my $string = somesubroutine($query);

print "Content-type: text/plain\n\n";
print $string;

sub somesubroutine
{
my $cgi = shift;

return $cgi->param('abc')
}
 
B

Brian McCauley

That variable is a reference to a hash.

"That" is unclear.

By which I mean that is unclear to what you are referring by the
pronown "that".

The "variable that holds the [CGI] object" that the OP refers to is a
variable that holds a reference to a thingy (that happens to be a hash)
that's been blessed into the class 'CGI'.

I think the point you are trying to make is that objects in Perl are
always handled via references so an extra level of indirection is
unecessary.
 
J

Jim Gibson

gmills said:
Hello -

I am creating a module that does some work for various CGI in my
system. I would like to pass the CGI object to subroutines in my
module, but I can't figure out how to do it. Right now, I'm taking a
reference to the variable that holds the object in my CGI, like this:

use CGI;
use MYMODULE;

$q = new CGI;

MYMODULE::subroutine(\$q);

But if, for example, in MYMODULE, I do this:

use CGI;

sub subroutine {

$qref = shift;
$q = $$qref;

foreach $name ($q->params) {


I get the message that CGI::params is an undefined subroutine, when it
should be the names of the parameters passed to the CGI script.

There is no params() method. Call param() in list context without
arguments to get the names of the parameters passed to the CGI script.

See 'perldoc CGI' for available methods and how to use them.
 
S

Scott Bryce

Brian said:
I think the point you are trying to make is that objects in Perl are
always handled via references so an extra level of indirection is
unecessary.

Yes. That is the point I was trying to make. Thank you for clarifying.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top