Just an expansion of Ovid's last comment: refactoring this code would (for example) turn this:
sub generate_extension
{
my $self = shift;
return( &generate_line_form_element( "text", "extension" , $self-
+>{extension}, $self->{extension} , "4" , "4" );
}
sub generate_email_address
{
my $self = shift;
return( &generate_line_form_element( "text", "email_address" , $s
+elf->{email_address},
$self->{email_address} , "30
+" , "50" );
}
... into this:
sub generate_stuff {
my $self = shift;
my $what = shift;
my @numbers = @_;
return( &generate_line_form_element( "text", $stuff , $self->{$st
+uff},
$self->{$stuff} , @numbers )
+;
}
... and calling it would look like this:
generate_stuff( 'extension', 4, 4 ); # this is what a
generate_stuff( 'email_address', 30, 50 ); # call would look like
If you see a lot of code that looks the same structurally, it's usually better to consolidate it into something smaller and less repetetive. That way, if you have an error (or just a change you want to make), you only have to fix it in one place, and you can be certain that all of your similar functions will work the same way. In a situation of significant redundancy, consolidation can save a lot of space.
--
Love justice; desire mercy.
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.