> require "$LIBRARY_DIR/castime_html.pm";
> use CGI; # load CGI routines
> use strict 'vars';
> my $q = new CGI; # create new CGI object
>
> if (! $q->param()) {
> &draw_tnc();
> }
>
> So far so good except &draw_tnc() is in the required file
castime_html.pm
> and cannot access $q
>
> error is
> Can't call method "header" without a package or object reference at
> castime_html.pm line 29.
>
> I need the subs in the required file to have access to all methods and
> values in the $q object.
>
> How do I do this?
Well it all depends.
I'm going to lie a little bit in what follows to keep
things simple.
Let's say castime_html.pm has a code fragment:
package foo;
sub bar {
my $obj = shift;
if ($obj->param()) {
# whatever
}
};
and in your script you wrote:
foo::bar($q);
then $obj in castime_html.pm would be a reference
to the $q object.
But $obj would only be visible within the bar sub.
If castime_html.pm had another code fragment:
package foo;
sub qux {
$global = shift;
};
sub waldo {
if ($global->param()) {
# whatever
}
};
then there is a variable called $global that is
visible throughout all the perl code (your script,
the use and required modules, everywhere).
Of course, you would still need to call the qux
sub to set it to the $q object:
foo::qux($q);
But once you've done that, anywhere in your
code could access $q, by accesing $global
that refers to the $q object.
To access $global, you can use the longhand
version:
if ($foo::global->param()) {
or, if the current package is foo, you can use
the shorthand name:
package foo;
if ($global->param()) {
Depending on what you want to do, the above
might be useful, though it is effectively hacking
in a procedural answer to an object problem.
Basically, I recommend you go read Learning Perl.
(I don't want to discourage you from buying it now,
but a new edition is due out in a month or so. So
go buy the 2nd to save yourself a lot of grief over
the next month, then give it to another newbie in
a month and go buy the 3rd edition.)