473,762 Members | 8,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sharing global vars with Explorer driving me insane...

9 New Member
I'm having issues sharing global variables with Explorer. This problem probably has a simple answer (as with most newbie questions).

The script.pl file:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use diagnostics; 
  5. use sigtrap; 
  6.  
  7. use vars qw($greeting1 $greeting2);
  8. use lib qw(.); 
  9.  
  10. # my modules
  11.  
  12. use ModB qw($greeting1 $greeting2);
  13. use ModA qw($greeting1 $greeting2);
  14.  
  15. $greeting1 = "Hey.";
  16. $greeting2 = "Yo.";
  17.  
  18. ModA::say_hi();
  19. ModB::say_hi();
Pretty straight forward. I have two modules to which I am passing two global scalars that I later want to display. Here's the code for the two modules, ModA and ModB respectively:

ModA.pm:

Expand|Select|Wrap|Line Numbers
  1. package ModA;
  2.  
  3. use strict;
  4.  
  5.  
  6. # debug?
  7. my $debug = 0;
  8.  
  9.  
  10.   BEGIN {
  11.     use Exporter ();
  12.  
  13.     @ModA::ISA         = qw(Exporter);
  14.     @ModA::EXPORT      = qw();
  15.     @ModA::EXPORT_OK   = qw($greeting1 $greeting2);
  16.  
  17.   }
  18.  
  19. use vars qw($greeting1 $greeting2);
  20.  
  21. #$VERSION = '0.01';
  22.  
  23. sub say_hi {
  24.  
  25.     print "ModA says " . $greeting2 . "\n";
  26.  
  27. }
  28.  
  29.  
  30. 1;
ModB.pm

Expand|Select|Wrap|Line Numbers
  1. package ModB;
  2.  
  3. use strict;
  4.  
  5.  
  6. # debug?
  7. my $debug = 0;
  8.  
  9.  
  10.   BEGIN {
  11.     use Exporter ();
  12.  
  13.     @ModB::ISA         = qw(Exporter);
  14.     @ModB::EXPORT      = qw();
  15.     @ModB::EXPORT_OK   = qw($greeting1 $greeting2);
  16.  
  17.   }
  18.  
  19. use vars qw($greeting1 $greeting2);
  20.  
  21. #$VERSION = '0.01';
  22.  
  23. sub say_hi {
  24.  
  25.     print "ModB says " . $greeting1 . "\n";
  26.  
  27. }
  28.  
  29.  
  30.  
  31.  
  32. 1;
  33.  
As you can see the two modules are identical aside from their names. My code works fine when I have just one module. However with two, it craps the bed. An interesting side note: the module that produces the error is always the one whose "use" declaration appears first in script.pl. For instance, when script.pl looks like this

Expand|Select|Wrap|Line Numbers
  1. .
  2. .
  3. .
  4. use ModB qw($greeting1 $greeting2);
  5. use ModA qw($greeting1 $greeting2);
  6. .
  7. .
  8. .
the error produces looks like this:

Expand|Select|Wrap|Line Numbers
  1. ./script.pl
  2. ModA says Yo.
  3. Use of uninitialized value in concatenation (.) or string at ModB.pm line 21 (#1)
  4.     (W uninitialized) An undefined value was used as if it were already
  5.     defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
  6.     To suppress this warning assign a defined value to your variables.
  7.  
  8.     To help you figure out what was undefined, perl tells you what operation
  9.     you used the undefined value in.  Note, however, that perl optimizes your
  10.     program and the operation displayed in the warning may not necessarily
  11.     appear literally in your program.  For example, "that $foo" is
  12.     usually optimized into "that " . $foo, and the warning will refer to
  13.     the concatenation (.) operator, even though there is no . in your
  14.     program.
  15.  
  16. ModB says 
However, switching the order of the "use" statements in script.pl will cause the other module to throw the error:

Expand|Select|Wrap|Line Numbers
  1. .
  2. .
  3. .
  4. use ModA qw($greeting1 $greeting2);
  5. use ModB qw($greeting1 $greeting2);
  6. .
  7. .
  8. .
produces

Expand|Select|Wrap|Line Numbers
  1. ./script.pl
  2. Use of uninitialized value in concatenation (.) or string at ModA.pm line 21 (#1)
  3.     (W uninitialized) An undefined value was used as if it were already
  4.     defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
  5.     To suppress this warning assign a defined value to your variables.
  6.  
  7.     To help you figure out what was undefined, perl tells you what operation
  8.     you used the undefined value in.  Note, however, that perl optimizes your
  9.     program and the operation displayed in the warning may not necessarily
  10.     appear literally in your program.  For example, "that $foo" is
  11.     usually optimized into "that " . $foo, and the warning will refer to
  12.     the concatenation (.) operator, even though there is no . in your
  13.     program.
  14.  
  15. ModA says 
  16. ModB says Hey.
I hope I've been clear with all that. I really don't get this and any help would be VERY much appreciated.

Thanks,

Pete
Sep 30 '07 #1
4 1755
pcaisse
9 New Member
Wow. I just realized that I could refer to variables in script.pl as $main::my_var. Is this considered bad practice if I'm just passing them one time from script.pl to my modules and not editing them?
Sep 30 '07 #2
KevinADC
4,059 Recognized Expert Specialist
Your code looks like you're just trying to get the hang of coding modules. If that is the case, it's not doing that very well. Normally you import functions into the main namespace from the modules, and pass variables/data in the system array instead of trying to share global variables. Modules generally have no idea what variables are in the main program, but the main program should know what is in the modules, at least the public interface parts of the module.
Sep 30 '07 #3
pcaisse
9 New Member
Your code looks like you're just trying to get the hang of coding modules. If that is the case, it's not doing that very well. Normally you import functions into the main namespace from the modules, and pass variables/data in the system array instead of trying to share global variables. Modules generally have no idea what variables are in the main program, but the main program should know what is in the modules, at least the public interface parts of the module.
Thanks for that clarification. It certainly makes sense for the modules to not know what variables are being used in the main program from a reusibility standpoint. I think I'm going to entirely rework the flow of my program.
Sep 30 '07 #4
KevinADC
4,059 Recognized Expert Specialist
Here is a sort of standard way to do wht you are trying:

script.pl

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use diagnostics; 
  4. use sigtrap; 
  5. use lib qw(.); 
  6. use ModB qw(say_hey);
  7. use ModA qw(say_hi);
  8.  
  9. my $greeting1 = "Hey.";
  10. my $greeting2 = "Yo.";
  11. say_hi($greeting1);
  12. say_hey($greeting2);
ModA.pm

Expand|Select|Wrap|Line Numbers
  1. package ModA;
  2.  
  3. use strict;
  4. # debug?
  5. my $debug = 0;
  6. our( @ISA, @EXPORT, @EXPORT_OK );
  7. require Exporter;
  8. @ISA        = qw(Exporter);
  9. @EXPORT     = qw();
  10. @EXPORT_OK  = qw(say_hi);
  11.  
  12. sub say_hi {
  13.  
  14.     print "ModA says " , shift , "\n";
  15.  
  16. }
  17. 1;
ModB.pm

Expand|Select|Wrap|Line Numbers
  1. package ModB;
  2.  
  3. use strict;
  4. # debug?
  5. my $debug = 0;
  6. our( @ISA, @EXPORT, @EXPORT_OK );
  7. require Exporter;
  8. @ISA        = qw(Exporter);
  9. @EXPORT     = qw();
  10. @EXPORT_OK  = qw(say_hey);
  11.  
  12. sub say_hey {
  13.  
  14.     print "ModA says ", shift , "\n";
  15.  
  16. }
  17. 1;
Of course this is just for learning purposes. It's rather silly to have a module with only one function But you see the main program knows what each module has to offer (the functions) but the modules have no idea what the main program is sending to them. They just echo (print) the value of the scalar variables sent in the function calls. You could code the module functions to have an idea about what type of data they expect to recieve: scalar, array, hash, glob, references, etc, and throw an error if something else is recieved.

If you throw in a constructor and return an object to the main program you have yourself an OO program. Then you can call the objects methods (functions) using the object.
Sep 30 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

14
3333
by: lkrubner | last post by:
If I set a variable at the top of my code like this: $name = "Lawrence"; It is now a global variable. If, later on, in a function, I want to do this: function uppercaseName() { global $name;
6
2026
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following code: #def put_file(file_id, delete=False): # """ Function to put the file on the FTP Server # """ # print " FTP for this file started"
16
2286
by: WaterBug | last post by:
When clicking on the following link from an email i.e - http://myserver/myapplication/myprogram.asp?urlvar1=some%20stuff&urlvar2=more%20stuff I get a server 500 error. With that same browser window open, if I click the link from the email again I get the desired page. The results are the same if the url is copied and pasted into the brower but the 'go' must be pressed twice. It appears that the server is trying to resolve the url vars...
0
1556
by: Emily | last post by:
Imagine a world where everybody shares and has faith in each other. We have all struggled at one time or another with our jobs or careers and have wondered if there was a better way to make a living. What if the solution was just to help each other, simply by giving and receiving. This would be a radical global experiment in faith. Imagine people all around the world connecting instantaneously and just giving and receiving money to each...
9
8656
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
2
3219
by: aidanhaylock | last post by:
Morning, This one is really driving me insane. I am developing a site for a client who doesn't particularly want to move their hosting away from their current provider. The current host are absolutely terrible with their support (shocking for a large hosting company) and I am running out of ideas. I usually use Dreamweaver to it's full potential when developing sites but due to the fact that the host won't allow external access to the...
3
1123
by: scripteaze | last post by:
Im tryin to call a var thats sitting in a function, example: class someclass(object): somevar = open(blah, 'r').readlines() def something(): for line in somevar: print line
0
1441
by: Gary Herron | last post by:
Jacob Davis wrote: Yuck, YUCK, YUCK! You are breaking *so* many good-programming-practices, I hardly know where to start. First off: A python global is not what you think. There are *no* program wide globals. There are only module wide globals. Also, the "global isglobal" is absolutely meaningless as anything declared there is a (module level) global by definition.
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9812
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5268
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.