Howto share variables among script & module

Discussion in 'Perl Misc' started by Bernd Schneider, Apr 4, 2006.

  1. 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!
     
    Bernd Schneider, Apr 4, 2006
    #1
    1. Advertising

  2. Bernd Schneider <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 4, 2006
    #2
    1. Advertising

  3. 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
     
    Bart Van der Donck, Apr 4, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guadala Harry

    Share SQL DB Among Dozens of Sites

    Guadala Harry, Feb 21, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    326
    Peter Bromberg [C# MVP]
    Feb 21, 2004
  2. MK
    Replies:
    6
    Views:
    701
    noSpAm0000
    May 16, 2005
  3. red floyd
    Replies:
    1
    Views:
    491
    Zhang Yan
    Sep 6, 2003
  4. just_life
    Replies:
    0
    Views:
    697
    just_life
    Aug 1, 2007
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,373
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page