>>>>> "SB" == Steve Bertrand <st...@ibctech.ca> writes:

  SB> Hi all,
  SB> I often come across situations where I need to create method names based
  SB> on an iterator of some sort, and I'm looking for feedback on whether
  SB> (ie. how) I can simplify the code, make it easier to read and make it
  SB> more scalable.

scalable in what way? it is a simple 2 level loop.
  SB> The relevant snip of the config file:

  SB>     if ( $self->ENABLE_REPLICATION() && $self->SLAVE_SERVERS() ){

  SB>         for my $slave_number ( 1 .. $self->SLAVE_SERVERS() ){

  SB>             my $slave_info = "SLAVE_${ slave_number }_";

  SB>             my $slave; # aref

  SB>             for my $item ( qw/ SOURCE USER PASS / ){

  SB>                 my $function = $slave_info . $item;

  SB>                 push @$slave, $self->$function();
  SB>             }

  SB>             push @$database_servers, $slave;
  SB>         }
  SB>     }

first off, i would invert the outer if and exit the sub/block and fall
through to this code. it saves a block entry, an indent and those
expensive pixels for the {}.

this can be reduced to a pair of nested maps if you really want to. here
is untested code for that:

$database_servers = [
        map { my $slave_info = "SLAVE_${_}_" ;
                [ map { my $meth = "$slave_info$_"; $self->$meth()
                } qw/ SOURCE USER PASS / ]
        } 1 .. $self->SLAVE_SERVERS() ] ;

much shorter but who knows if it is more readable. different formatting
could help.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to