Howto share variables among script & module

  • Thread starter Bernd Schneider
  • Start date
B

Bernd Schneider

Hello!

I am currently having some difficulties to share some variables.

I got a variable $a in my script index.pl.
Now I am using a module A.pm in this script.

In the script I got the variable $cgi (because I am using the Module
CGI.pm). How can I give this variable to the module so that it can be
used globally in the module? I do not want to rewrite all my functions
to give it as a parameter.

Is there a possibility to do this? Or is it not possible?

Would there be another approach to make the CGI Variables visible in my
module A.pm?

Thanks in advance!
 
T

Tad McClellan

Bernd Schneider said:
I am currently having some difficulties to share some variables.


Here is a good tutorial on Perl's two different systems of
variables and how they are scoped:

"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html

I got a variable $a in my script index.pl.


That is its short name.

Its long name is $main::a (assuming it is not a lexical (my) variable)

(The $a variable is special, it is used in sort()ing, so it is
best not to use that name for other things.)

Now I am using a module A.pm in this script.

In the script I got the variable $cgi


That would be $main::cgi

(because I am using the Module
CGI.pm).


You can choose any variable name you like, it does not have
to "match" the module's name.

You never mention needing access to $a in A.pm, only $cgi, so
I wonder why you are even mentioning a variable named $a
in your post...

How can I give this variable to the module so that it can be
used globally in the module?


You don't need to "give" it at all, A.pm can access them
using their long names.

I do not want to rewrite all my functions
to give it as a parameter.


If you hadn't designed your code to use global variables, then
this problem would have never happened in the first place.

Bad design decisions can cost you later...

Is there a possibility to do this? Or is it not possible?


Sure, see above and elsewhere.

Would there be another approach to make the CGI Variables visible in my
module A.pm?


Design your code to communicate with function arguments rather
than through global variables.
 
B

Bart Van der Donck

Bernd said:
I got a variable $a in my script index.pl.
Now I am using a module A.pm in this script.

In the script I got the variable $cgi (because I am using the Module
CGI.pm). How can I give this variable to the module so that it can be
used globally in the module? I do not want to rewrite all my functions
to give it as a parameter.

Is there a possibility to do this? Or is it not possible?

Would there be another approach to make the CGI Variables visible in my
module A.pm?

Variables that were declared in CGI.pm, can easily be imported
(globally) into A.pm.

package [...]; # first line of A.pm
BEGIN { import CGI }
[A.pm code starts]
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top