in reply to Re^4: redacting from config hash
in thread redacting from config hash
I am very pleased to have 2 more excellent responses from the same sources. I hope I'm not trying anyone's patience for not latching on quicker, but we really are in the thick of it now and gaining ground. I present with an error that I don't understand after redesigning what I'm doing to be more descriptive.
The questions that haukex raised were partially dealt with by code that soonix wrote. What he wrote first worked, but the one where
my $ini = Config::Tiny->new; $ini->{$_} = $config{$_} for keys %config;
were substituted for the line used to instantiate the object, I got an error on line 38, which I marked. I'd be curious to know why, but having one way to do things is enough for now. It's probably the first time I've blessed anything in the world of bits and bytes, maybe appropriate to my future friar status
I won't be "redacting" anything at all now. I'm gonna use Config::Tiny to create and then draw values from an .ini file that is not in the filespace to be copied. It's better data hygiene and really the path forward if the goal is to share with others in ways where they can recreate their own critical values without need to be aware of the internals. Redaction will occur by not copying anything of the form configx.pm . Then I'm gonna alter my script to read in the values from that other location. As it's written, that will be indicated by the return value in the current caller.
What follow are 3 listings. The first is a working script that created an ini file successfully. The second is the current driver, 1.initialize.pl which calls the module that I'm writing to capture these functions, the third listing, called utils2.pm. What used to be called "redact" is being renamed "archive", and isn't even being called yet, because I have an error with "make_ini".
#!/usr/bin/env perl use 5.011; # implies strict + feature 'say' use warnings; use Data::Dumper; use Config::Tiny; use FindBin; use lib $FindBin::Bin; use config2; use constant { INIFILE => 'example3.ini', ENCODING => 'utf8' }; sub create { say Dumper \%config; #next line is equivalent to subsequent 2 lines my $ini = bless \%config, 'Config::Tiny'; #my $ini = Config::Tiny->new; #$ini->{$_} = $config{$_} for keys %config; $ini->write(INIFILE, ENCODING); say 'created ', INIFILE; } sub show { say 'reading ', INIFILE; my $ini = Config::Tiny->read(INIFILE, ENCODING); my %config = %$ini; #line 38 say Dumper \%config; } create() unless -e 'example3.ini'; show() ----------- #!/usr/bin/perl -w use 5.011; use lib "template_stuff"; use utils2; my $return = make_ini(); say "return is $return"; __END__ ------------- package utils2; require Exporter; use utils1; our @ISA = qw(Exporter); our @EXPORT = qw( archive make_ini create show); sub make_ini { use 5.011; # implies strict + feature 'say' use warnings; #thx soonix use Data::Dumper; use Config::Tiny; use FindBin; use lib $FindBin::Bin; use config2; use constant { INIFILE => '1.example.ini', ENCODING => 'utf8' }; sub create { use Path::Tiny; use config2; my $ini_path=path("/home/bob/Documents", "INIFILE"); say Dumper \%config; #next line is equivalent to subsequent 2 lines my $ini = bless \%config, 'Config::Tiny'; #my $ini = Config::Tiny->new; #$ini->{$_} = $config{$_} for keys %config; $ini->write( $ini_path, ENCODING ); say 'created ', $ini_path; return $ini_path; } sub show { say 'reading ', INIFILE; my $ini = Config::Tiny->read( INIFILE, ENCODING ); my %config = %$ini; #line 38 say Dumper \%config; } create() unless -e $ini_path; show() } sub archive { use strict; use warnings; use 5.010; use Path::Tiny; use config2; # old way of getting values my $sub_hash = "my_github"; my $email = $config{$sub_hash}->{'email'}; my $password = $config{$sub_hash}->{'password'}; my $tempdir = Path::Tiny->tempdir('backup_XXXXXX'); say "temp dir is $tempdir"; my $scratch_file = $tempdir->child( 'batch_01', '1.manifest.txt' )-> +touchpath; say "scratch_file is $scratch_file"; my $parent = $scratch_file->parent; say "parent is $parent"; chdir $tempdir unless $tempdir->subsumes( Path::Tiny->cwd ); system("pwd"); use Data::Dumper; print Dumper \%config; my $b = "dummy"; return $b; } 1;
Where I seem to have stumbled is combining the Path::Tiny and the Config::Tiny techniques:
$ ./1.initialize.pl Global symbol "$ini_path" requires explicit package name (did you forg +et to declare "my $ini_path"?) at template_stuff/utils2.pm line 47. BEGIN not safe after errors--compilation aborted at template_stuff/uti +ls2.pm line 53. Compilation failed in require at ./1.initialize.pl line 5. BEGIN failed--compilation aborted at ./1.initialize.pl line 5. $
To my eye, I have declared "my $ini_path" in the presence of the appropriate modules in the create() function. Maybe fresh eyes in the morning will help....
|
|---|