Beefy Boxes and Bandwidth Generously Provided by pair Networks
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.

my $get_val_ref; my $set_val_ref = sub { my $val = shift; return sub { return $val ; }; }; sub init { $get_val_ref = $set_val_ref->(shift); } sub read_val { return $get_val_ref->(); }
(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
in thread Simple question about namespaces by woland99

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 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?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-03-28 19:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found