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':
use strict;
use Foo;
my $object = Foo->new('admin');
$object->show_config();
sleep(15);
$object->change_config();
$object->show_config();
FILE 'Foo.pm':
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;
FILE 'config.cfg':
option1=value1
option2=value2
option3=value3
Output:
KEY: option1 VALUE: value1
KEY: option2 VALUE: value2
KEY: option3 VALUE: value3
KEY: option1 VALUE: value1
KEY: option2 VALUE: value2
KEY: option3 VALUE: value3
BioHazard
reading between the lines is my real pleasure
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.