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.


In reply to "array" search by Sheol

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.