this post was submitted on 20 Jun 2023
4 points (100.0% liked)

Perl

184 readers
1 users here now

founded 1 year ago
MODERATORS
4
Chaining Substitutions (lemmy.sdf.org)
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/perl
 

I was recently reading through Perl Regular Expression Tutorial (perlretut) when I found the section on using the 'r' option. I'd seen this before but rarely had any reason to use it. Then it occurred to me that you could chain these together.

I've been programming Perl for 30+ years and don't think I've ever seen this before.

Here's an example that shows how I would usually do this, i.e. a series of var =~ s/// lines, and how to do the same thing in one line while initializing a variable.

#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';

my $T = "one two trash THREE random";
my $T2 = $T;
$T2 =~ s/trash|random//g;
$T2 =~ s/(THREE)/lc($1)/e;
$T2 =~ s/\s+/ /g;
$T2 =~ s/^/Count with me: /;

my $T3 = $T =~ s/trash|random//gr =~ s/(THREE)/lc($1)/er =~ s/\s+/ /gr =~ s/^/Count with me: /r;

say "T  := $T\nT2 := $T2\nT3 := $T3";

Output:

$ ./perl-subst-test.pl 
T  := one two trash THREE random
T2 := Count with me: one two three 
T3 := Count with me: one two three 

Anyways, I had to tell someone ...

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 1 year ago* (last edited 1 year ago)

Where do I find this slack and discord? asking for a friend...

edit: I found "Perl Hideout" ... is that one?