• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 635
  • Last Modified:

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.
0
franklins
Asked:
franklins
  • 5
  • 3
  • 2
1 Solution
 
nanullnetCommented:
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
 
icdCommented:
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
 
franklinsAuthor Commented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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

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
 
nanullnetCommented:
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
 
franklinsAuthor Commented:
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
 
icdCommented:
I gather that my comment/answer was correct? If not then please feel free to reject this one.
0
 
franklinsAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now