in reply to Re^2: Advice on transforming a sub into a re-entrant recursive-able method.
in thread Advice on transforming a sub into a re-entrant recursive-able method.

I haven't really looked at the semantic aspects of your rewrite yet, because I'm not exactly sure which advice from Higher Order Perl you're trying to follow, in particular which coderefs you want to pass to where...

As it is, it's not clear to me how the three functions are related. Is snmp_get the toplevel function? If so, how is it supposed to call the other routines (via coderefs you pass in as args?), etc.  Could you elaborate a bit on how you want the new version to operate, i.e. what you want to call with which args, and what it should return (a few lines of sample code showing how you're planning to use it would help).

  • Comment on Re^3: Advice on transforming a sub into a re-entrant recursive-able method.
  • Download Code

Replies are listed 'Best First'.
Re^4: Advice on transforming a sub into a re-entrant recursive-able method.
by hechz (Novice) on Jul 21, 2008 at 18:33 UTC
    The advice I am attempting to follow is from page 48 in HOP... "There is an important lesson to learn here: make functions re-entrant by default, because sometimes the usefulness of being able to call a function recursively will be a surprise"...

    The behavior I am looking for is to pass in the name of the thing I am looking for, and to generate a hash of the MACs keyed off of the interface names. I am assuming that I need to pass in sub-routines that gather the OIDs from the lookup table, and perhaps call a generic sub that performs the $snmp_sess->get_request.

      because sometimes the usefulness of being able to call a function recursively will be a surprise

      It doesn't make any sense to try to follow advice if you don't know what it means. Your original function does one fairly simple thing. I think it's highly unlikely that it will ever need to be called recursively. If it turns out that that it will, the function is already in pretty good shape, because it uses no external variables (that is, it accesses no global variables and no external lexical variables). However, it does depend on external state — specifically, the state stored in the snmp session object; and also whatever is read from the snmp space via the snmp session object. These factors suggest that making this function re-entrant may be complicated, and it's quite likely that your little function isn't the right place to solve the problem. On the other hand, the data from snmp space may be relatively static, and the snmp session may be a well-behaved black box (storing no state that would impact your function). If this is the case, then your function is already as recursion-ready as it needs to be. If you didn't figure out all this yourself, then probably you should simply skip that nugget of wisdom from HOP, for now.

      See also: voodoo programming and You aren't gonna need it.

      Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.
        I agree. The point of altering this routine is to force my self to understand the advice.
        I have been able to follow along with the various and sundry examples of OO-ification, and can use and alter other programmers OO code. In this case I want to make my brain bend enough to begin synthesizing it. My original method of achieving that goal was going to be to just write variants of the code from the book, and to try to use them.

        Then I thought about adding some more queries to my SNMP scan parsing script, and thought to myself "Hmmm, why not try to refactor my current routines to be more generic."
        The statements asserting that I am not quite sure what I am trying to do are fair. In response I can only offer, that I am trying to do something out of my comfort zone to improve my skills. The reason that I am asking for help is that I have failed to achieve that "Aah-ha!" moment, and the underlying mindset of reducing functionality to a base-case has not come.

        So to that end, I shall rephrase. If you, the great and esteemed monks :-), were to write a block of code that could be handed string, and return a hash keyed with interface names with values of MAC addresses, such that the behavior of the lookups of the information and the OIDs changed based on the kind of device the script found, what would be the your first step in dividing this problem into discreet elements of function.

        Below you'll find the entirety of the script, which; I hope, will provide some context to the Monks. moved code to the top-level message