in reply to Advice on transforming a sub into a re-entrant recursive-able method.

I think you're failing to see it because you're unclear on what you wish to accomplish. That also makes it hard to give you a good answer. So I'll settle for giving good advice, and some of it may pertain.

My first piece of advice is to stop using prototypes. Prototypes in Perl do not do what you think, and what they do in most cases is something you really don't want. The (unfortunately now MIA, so go to the wayback machine) article FMTYEWTK about Prototypes in Perl explains this in great detail.

Of course you've avoided most of the pain by using method calls, which causes the prototype to be ignored. Thereby they serve no purpose other than to confuse the maintenance programmer.

Another general tip is that it is generally good programming style to declare lexical variables as close to their scope as possible. So move your declarations in.

Error handling. Rather than return errors, I prefer throwing an exception with die. That way if someone forgets to have error handling you still know that things went wrong. (Opinions differ on this.)

A more relevant tip is that you shouldn't rewrite code without a reason. You have code. It works. A rewrite takes effort and may not work. So why do it?

Now let's look at your rewrite. You say you're trying to follow the advice in Higher Order Perl. I'd take that to mean that you're interested in doing things like having lazy lists. If you want to do that, then you need to start your rewrite from the inside out. So you want to start your rewrite with get_entries. So you'd want to have a function named something like get_entries_iterator that returns a function that you can call to get each entry in turn. And then you'd call it repeatedly to get each entry.

My second response to Re: Let's get lazy should give you a sense of what this could look like.

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

Replies are listed 'Best First'.
Re^2: Advice on transforming a sub into a re-entrant recursive-able method.
by hechz (Novice) on Jul 21, 2008 at 21:42 UTC
    I have now established what I wish to accomplish! the input to the initial call of get SNMP will always be the same, and IP. What I want the sub to do is then call itself with different arguments and behave in a certain manner. That manner being to first get the list of interfaces, and say map them to a hash, then get the interface MAC addresses and place them as the values. My eventual goal is to have the type of device determined as well so that get_snmp will know to use different OIDs for different hardware... Different vendors put their values in different places