Pathologically Eclectic Rubbish Lister | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
I do not know if it will be useful in your case, but this is something I did a few years ago (I would probably do it slightly differently nowadays, but it worked and is still working). I created a module to simplify access to a low-level API to an application database, with higher level functions hiding from the user the long and complicated succession of calls to the API for any action on the database. Besides this simplification of the function calls, I also wanted to simplify the list of arguments passed to each function called. Many of these arguments were just the same throughout the program: database schema ID, connection ID, transaction ID, error buffer ID, etc. What I did was to create a lexical hash global to the module. And I had an init_connection function that used a hashref to populate the hash. Then, any other function within the module could use a ref to the same hash to retrieve the values it needed. Because of the refs to this hash, the hash became a persistent object within the module, even though no OO syntax was needed to do it. The module as a whole acted as a closure in which each function could access to the hash elements it needed. Two of hash elements were passed by the main program to the init function, the other were calculated within the init function, and then they were all stored in the hash for further use by all the other functions of the module, so that the main program did not have to worry about all these persistent parameters, making the module's functions calling syntax much simpler. Going further in the same direction and forgetting about the specific case I was talking about, this is how you could possibly make one variable persistent in a module and create an accessor and a mutator to it, without having to change your syntax to OO. (In case this is not clear, init and read_val are the functions exported by the module to the main program.) The code above is simplified to real bare bones (only one variable to be used and kept from one call to the next, etc.), but it works. Basically, change the variable passed to init to an array, a list or a hash, make the other changes needed in that case, and you are set up. I hope this is clear and this helps. I tried to make this post as short as possible and still giving the clues I wanted to give, please ask if you think that this might give you an interesting lead but that my explanbations were too terse or otherwise not clear. In reply to Re: Simple question about namespaces
by Laurent_R
|
|