I seem to have a small issue with my script where I need to call “tied” against a sub routine that is earlier in the the script so that I can access the functions that are related to the object that the hash is tied to. The problem is when I go to run the script it return the error “Can’t call method “SetWriteMode” on unblessed reference at cbc_encrypt_test.pl line 30.” At first I had no idea what it was talking about, I think the problem is due to me using a reference to point back at the subroutine which return the hash reference in the first place. Because as far I understand “config_file = \%cfg” in this case. After looking at perldoc about perlref’s I am still lost. I read over the document pretty well and did not see anything about referencing subroutines in the way I need to.
Here the entire script so far.
#!/usr/bin/perl
use strict;
use warnings;
use Term::ANSIColor;
use Config::IniFiles;
use Crypt::CBC;
start_script();
sub start_script {
system ("clear");
encrypt_password();
} # end start_script
sub config_file {
my $cfg_file = 'settings.ini';
my %cfg;
tie %cfg, 'Config::IniFiles', ( -file => "$cfg_file" );
return \%cfg;
} # end config_file
sub encrypt_password {
my $password = config_file()->{ESX}{password};
my $cipher = Crypt::CBC->new( -key => 'EF1FAD9B87F8365B242669E624FEB36CDBCCFEE0096CC45DDDCF6F5995E83F61',
-cipher => 'Rijndael'
);
my $encrypted_password = $cipher->encrypt_hex("$password");
chomp $encrypted_password;
config_file()->{ESX}{password} = $encrypted_password;
tied config_file()->SetWriteMode(0666);
tied config_file()->RewriteConfig();
return $encrypted_password;
} # end encrypt_password
Looks like a precedence issue. Running the construct through
B::Deparseshows the problem:So Perl first tries to call the method on the reference returned by
config_file(), and fails because that reference is not a blessed object (liketiedwould return).Instead try:
or better yet:
or
Also, depending on how
Config::IniFilesis written, you may run into problems where each call toconfig_file()returns a new object, so the aggregate calls you make don’t apply to each other properly. You should cache the object as shown in my answer to your previous question.Edit: I didn’t catch the missing dereference that ysth pointed out which would have been the second problem. Fixed above.