develooper Front page | perl.perl5.porters | Postings from April 2011

perldelta: better /r example?

Thread Next
From:
Tom Christiansen
Date:
April 25, 2011 13:31
Subject:
perldelta: better /r example?
Message ID:
8161.1303763504@chthon
Consider:

    =head3 Non-destructive substitution

    The substitution (C<s///>) and transliteration
    (C<y///>) operators now support an C</r> option that
    copies the input variable, carries out the substitution on
    the copy, and returns the result.  The original remains unmodified.

      my $old = "cat";
      my $new = $old =~ s/cat/dog/r;
      # $old is "cat" and $new is "dog"

But consider that this has been perfectly normal with different grouping:

    NEW: $new = old =~ s/cat/dog/r;
    OLD: ($new = $old) =~ s/cat/dog/;

And so has this, actually:

    NEW: @new = map { s/cat/dog/r } @old;
    OLD: for (@new = @old) { s/cat/dog/ } 

It would seem better to give an example of something that wasn't so
trivially done before.  For example, this prints "Enlisted" from "unlost":

    print ucfirst "unlost" =~ y/aeiouy/yuoiea/r =~ s/$/ed/r;

--tom

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About