Solved

Perl Modules & Main Program Variables.

Posted on 1997-12-30
10
600 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
binary to char / hexadecimal 5 104
Removing file extension within a file. 4 90
rename outfile before writing 2 71
PERL - Find newest folder 12 117
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

22 Experts available now in Live!

Get 1:1 Help Now