in reply to How can I avoid code repetition here

Whilst taking note of moritzs reply, I would implement your requirement as (untested):
my $str = $change_in_place && $default_argument ? $_ : $_[0]; $str =~ s/^($lchompstr)*//; $_[0]; }
Or possibly
(my $str = $change_in_place && $default_argument ? $_ : $_[0]) =~ s/ +^($lchompstr)*//; $_[0]; }
i.e. substituting either of the above for
. . . if($change_in_place && $default_argument) { s/^($lchompstr)*// } else { $_[0] =~ s/^($lchompstr)*//; } $_[0]; }
A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re^2: How can I avoid code repetition here
by rovf (Priest) on Oct 07, 2009 at 13:19 UTC
    my $str = $change_in_place && $default_argument ? $_ : $_[0]; $str =~ s/^($lchompstr)*//; $_[0]; }
    I think this would (in the case $change_in_place && $default_argument) change only a copy of $_, not $_ itself. Maybe one could somehow use a foreach loop iterating over a one-element list, but I don't see how this would work in the general case.

    -- 
    Ronald Fischer <ynnor@mm.st>
      my $str = $change_in_place && $default_argument ? $_ : $_[0]; $str =~ s/^($lchompstr)*//;
      I think this would (in the case $change_in_place && $default_argument) change only a copy of $_, not $_ itself. Maybe one could somehow use a foreach loop iterating over a one-element list, but I don't see how this would work in the general case.
      I think that you could just take advantage of the fact that “ternary conditionals preserve lvalue-ness”, and write:
      ($change_in_place && $default_argument ? $_ : $_[0]) =~ s/^(?:$lchomps +tr)*//;
      (I've taken the liberty of making your parentheses non-capturing. :-) ) I'm not sure why you end with the $_[0] statement, though—it seems that you're returning the first argument even if you ignored it (because of $change_in_place and $default_argument).