Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

I don't understand a piece of this code

by Socrates440 (Acolyte)
on Jun 18, 2012 at 02:59 UTC ( #976725=perlquestion: print w/replies, xml ) Need Help??
Socrates440 has asked for the wisdom of the Perl Monks concerning the following question:

I am working my way through a beginning perl text book in my free time and I am currently reading the chapter on regular expressions. Anyways, I am confused by a piece of this code:
$what = "[box]"; foreach (qw(in[box] out[box] white[sox])) { if (/\Q$what\E/) { print "$_ matched!\n"; } }
I get that it is storing the string \box\ in the varriable $what. If I understand it right, the foreach statement parses each of the strings in the parenthesies looking for the string \box\. What is the "qw" in the line "(qw(in\box\ out\box\ white\sox\))" though? I have no idea what that is. Sorry about the words box and sox being highlighted in red. I don't know how to stop this site from interpolating (correct word?) the brackets.

Replies are listed 'Best First'.
Re: I don't understand a piece of this code
by bitingduck (Chaplain) on Jun 18, 2012 at 03:07 UTC

    The "qw" just stands for "quote words" for quoting a list of words. Which is pretty much what you inferred.

    I don't see anything in red though-- code tags generally do a good job keeping the site from doing strange things to code

Re: I don't understand a piece of this code
by Albannach (Prior) on Jun 18, 2012 at 03:40 UTC
    I rather think of qw as quote whitespace since what it does is break up the subject string using whitespace as separators. So the string inside the qw() is broken into 3 pieces, not 3 words as in this case each resulting piece contains 2 words and some square brackets. See perlop under Quote and Quote-like operators.

    The next think you need to know is that \Q and \E are allowing you to use the content of the variable $what as literal text without interpreting any special characters, which in this case are the square brackets. Have a look at perlre to see what [box] means inside a regular expression.

    So in summary it is looking for the string [box] including the square brackets, withing the 3 separate strings in[box], out[box], and white[sox], and prints matches.

    I'd like to be able to assign to an luser

      That's how I initially learned it as well, but j ust says "quote a list of words" where you have to sort of liberally accept that a word is letters separated by whitespace. "Quote with whitespace" makes more sense to me than "quote whitespace", but it mostly just matters that it works as advertised.

Re: I don't understand a piece of this code
by cheekuperl (Monk) on Jun 18, 2012 at 03:25 UTC
    The code is doing what you've understood:
    $what = "[box]"; foreach (qw(in[box] out[box] white[sox])) { #Assign to $_, one by one, + each value in parenthesis if (/\Q$what\E/) { print "$_ matched!\n";
    \Q and \E disable special meaning of all metachars in between them. See perlre
Re: I don't understand a piece of this code (What is qw()? How does qw() work?)
by Anonymous Monk on Jun 18, 2012 at 03:30 UTC
Re: I don't understand a piece of this code
by jamesbutler (Initiate) on Jun 18, 2012 at 04:07 UTC
    You may find this website helpful

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://976725]
Approved by Albannach
[Your Mother]: Stick with what you know. Or post a SOPW and if it's not complicated I guarantee someone will answer; me if it's straightforward and no one else gets it better first.
[1nickt]: ++YourMother (Grammar Vigilante of Bristol)
[Lady_Aleena]: Um, how does grep do multiline? print "$file:$_" if grep { /get_(array|hash| data)\(.+\)/ } $_; only returns the same 18 lines the command line grep does.
[Lady_Aleena]: my scratchpad has what I have so far.
[choroba]: what do you mean by multiline?
[Discipulus]: thanks 1nickt!
[choroba]: grep is a filter, it selects elements from a list based on a boolean condition
[moritz]: you need to read the file in one go to do a multiline regex match
[erix]: ha ha, I like the Vigilante
[Lady_Aleena]: Forget it. I'm getting angry with it.

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2017-05-23 19:49 GMT
Find Nodes?
    Voting Booth?