Solved

Perl Modules & Main Program Variables.

Posted on 1997-12-30
10
598 Views
Last Modified: 2006-11-17
How can I open up the variables in my main perl program to a module so that it can work with them? I'm only wanting to pass the variable names so that I can check and see if they are defined and then if they are defined, check thier contents against a pattern.

I'll start the bidding at 10 points. If it can't be done I'll give the 10 points, if it can be done I'll give 100.
0
Comment
Question by:franklins
  • 5
  • 3
  • 2
10 Comments
 
LVL 1

Expert Comment

by:nanullnet
ID: 1209408
If you call the module from inside the main perl script, and you do not redefine the same named variables in the module, the module should be able to see all variables in the main script.  So just try to call the variables from inside the module, and remember not to use the same variable names inside the module.
0
 
LVL 5

Expert Comment

by:icd
ID: 1209409
If you are using:-

require YourModule;

which is the recommended way of using module YourModule.pm then the variables in the Main module are *not* automatically available to the YourModule module. To make them available you need to individually import them as:-

*scalar = *main::scalar;
*anotherScalar = *main::anotherScalar;

Alternatively just use the full name in your program. i.e.

$localScalar = $main::fred + 1;

I think you can also use the following method:-

$localScalar = $::fred + 1;

since 'main' is the default.

I thinks these are correct, I just can't place my hand on the camel book to confirm all the details.

0
 

Author Comment

by:franklins
ID: 1209410
I'm reopened the question for icd to answer. I infact tried suggestion number one before asking the question.  But suggestion number two will work, because I seem to remember seeing something about it in my perl documentation. I just didn't know at the time what it was for.
0
 
LVL 1

Expert Comment

by:nanullnet
ID: 1209411
This is incorrect.  I use this every day.  If you require, all variables are able to be seen by the module, period.  If this does not work for you, then you have a syntactical problem.  Post your code if need be, but here is a proven example.

Script 1 named "require.pl":

#!perl
$var = "jimmy";
require "require2.pl";
&mod;

Script 2 named "require2.pl":

#!perl
sub mod {
      print "$var\n";
}
1;

This works.  Try it.
0
 

Author Comment

by:franklins
ID: 1209412
Please note, I asked about modules not two perl scripts.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:franklins
ID: 1209413

Here is the test program code.


use ppwww;

$var1 = "Me two";
checkVar("H","E","var1");


Here is the module code.

require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(checkVar);

use vars qw($Version);
$Version = .01;

sub checkVar {
     my $OutPutType = shift(@_);
     if ($OutPutType eq "H") {
          my $r = "<BR>\n";
          my $q = "<BLOCKQUOTE>";
          my $qc = "</BLOCKQUOTE>";
          } else {
          my $r = "\n";
          my $q = "     ";
          }
     foreach (@_) {
          $var = shift(@_);
          $var = shift(@_);
          $var = "main::"."$var";
          print "\"$var\"\n";
          print "\"".$$var."\"\n";          if (defined($$var)) {
               if ($_ eq "E") {
                    if ($$var=~m/[^A-Za-z0-9@_.]/) {
                         print "works";
                         } else {
                         print "maybe";
                         }
                    print "\"".$$var."\"\n";
                    }
               } else {
               return "\"$var\" is undefined. $q Possible error in form.";
               }
          }
     }


Inside the module, $var1 from the main program is not available to the module without using the $main::var1;  This code above works correctly without a problem.  Two perl scripts are not the answer because I was looking for away to do this with a script and a module.  I tried doing $var1 in the module or $$var as would be necessary from inside the module because all I'm passing is the variable name.  When it printed it printed "".
0
 
LVL 1

Expert Comment

by:nanullnet
ID: 1209414
Yes, strictly speaking, with a "module" you must use the full $main::var syntax.  Why is there a problem with that?  The variable is visible to the module though, it is just rereferenced in the module context.
0
 

Author Comment

by:franklins
ID: 1209415
I didn't know that and that is what I was looking for.  Your answer was for two scripts not a script and a module.  That was why I rejected it.
0
 
LVL 5

Accepted Solution

by:
icd earned 100 total points
ID: 1209416
I gather that my comment/answer was correct? If not then please feel free to reject this one.
0
 

Author Comment

by:franklins
ID: 1209417
Yes, it was correct.  It was what solved the problem and allowed me to complete the portion of the module I was working on.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now