Sheol has asked for the wisdom of the Perl Monks concerning the following question:
Well, I've got a few objects in perl, that have keys => [] and value => [], or similar attributes in them. I am finding that I am constantly have to search through them the exact same way each time.
That code would be:
for(my $i = 0; $i < count(@{$self->{keys}}); $i++) { my $id = $self->{keys}->[$i]; if($key =~ m!^$id$!) { # do something with the knowledge that we got the correct key # then exit out of function using this loop } } # do something with the knowledge there is no key with that name
Now, 1) There is generally going to a lot of places this will appear, and it is ending up being used more often than some of my functions||methods... that normally shows something bad with coding. 2) There is a chance I may want to change what is done at those sections of code. Mostly because I am pretty damn sure that is not efficent, and I'll probably tweak it later.
So, I am thinking about putting that piece of code somewhere else, and doing something similar to an inline function or macro.
1) Should I have a module that contains a set of functions that should appear in all modules (possibly using @ISA)
2) Should I have an anonymous routine that is loaded as a reference in the modules. Or rather defining the "search" function elsewhere, and loading it as part of the object's propperties
3) Having the functions that make use of it require to have a function for doing the search loaded with @_.
I also plan to have a section of code in this project, that acts as a tied array, but I have yet to decide on that.
Thank you for your help.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: "array" search
by Aristotle (Chancellor) on Jan 13, 2006 at 06:45 UTC | |
by Sheol (Novice) on Jan 14, 2006 at 08:28 UTC | |
|
Re: "array" search
by ikegami (Patriarch) on Jan 13, 2006 at 07:11 UTC | |
by Sheol (Novice) on Jan 14, 2006 at 08:21 UTC | |
by ikegami (Patriarch) on Jan 14, 2006 at 09:16 UTC | |
|
Re: "array" search
by lidden (Curate) on Jan 13, 2006 at 06:47 UTC | |
by Sheol (Novice) on Jan 14, 2006 at 08:47 UTC |