in reply to Changing package variable via object-instance
Hi,
Thanks for the quick replies. There's a mistake in the constructor in line 10. Must be $proto and not $package.
The code I posted is not the code of my complex program. It is an equivalent piece that is in a symanticly point of view the same. I now tested it and it did not work. I changed flattening into references and it presented the same results to me: I changed the first line of config.cfg to option1=HASBEENCHANGED while sleep(15).
FILE 'test.pl':
reading between the lines is my real pleasure
Thanks for the quick replies. There's a mistake in the constructor in line 10. Must be $proto and not $package.
The code I posted is not the code of my complex program. It is an equivalent piece that is in a symanticly point of view the same. I now tested it and it did not work. I changed flattening into references and it presented the same results to me: I changed the first line of config.cfg to option1=HASBEENCHANGED while sleep(15).
FILE 'test.pl':
FILE 'Foo.pm':use strict; use Foo; my $object = Foo->new('admin'); $object->show_config(); sleep(15); $object->change_config(); $object->show_config();
FILE 'config.cfg':package Foo; use strict; my $config = _parse_file(); sub new { my $proto = shift(); my $status = shift() || 'normal'; my $package = ref($proto) || $proto; my $self = { status => $status }; bless($self, $package); return $self; } sub change_config { my $self = shift(); return unless $self->{status} ne 'admin'; $config = _parse_file(); } sub show_config { foreach (keys(%{$config})) { print "KEY: $_\tVALUE: $$config{$_}\n"; } } sub _parse_file { my $filename = 'test.cfg'; my %temp; open(CONFIG, "<$filename") or die "can not open!\n"; while (<CONFIG>) { chomp($_); my ($key, $value) = $_ =~ m/^(\w+)=(.*)$/; $value ||= ''; $temp{$key} = $value if ($key); } close(CONFIG); return \%temp; } 1;
option1=value1 option2=value2 option3=value3Output:
KEY: option1 VALUE: value1 KEY: option2 VALUE: value2 KEY: option3 VALUE: value3 KEY: option1 VALUE: value1 KEY: option2 VALUE: value2 KEY: option3 VALUE: value3BioHazard
reading between the lines is my real pleasure
In Section
Seekers of Perl Wisdom