in reply to Re^2: redacting from config hash
in thread redacting from config hash

if we're to imitate the syntax in my source it begins: our %config = (

You can get this kind of output from my code by changing Data::Dumper->new([$CONFIG],['$CONFIG']) to Data::Dumper->new([$CONFIG],['*config']) (change the variable names as you like); you'd just have to prepend the our yourself. See also the Data::Dumper docs.

What makes it very difficult is that we seem to be acting on perl syntax itself.

Yes, that's a pretty complex topic, so soonix's suggestion to use a different configuration file format is a good way to avoid that issue.

Variable "$config" is not imported at template_stuff/utils2.pm line 61 +. Global symbol "$config" requires explicit package name (did you forget + to declare "my $config"?) at template_stuff/utils2.pm line 61.
Why is my syntax on the config hash clashing with that provided by man Config::Tiny and haukex?

You've done use config2;, which should give you a hash %config, which you can confirm if you comment out the line with ->write: do you see the output of print Dumper \%config;? Then, you're doing $config->write, which means "call the method write on the object $config" - not the hash %config! That's why Perl complains about the missing variable $config.

It's possible to create a new Config::Tiny object and then copy over the config from config2.pm's %config into that object. However, I am wondering what the goal of your code is here. Are you trying to automate the conversion from config2.pm into an INI format? If the config is long, I can understand that, but if it's as short as you showed, why not do that step once, by hand? You can then use a method similar to what I showed to create a redacted version, or, again, since that's something you'd probably only be doing once (?), you can do that by hand as well.

Taking a step back: My understanding so far is that you want to provide your code for download for others to use, is that right? And you're figuring out a way to disentangle the stuff you've written for yourself (config files etc.) from the general stuff that you want to distribute to everyone? That's pretty common, and a situation I've been in plenty of times. I've found the best approach is to look at it from a different angle and put yourself in the user's shoes, as if you knew nothing about the internals of the package: what steps would a user who downloads the package have to take to set it up? How would you describe those steps in your README for the user to follow? And then, what do you as the developer have to provide to make that as easy as possible?

Replies are listed 'Best First'.
Re^4: redacting from config hash
by Aldebaran (Curate) on Aug 02, 2018 at 23:09 UTC
    what steps would a user who downloads the package have to take to set it up? How would you describe those steps in your README for the user to follow? And then, what do you as the developer have to provide to make that as easy as possible?

    It seems to me that a user would want a one session experience to populate his/her critical data. Thus, I've been removing hard-coded values one by one. What remains is a hard-coded path to an ini file, which I think is on the verge of working, but vexingly, does not. This is an analog to what I've used before to instantiate the sftp object. Perl thinks this is illegal:

    sub get_tiny{ use 5.011; use warnings; use Net::SFTP::Foreign; use Config::Tiny; use Data::Dumper; my $ini_path = qw( /home/bob/Documents/html_template_data/3.values.ini + ); say "ini path is $ini_path"; my $sub_hash = "my_sftp"; my $Config = Config::Tiny->new; $Config = Config::Tiny->read( $ini_path , 'utf8' ); say Dumper \%Config; my $domain = $Config{$sub_hash}->{'domain'}; my $username = $Config{$sub_hash}->{'username'}; my $password = $Config{$sub_hash}->{'password'}; my $port = $Config{$sub_hash}->{'port'}; #dial up the server say "values are $domain $username $password $port"; my $sftp = Net::SFTP::Foreign->new( $domain, user => $username, port => $port, password => $password) or die "Can't connect: $!\n"; return $sftp; }

    List of compiler complaints:

    $ ./1.qy1.pl Global symbol "%Config" requires explicit package name (did you forget + to declare "my %Config"?) at template_stuff/html4.pm line 215. Global symbol "%Config" requires explicit package name (did you forget + to declare "my %Config"?) at template_stuff/html4.pm line 216. Global symbol "%Config" requires explicit package name (did you forget + to declare "my %Config"?) at template_stuff/html4.pm line 217. Global symbol "%Config" requires explicit package name (did you forget + to declare "my %Config"?) at template_stuff/html4.pm line 218. BEGIN not safe after errors--compilation aborted at template_stuff/htm +l4.pm line 239. Compilation failed in require at ./1.qy1.pl line 5. BEGIN failed--compilation aborted at ./1.qy1.pl line 5. $

    I just don't see where I'm not correctly imitating the syntax shown on Config::Tiny.

      I just don't see where I'm not correctly imitating the syntax shown on Config::Tiny

      Part way through you've stopped using a hashref and started using a hash. Just stick with the hashref. eg:

      my $Config = Config::Tiny->new; $Config = Config::Tiny->read( $ini_path , 'utf8' ); say Dumper $Config; my $domain = $Config->{$sub_hash}->{'domain'}; my $username = $Config->{$sub_hash}->{'username'}; my $password = $Config->{$sub_hash}->{'password'}; my $port = $Config->{$sub_hash}->{'port'};

      HTH.

        thx hippo, your code as posted worked:

        my $domain = $Config->{$sub_hash}->{'domain'}; my $username = $Config->{$sub_hash}->{'username'}; my $password = $Config->{$sub_hash}->{'password'}; my $port = $Config->{$sub_hash}->{'port'};

        but I was curious whether it would still work without the second arrows, and it does:

        sub get_tiny { use 5.011; use warnings; use Net::SFTP::Foreign; use Config::Tiny; use Data::Dumper; my $ini_path = qw( /home/bob/Documents/html_template_data/3.values.i +ni ); say "ini path is $ini_path"; my $sub_hash = "my_sftp"; my $Config = Config::Tiny->new; $Config = Config::Tiny->read( $ini_path, 'utf8' ); say Dumper $Config; # -> is optional between brackets my $domain = $Config->{$sub_hash}{'domain'}; my $username = $Config->{$sub_hash}{'username'}; my $password = $Config->{$sub_hash}{'password'}; my $port = $Config->{$sub_hash}{'port'}; #dial up the server say "values are $domain $username $password $port"; my $sftp = Net::SFTP::Foreign->new( $domain, user => $username, port => $port, password => $password ) or die "Can't connect: $!\n"; return $sftp; }

        Thx for your comment.

      ... my $Config = Config::Tiny->new; $Config = Config::Tiny->read( $ini_path , 'utf8' ); say Dumper \%Config; my $domain = $Config{$sub_hash}->{'domain'}; my $username = $Config{$sub_hash}->{'username'}; my $password = $Config{$sub_hash}->{'password'}; my $port = $Config{$sub_hash}->{'port'}; ... Global symbol "%Config" requires explicit package name (did you forget + to declare "my %Config"?) at template_stuff/html4.pm line 215. Global symbol "%Config" requires explicit package name (did you forget + to declare "my %Config"?) at template_stuff/html4.pm line 216. Global symbol "%Config" requires explicit package name (did you forget + to declare "my %Config"?) at template_stuff/html4.pm line 217. Global symbol "%Config" requires explicit package name (did you forget + to declare "my %Config"?) at template_stuff/html4.pm line 218. ...

      Config::Tiny returns a (blessed) reference to a hash, which is stored in the scalar $Config, so there is no %Config hash available. Both \%Config and $Config{$sub_hash} are however attempting to access a hash named %Config. Instead of Dumper \%Config, do Dumper $Config, and instead of $Config{$sub_hash}, say $Config->{$sub_hash}, and it should work. See perlreftut and perlref.

        I have a lot to learn when it comes to the syntax of hashes, and needed to be re-reminded of the abbreviation and the arrow rule. We've traversed a fairly wide arc in this thread from beginning with redacting to implementing Config::Tiny. I didn't get very far on the archiving itself and work on that presently. I do think that better usenet and perlmonks threads end when OP posts code that works. I ended up creating a new script that a) holds example values for the user to change when they customize their settings, and b) creates the .ini file that will populate values for dialing up a server. I'll post STDOUT, the .ini file, and then the script itself

        Of course, none of what works in the above code would have happened without the excellent comments and provision of source that this forum provided. Thank you.