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

Hi moks,

#!/usr/bin/perl use strict; + + my @list = q('Ashok Hello'); + print "val: $_\n" foreach @list; @list = qw('Ashok Hello'); print "val: $_\n" foreach @list;

Output is as follows :- val: 'Ashok Hello' val: 'Ashok val: Hello'
why the Space is not Interpolted in the first foreach loop. can you please explain me how the internal goes here.

"Keep pouring your ideas"

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+$/.