Solved

Perl Modules & Main Program Variables.

Posted on 1997-12-30
10
609 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans

696 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