Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Changing package variable via object-instance

by BioHazard (Pilgrim)
on Aug 25, 2003 at 19:20 UTC ( #286466=note: print w/ replies, xml ) Need Help??


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':

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


Comment on Re: Changing package variable via object-instance
Select or Download Code
Re2: Changing package variable via object-instance
by bbfu (Curate) on Aug 25, 2003 at 20:58 UTC

    In test.pl:

    my $object = Foo->new('admin');

    In Foo.pm:

    sub change_config { my $self = shift(); return unless $self->{status} ne 'admin'; ... }

    change_config isn't doing anything if the status is equal to 'admin'. Try changing the status you're giving the object to something else.

    Also, you might want to look into using one of the Config::* modules (such as Config::INIFiles) for parsing your config file.

    bbfu
    Black flowers blossom
    Fearless on my breath

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://286466]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (15)
As of 2014-07-29 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (213 votes), past polls