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

includes

I have one script with a include to another script.  How can I use the variables from the include script in the original scrip.

require "/user.pl";

this is my require statement.
0
brink1978
Asked:
brink1978
  • 4
  • 3
  • 2
2 Solutions
 
brink1978Author Commented:
for instance user.pl has 1 vars in it user name.  I want to user $userName in the script.
0
 
KavarCommented:
the safest way to do this....

in user.pl include a function that returns a reference to the variable

sub get_userName {
return \$userName;
}


0
 
lbertaccoCommented:
Don't use "require", but "do" as in:

do "/user.pl";

then you will be able to use the veriables defined in user.pl
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
KavarCommented:
like I was saying, the safest way is to use the "get" method because then you don't have to worry about variable names stepping on each other
0
 
lbertaccoCommented:
Kavar, you are right. However it depends on what you are doing. For example if you just want to print out the final values of variables defined in a different script, I'd go with the "do" solution.
0
 
KavarCommented:
yeah, thats a very good point :)
0
 
brink1978Author Commented:
With using
sub get_userName {
return \$userName;
}

How do I call it from the script?
0
 
KavarCommented:
hmmm, you should be able to use the import function of your package,
if your package does not have an import function, you can use "use"

use Modulename();
0
 
lbertaccoCommented:
Well I consider my answer more fit to the author question (he was trying with "require"...so just changing it to a "do" is much simpler than implementing accessory functions)
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.

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