I read somewhere that it's sensible to define search methods as class methods - put them inside the class definition of the objects they return. eg if I have a class called 'File', then as well has having 'File->new' return an object , I can have 'File->search' return an array of objects. This is supposed to be better than creating an object especially for searching.
(I can't remember exactly why this is meant to be good, nor where I read it - maybe there are exceptions?)
If I do do it this way, how do I pass the necessary information to another class about where it should search? I could just pass the class name, like so:
my $ui = UI->new;
$ui->query_dialog( { search_with => 'DB::File' } );
package UI;
sub query_dialog {
my( $self, $args ) =@_;
print "Name? ";
my $name = <STDIN>;
my $search_class = $args->{search_with};
my @files = $search_class->search( { name => $name } );
...
}
but this would require my 'UI' package to 'use' or 'require' the class, which seems messy.
How, if I want to keep my search object and my UI object totally ignorant of each others class, should I pass the search object?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.