Link to home
Start Free TrialLog in
Avatar of franklins
franklins

asked on

Perl Modules & Main Program Variables.

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.
Avatar of nanullnet
nanullnet

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

Avatar of franklins

ASKER

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.
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.
Please note, I asked about modules not two perl scripts.

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 "".
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of icd
icd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, it was correct.  It was what solved the problem and allowed me to complete the portion of the module I was working on.