Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Howto share variables among script & module

Reply
Thread Tools

Howto share variables among script & module

 
 
Bernd Schneider
Guest
Posts: n/a
 
      04-04-2006
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!
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      04-04-2006
Bernd Schneider <(E-Mail Removed)> wrote:

> 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.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Bart Van der Donck
Guest
Posts: n/a
 
      04-04-2006
Bernd Schneider wrote:

> 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]

--
Bart

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
How to share common code among pages in asp.net 2.0 just_life ASP .Net 0 08-01-2007 05:31 PM
Share common HTML code among different HTML files? MK HTML 6 05-15-2005 11:32 PM
Share SQL DB Among Dozens of Sites Guadala Harry ASP .Net 1 02-21-2004 06:50 PM
Re: How to share vector among different processes? red floyd C++ 1 09-06-2003 11:20 AM



Advertisments