in reply to Making the later front slash "/" optional

A question mark is a special regex character that means match the previous character 0 or 1 time:

use strict; use warnings; use 5.010; my $location = 'data'; my @fnames = ('/data', '/data/', 'data/'); for my $fname (@fnames) { if ($fname =~ m{/ $location /?}xms ) { say $fname; } } --output:-- /data /data/

But you can use string interpolation in your regex to make your code even clearer:

use strict; use warnings; use 5.010; my $location = 'data'; my @fnames = ('/data', '/data/', 'data/'); my $pattern = "/ $location /?"; for my $fname (@fnames) { if ($fname =~ /$pattern/xms ) { say $fname; } } --output:-- /data /data/
You can use the /o flag to signal to perl that the variable won't change and to compile the regex only 'o'nce. That keeps perl from recompiling the regex evertime you try a match.