Reputation: 1247
I am new at configuring perl and I'm trying to find the best way to set some common variables. I know there are lots of ways of doing this but I want to find the "best" ;). I came a crossed the .perl_env file in my root dir but can't find much documentation on it. Is this a place I can keep variables like BINDIR and VARDIR (locations that I use in my perl scripts) or is there a better way. Thanks!!
Upvotes: 0
Views: 252
Reputation: 20280
Like everyone else, I've never heard of a .perl_env
file. Also I'm assuming you mean that you want to set Perl global variables, since if you mean to set system global variables (environment variables) you should be doing that in a Bash script.
Anyway, if you really wanted to do this I would make a file named MyGlobalVariables.pm
and place it in your PERL5LIB directory.
package MyGlobalVariables;
use strict;
use warnings;
use parent 'Exporter';
our @EXPORT = qw/$SomeVar/;
our $SomeVar = 'Some Value';
Remember that each variable must be added to the @EXPORT
array.
Then in your scripts you can write use MyGlobalVariables;
and voila, there they are. This mechanism is a lot like a custom style file in LaTeX; of course that is a macro language.
All that said, I really don't think this is a good idea. Why? Two reasons:
my
rather than our
) unless there is a real reason to do so.Upvotes: 2
Reputation: 9026
For global variable like that, I have them set in the environment (I use Windows, but obviously you can do the same in Unix). In Perl, the environment is stored in the %ENV
variable (part of the system.) You would refer to your BINDIR as $ENV{BINDIR}
.
Upvotes: 1
Reputation: 185504
If you want to deal with a configuration file to share variables between scripts, see the following example :
INPUT file:
$ cat /tmp/config
[name1]
val1=foobar
val2=qux
[name2]
val1=10
val2=100
Perl code :
$ perl -we '
use Data::Dumper;
use Config::IniFiles;
my $ini = new Config::IniFiles(
-file => "/tmp/config", -allowcontinue => 1
);
print Dumper $ini->{"myparms"};
'
OUTPUT :
$VAR1 = {
'name2' => [
'val1',
'val2'
],
'name1' => [
'val1',
'val2'
]
};
If instead you need a shell
variable to share, by example if using bash
:
$ echo "export MYVAR=foobar" >> ~/.bashrc
$ . !$
$ perl -le 'use Env qw/MYVAR/; print $MYVAR;'
Upvotes: 1
Reputation: 386256
Hum, I've never heard of .perl_env
, and it's not in the documentation:
$ perldoc perlrun | grep .perl_env || echo "not found"
not found
But let's give it a try anyway.
$ echo '$ENV{FOO}="BAR";' >~/.perl_env
$ FOO=BAZ perl -E'say $ENV{FOO}'
BAZ
Nope, doesn't work.
Upvotes: 2