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

perldelta: better /r example?

Thread Next
Tom Christiansen
April 25, 2011 13:31
perldelta: better /r example?
Message ID:

    =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;


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About