in reply to Re: 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.

So the basic tack is right? (like I wrote, the rewrite is more pseudo-perl than anything else).
  • Comment on Re^2: Advice on transforming a sub into a re-entrant recursive-able method.

Replies are listed 'Best First'.
Re^3: Advice on transforming a sub into a re-entrant recursive-able method.
by almut (Canon) on Jul 21, 2008 at 18:07 UTC

    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).

      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.