in reply to Accessing module variables

Hello talexb,

If you have many modules with such data (really password in modules? I'm sure you are aware already of this..) I'd consider a radical change: parse everything with PPI and produce many distinct modules with a lonely accessor, or better put everything in a db.. anyway..

cat Varz.pm package Varz; use Exporter; our @EXPORT = ( $User, $Password, $Url, $Prefix ); our $User = 'Joe'; our $Password = 'Secret123'; our $Url = 'www.example.com'; our $Prefix = 'EX'; 1;

You can just copying vaules from one package to your current one as in:

perl -Mstrict -MVarz -we "our($User, $Password, $Url, $Prefix); map{ $ +main::{$_} = $Varz::{$_} } keys %Varz:: ; print join qq(\n),$User, $ +Password, $Url, $Prefix" Joe Secret123 www.example.com EX

If you want to get a bit mad about symbol table, I recently moved my library

L*

PS you may want to grep out unwanted elements: grep{$_ !~/export|begin/i}keys %Varz::

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Accessing module variables
by talexb (Chancellor) on Apr 19, 2018 at 22:49 UTC
      If you have many modules with such data (really password in modules? I'm sure you are aware already of this..)

    I wanted something quick that wouldn't involve me putting credentials into the scripts themselves .. and would also allow the credentials to be shared between multiple scripts. Putting them in a module that wasn't covered by version control seemed to be the easy solution.

    In hindsight, a better way would have been to create a credentials module that would do a lookup in a JSON configuration file .. just so I don't have to copy the four lines of code to open, read and transform JSON into a Perl data structure. That's for the future, I guess.

    Anyway, I have a solution that works fine right now. Thanks for the feedback.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.