Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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:

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"

Comment on Curious to know the internals of the following code
Select or Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2014-09-23 19:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (239 votes), past polls