in reply to Refactoring challenge.
A small point: that s/// is pretty inefficient, and replacing it with:
makes it clear that you could do it implicitly in the previous line just by adding an extra argument:substr($str, 0, $pos1) = '';
print $indent x $depth, substr( $str, 0, $pos1, '' );
dragonchild's refactored version would not need the $pos = $newpos + 2 correction if the (suitably renamed) $_print() did the same thing.
I'd be tempted to make it read a bit more cleanly by also passing in the depth to $_print() and include a slight reordering to flatten:
my $_consume = sub { print $indent x $_[2], substr $_[0], 0, $_[1], ''; } sub pp { return unless @_; my($str) = @_; my($length, $first) = f1($str); if (!defined $first) { $_consume->($str, $length, $depth); } elsif ($first eq CONST1) {
$_consume->($str, $length + 1, --$depth); } elsif (defined(my $newlen = f2($str, $first))) { $_consume->($str, $newlen + 2, $depth); } else { $_consume->($str, $length, $depth++); } return $str; }
Update: passing $str to be modified in this way is probably not safe - if I remember right, any attached magic will cause a temporary copy to be passed instead - so some slight modifications to pass \$str instead are probably required.
Update 2: added a missing + 1
Hugo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Refactoring challenge.
by BrowserUk (Patriarch) on Mar 06, 2005 at 13:17 UTC | |
by hv (Prior) on Mar 06, 2005 at 13:28 UTC | |
by BrowserUk (Patriarch) on Mar 06, 2005 at 14:08 UTC | |
|
Re^2: Refactoring challenge.
by dragonchild (Archbishop) on Mar 07, 2005 at 13:26 UTC |