Regular Expressions   «Prev 

Perl x Modifier

The x modifier uses pretty-printing extensions.
It is designed to allow you to format and comment your regular expressions. This is covered in more detail later in this module.
Regular expression modifiers are usually written in documentation as for example, "the /x modifier", even though the delimiter in question might not really be a slash.
x allows whitespace and comments whithin the regex

The idea is that you want to output dates like 08/04/2012 for April 4th, 2012. Write a like() test for that, adding it to testit.t as usual.

# we use the /x modifier on the regex to allow whitespace to be# ignored. 

This makes the regex easier to read like today(),
qr{^ \d\d/\d\d/\d\d\d\d $}x,
today() should return a DD/MM/YYYY format;

Run your testit.t script a few times. Because you are using a random value, sometimes it might pass, but other times it will fail with something like this:
not ok 4 - today() should return a DD/MM/YYYY format
1..4
# Failed test today() should return a DD/MM/YYYY format
# at t/testit.t line 11.
# 3/4/2012
# does not match (?x-ism:^ \d\d/\d\d/\d\d\d\d $)
# Looks like you failed 1 test of 4.

The regular expression attempts to match two digits, followed by a slash, two more digits, a slash, and four digits. The ^ and $ anchors force the regex to match from the start to the end of the string.

/x modifier

The /x modifier enables you to introduce white space and comments into an expression for clarity. For example, the following match expression looks suspiciously like line noise:
$matched = /(\S+)\s+(\S+)\s+(\S+)\s+\[(.*)\]\s+"(.*)"\s+(\S+)\s+(\S+)/;

Adding the /x modifier and giving some description to the individual components allows us to be more descriptive about what we are doing:
matched = /(\S+) #Host
\s+ #(space separator)
(\S+) #Identifier
\s+ #(space separator)
(\S+) #Username
\s+ #(space separator)
\[(.*)\] #Time
\s+ #(space separator)
"(.*)" #Request
\s+ #(space separator)
(\S+) #Result
\s+ #(space separator)
(\S+) #Bytes sent
/x;

Although it takes up more editor and page space, it is much clearer what you are trying to achieve.
There are other operator-specific modifiers, which we’ll look at separately as we examine each operator in more detail.