If you're going to base something on chomp, you should use the same interface. In other words, you should work in-place.

sub chimpx { my $pat = @_ ? shift : $/; for @_ ? @_ : $_; }

If you want do a non-destructive chimp, use

chimpx $pat, my $dst = $src;

Update: ok, since I didn't preserve chomp's return value, let's do it your way:

sub chimpx { my $pat = @_ ? shift : $/; my $targs = sub { \@_ }->( @_ ? @_ : $_ ); @$targs = @$targs if defined(wantarray); s/^(?:\Q$pat\E)*// for @$targs; return wantarray ? @$targs : $targs->[0]; }

Tests:

use Test::More tests => 18; { local $_ = 'ababac'; my $x = 'ababac'; chimpx 'ab', $x; is($_, 'ababac', 'void pat var - $_ preservation'); is($x, 'ac', 'void pat var - result verification'); } { local $_ = 'ababac'; my $x = 'ababac'; my $y = chimpx 'ab', $x; is($_, 'ababac', 'scalar pat var - $_ preservation'); is($x, 'ababac', 'scalar pat var - arg preservation'); is($y, 'ac', 'scalar pat var - result verification'); } { local $_ = 'ababac'; chimpx 'ab'; is($_, 'ac', 'void pat novar - result verification'); } { local $_ = 'ababac'; my $y = chimpx 'ab'; is($_, 'ababac', 'scalar pat novar - $_ preservation'); is($y, 'ac', 'scalar pat novar - result verification'); } { local $_ = "$/$/c"; chimpx; is($_, 'c', 'void nopat novar - result verification'); } { local $_ = "$/$/c"; my $y = chimpx; is($_, "$/$/c", 'scalar nopat novar - $_ preservation'); is($y, 'c', 'scalar nopat novar - result verification'); } { local $/ = 'ab'; local $_ = "$/$/ac"; chimpx; is($_, 'ac', 'alt $/ - result verification'); } { my $x1 = 'ababac'; my $x2 = 'ababad'; chimpx('ab', $x1, $x2); is($x1, 'ac', 'void multiple args - 1st result verification'); is($x2, 'ad', 'void multiple args - 2nd result verification'); } { my $x1 = 'ababac'; my $x2 = 'ababad'; my ($y1, $y2) = chimpx('ab', $x1, $x2); is($x1, 'ababac', 'scalar multi args - 1st arg preservation'); is($x2, 'ababad', 'scalar multi args - 2nd arg preservation'); is($y1, 'ac', 'scalar multi args - 1st result verification'); is($y2, 'ad', 'scalar multi args - 2nd result verification'); }

In reply to Re: How can I avoid code repetition here by ikegami
in thread How can I avoid code repetition here by rovf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.