Re: global variables defined in an external file
by choroba (Cardinal) on May 06, 2013 at 15:49 UTC
|
Without seeing any code or signs of "no success", one can only speculate. You probably did not use Exporter correctly.
| [reply] |
Re: global variables defined in an external file
by Old_Gray_Bear (Bishop) on May 06, 2013 at 16:00 UTC
|
Whenever I see 'global variables' I immediately think "Configuration File".
There are many, many modules on CPAN in the Config name-space. My particular favorites are Config::Simple and Config::Tiny.
Isolating your global variables into one place and explicitly calling that place 'configuration' is a Goodness for the maintainer.
----
I Go Back to Sleep, Now.
OGB
| [reply] |
|
package x;
use warnings;
use strict;
use Exporter 'import';
our @EXPORT = qw/$a_template $sc/;
our $a_template = "a12 a10 a5";
our $sc = 'All';
our @ar = qw/the variables/;
our %ha = (exported => ' succesfully.');
1;
many thanks | [reply] [d/l] |
Re: global variables defined in an external file
by NetWallah (Canon) on May 06, 2013 at 15:50 UTC
|
Show us the last thing you tried, or preferably, some small sample code that compiles, and represents what you are trying to do.
Alternatively, read up and follow the instruction in Exporter.
"I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
-- Dr. Cox, Scrubs
| [reply] |
|
use strict;
use warnings;
package x;
use Exporter;
my $VERSION = 1.00;
my @EXPORT = ();
my @ISA = qw(Exporter);
my $a_template = "a12 a1 a4 a11 a2 a10 a12 a20 a15 a15 a1 a10 a1 a1 a
+1 a2 a2 a1 a3 a3 a9 a1 a15 a2";
@EXPORT = qw(xxx $a_template);
sub xxx {print "in package x\n";}
1;
| [reply] [d/l] |
|
| [reply] |
|
my creates a block-scoped variable, while you need to create (using vars or our, as already suggested) a package-scoped variable (available in the symbol table), so it actually can be exported.
Read Coping with Scoping for more information on this topic.
| [reply] |
Re: global variables defined in an external file
by Anonymous Monk on May 06, 2013 at 15:48 UTC
|
Really, the question to ask is how many are the global variables? I know it's good practice not Export variables, but if it must be done, you can do:
The pm file:
package Def;
use warnings;
use strict;
use vars qw(@ISA @EXPORT $foo $bar $boo);
BEGIN{
require Exporter;
Exporter->import();
}
@ISA = qw(Exporter);
@EXPORT = qw($foo $bar $boo);
1;
The file which uses the pm file
#!/usr/bin/perl
use warnings;
use strict;
use Def; # the variable defined module
$foo = 'you can\'t foo me';
$bar = 'Let\'t meet in the bar';
$boo = 'The fans boo you today';
print $boo,$foo,$bar;
| [reply] [d/l] [select] |
|
It is the ohter way round:
Def.pm:
package Def;
use warnings;
use strict;
use Exporter 'import';
our @EXPORT = qw/$sc @ar %ha/;
our $sc = 'All';
our @ar = qw/the variables/;
our %ha = (exported => ' succesfully.');
__PACKAGE__
The script:
#!/usr/bin/perl
use warnings;
use strict;
use Def;
print "$sc @ar ", %ha;
| [reply] [d/l] [select] |
|
thanks for your very clear response....however, I'd like to do this
$foo = 'you can\'t foo me';
$bar = 'Let\'t meet in the bar';
$boo = 'The fans boo you today';
in the module and reference these variables in the calling program as: print $boo, etc.
| [reply] [d/l] |
Re: global variables defined in an external file
by Laurent_R (Canon) on May 06, 2013 at 23:22 UTC
|
Hum, just one question: are you really sure that you need all these global variables?
I do not have a religious zeal against global variables, but I know by experience it is often better to avoid them when possible (provided this does not lead you to stupidly complicated contortions). But still, almost every time I am using one, I am asking myself whether this is the best way to go.
| [reply] |
Re: global variables defined in an external file
by fionbarr (Friar) on May 06, 2013 at 15:44 UTC
|
| [reply] |
Re: global variables defined in an external file
by educated_foo (Vicar) on May 07, 2013 at 14:05 UTC
|
| [reply] [d/l] |