Hi Monks, I want to share a variable across multiple files. For this, I wrote one module, my_module.pm as below:
#!/usr/bin/perl
package my_module;
require Exporter;
our @ISA = qw/Exporter/;
our @EXPORT = qw/@my_var change_var/;
our @my_var = (1..10);
sub change_var{
@my_var = (20..30);
}
1;
I used the module in 1.pl as below:
use my_module;
print "@my_var\n"; # prints 1..10
change_var();
print "@my_var\n"; # prints 20..30
system("perl 2.pl"); # calling another script
exit;
The other script 2.pl is as below:
use my_module;
print "@my_var\n"; # prints 1..10 but expecting it to be 20..30
The changes made to the variable @my_var in 1.pl is not reflected in 2.pl. I want to make the variable @my_var global. Please let me know where I am wrong. Please suggest if I should take any other approach.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.