Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Good Evening,

I do have a probably simple problem but I just can not fix it. Here is my point:

I have an own module (package) which is designed to create object-instances. In this package I have a "global" variable named %config. I put the word 'global' in quotation marks, because I think that's where my problem begins. I tried my, our and double-colon namespace syntax. This hash should be equal for all created objects. So far, so good. Every object is able to access %config. The package now contains a subroutine for reloading the (changed) config-file and storing its contents into %config after parsing. At this point the hash is either empty (or not accessible anymore?) for all object instances or did not change at all. That depends on how I declared the global hash. As you can see the sub-routine is the same as the one I call at the beginning of the package (to fill %config). I even tried almost-nonsense combinations in my desperation, but I am sure it is easily possible in any other way.

My program is very complex so I chose to post a simple (pseudo) takeout - The substantial is of importance for me. The _parse_file() sub-routine is purposely missing in order to keep clarity.
package Foo; use strict; my %config = _fill_config(); sub new { my $proto = shift(); my $status = shift() || 'normal'; my $package = ref($package) || $package; my $self = { status => $status }; bless($self, $package); return $self; } sub change_config { my $self = shift(); return unless $self->{status} ne 'admin'; %config = _fill_config(); } sub _fill_config { my %temp = _parse_file('config.cfg'); return %temp; }
Thank you very much for your suggestions, Monks.

BioHazard
reading between the lines is my real pleasure

In reply to Changing package variable via object-instance by BioHazard

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-10-01 17:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (3 votes). Check out past polls.

    Notices?