Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Curious to know the internals of the following code

by jesuashok (Curate)
on Nov 08, 2005 at 07:18 UTC ( #506632=perlquestion: print w/replies, xml ) Need Help??

jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Curious to know the internals of the following code
by BUU (Prior) on Nov 08, 2005 at 07:20 UTC
    The first time you assign to @list you use 'q' which is a pseudonym (or perhaps real name) for the '' operator. The second time you assign you use qw// which splits on spaces.
Re: Curious to know the internals of the following code
by jbrugger (Parson) on Nov 08, 2005 at 07:32 UTC
    A little bit of reading would have helped...
    It's all there: perlop
    # q/STRING/ # 'STRING' A single-quoted, literal string. A backslash represents a backslash un +less followed by the delimiter or another backslash, in which case th +e delimiter or backslash is interpolated. $foo = q!I said, "You said, 'She said it.'"!; $bar = q('This is it.'); $baz = '\n'; # a two-character string qw/STRING/ Evaluates to a list of the words extracted out of STRING, using embedd +ed whitespace as the word delimiters. It can be understood as being r +oughly equivalent to: split(' ', q/STRING/); the differences being that it generates a real list at compile time, a +nd in scalar context it returns the last element in the list. So this + expression: qw(foo bar baz) is semantically equivalent to the list: 'foo', 'bar', 'baz'
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: Curious to know the internals of the following code
by blazar (Canon) on Nov 08, 2005 at 10:57 UTC
    Because despite the name @list you're in fact assigning it a single element which is precisely the string "'Ashok Hello'", that is: without the external double quotes, but with the internal single ones. That is precisely the difference between q() and qw(): the former returns non interpolated strings, and when used with "'" (single quotes) as a delimiter "q" become redundant, while the latter returns a list of (non interpolated) "words"1, splitting on whitespace.

    1 Notice the double quotes - I mean that they're not required to match /^\w+$/.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://506632]
Approved by jbrugger
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2023-05-28 10:45 GMT
Find Nodes?
    Voting Booth?

    No recent polls found