Tonight’s folly

I just realised that since the addition of /r, you can now write s!!!regex. Or s!!!regexp if you prefer.

6 Comments

Finally a use for the /r modifier.

Well, this is quite pretty:

my @new = map { s/foo/bar/r } @old;

Compared with the old way:

my @new = map { (my $tmp = $_) =~ s/foo/bar/; $tmp } @old;

map { s/foo/bar/ } (my @new = @old); or my @new; map { s/foo/bar/ } (@new = @old); works fine too

The problem with /r is that it should really be r// not s//r, Things that are different should look different.

my @new = map { s/foo/bar/; $_ } @{[@old]}; is also an option, though not a terribly pretty one.

and for those who dislike map in void context: s/foo/bar/ for (my @new = @old);

Leave a comment

About Aristotle

user-pic Waxing philosophical