tags:

views:

186

answers:

3

My usual 'x' usage was :

print("#" x 78, "\n");

Which concatenates 78 times the string "#". But recently I came across this code:

while (<>) { print if m{^a}x }

Which prints every line of input starting with an 'a'. I understand the regexp matching part (m{^a}), but I really don't see what that 'x' is doing here.

Any explanation would be appreciated.

+18  A: 

It's a modifier for the regex. The x modifier tells perl to ignore whitespace and comments inside the regex.

In your example code it does not make a difference because there are no whitespace or comments in the regex.

sepp2k
Thank you for that clear explanation.
OMG_peanuts
@sepp2k: it may be worth an edit mentioning that you can get to this through `perldoc perlre`; it's even on the first page!
leander
+5  A: 

In this use case 'x' is a regex modifier which "Extends your pattern's legibility by permitting whitespace and comments." according to the perl documentation. However it seems redundant here

bjg
Not "redundant" but "unnecessary".
clintp
@clintp: and the difference between redundant (meaning 'unnecessary') and unnecessary is?
Jonathan Leffler
'redundant' doesn't simply mean 'unnecessary'. It means 'unnecessary because of something else that serves the same purpose'. In this case, there is nothing else telling the regex to ignore whitespace/comments. There just aren't any whitespace/comments, so it is unnecessary to ignore them.
pkaeding
To be even more pedantic it only means "there is something else that serves the same purpose". Something doesn't have to be unnecessary to be redundant (see redundant power supply or security systems for example).
sepp2k
But redundant workers are simply people who have been cast off by their company; there may not be anyone also doing their job at all if the company ceased a certain operation. Redundant has multiple meanings. I'm almost tempted to withdraw my cavilling at @clintp's comment (it is redundant too).
Jonathan Leffler
From the OED: **redundant** *adjective* no longer needed or useful; superfluous. *Engineering* (of a component) not strictly necessary to functioning but included in case of failure in another component. *chiefly Brit.* (of a person) no longer in employment because there is no more work available **unnecessary** *adjective* not needed
Mike
From the Apple MacOS X Dictionary (not as weighty a tome, or authority, as the OED, but interesting anyway): Redundant ... unnecessary, not required, inessential, unessential, needless, unneeded, uncalled for; surplus, superfluous. ANTONYMS essential, necessary.
Jonathan Leffler
`perlcritic --brutal` will complain about the lack of `xms`, so I've begun to see this as somewhat necessary to shut it up. It has helped me catch a few regex errors, too. (There's something reassuring about having a critic-clean program with very few or no `## no critic` notations.)
leander
+6  A: 

The "x" in your first case, is a repetition operator, which takes the string as the left argument and the number of times to repeat as the right argument. Perl6 can replicate lists using the "xx" repetition operator.

Your second example uses the regular expression m{^a}x. While you may use many different types of delimiters, neophytes may like to use the familiar notation, which uses a forward slash: m/^a/x

The "x" in a regex is called a modifier or a flag and is but one of many optional flags that may be used. It is used to ignore whitespace in the regex pattern, but it also allows the use of normal comments inside. Because regex patterns can get really long and confusing, using whitespace and comments are very helpful.

Your example is very short (all it says is if the first letter of the line starts with "a"), so you probably wouldn't need whitespace or comments, but you could if you wanted to.


Example:

m/^a     # first letter is an 'a'  
         # <-- you can put more regex on this line because whitespace is ignored
         # <-- and more here if you want
 /x
vol7ron
thanks vol7ron, I didn't know you could put comments in regex
Armando