-1

I am writing a Perl script that is run by a user and makes use of the current Linux environment as variables and other variables as well. The environment settings may change and be different from what they were originally.

However, I'm trying to use self-contained Perl Modules and need to be able to access these variables. What is the best practice to go about doing this? I can just pass along 10 variables when I create an object using the Perl Module, but that seems excessive...

Thanks

Eugene K
  • 3,381
  • 2
  • 23
  • 36

1 Answers1

3

The environment variables are accessible from anywhere in the global %ENV hash:

print $ENV{HOME};

If you are creating objects, they probably have some attributes (being the objects hashes, arrays or even inside out objects...) Just store the relevant values into the attributes, e.g.

my $obj = Some::Package->new( name    => 'Homer',
                              surname => 'Simpson',
                              city    => 'Springfield',
                              # ... 7 more
                            );
choroba
  • 231,213
  • 25
  • 204
  • 289
  • 1
    Also, a good option is to make the object [singleton](http://search.cpan.org/perldoc?Class%3A%3ASingleton). – Eugene Yarmash Aug 24 '12 at 22:05
  • @eugeney Well, it *is* an option, but not necessarily a good one - google "singleton bad" for lots of interesting discussion on that both on and off SO. – Bill Ruppert Aug 25 '12 at 12:10
  • The $ENV variables can change though to my understanding, if the script gets forked off into another terminal. The $ENV variables were used as an example as well. However I do like the 2nd suggestion of using the "( name => 'value'," format. – Eugene K Aug 27 '12 at 17:27