Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
The stupid question is the question not asked
 
PerlMonks  

scalar in list context

by manishrathi (Beadle)
on Jan 26, 2010 at 20:47 UTC ( #819771=perlquestion: print w/ replies, xml ) Need Help??
manishrathi has asked for the wisdom of the Perl Monks concerning the following question:

when do we use scalar in list context ?
($len) = "length" ; #scalar in list context $len1 = "lengthy" ; print "1 - ", ($len), "\n"; print "2 - ", $len , "\n"; print "3 - ", $len1, "\n" ; print "4 - ", ($len1), "\n";
I get the same result all the times. So what diference does it make when I use scalar in list context ? Whats the point in doing that ?

Comment on scalar in list context
Download Code
Re: scalar in list context
by chromatic (Archbishop) on Jan 26, 2010 at 21:10 UTC

    There's no syntactic difference between these lines of code. The parentheses are superfluous:

    print "1 - ", ($len), "\n"; print "2 - ", $len , "\n"; print "3 - ", $len1, "\n" ; print "4 - ", ($len1), "\n";

    I don't understand your question.

Re: scalar in list context
by ikegami (Pope) on Jan 26, 2010 at 21:18 UTC

    A scalar returns the same in both scalar and list context.

    One uses scalars in list context all the time. For example, %h = ( a => 1, b => 2 ); has four.

    Update: Fixed grammar. Added second paragraph.

Re: scalar in list context
by JavaFan (Canon) on Jan 26, 2010 at 21:36 UTC
    Whats the point in doing that?
    The point is either the list, or the context. The scalar just happens to be part of the expression making the list. Just as in
    $foo + 3;
    there are two scalars in the arithmetic expression.
Re: scalar in list context
by Crackers2 (Vicar) on Jan 26, 2010 at 21:38 UTC

    Maybe you mean the context of the assignment?

    @array = ( "one", "two", "three" ); ($len) = @array; $len1 = @array; print "1 - ", $len, "\n"; print "2 - ", $len1, "\n";

    Where the first will print "one" and the second will print "3", since (IIRC) assignment forces the context of the LHS onto the RHS.

    Update: See ikegami's post below for the correct explanation of what I was trying to say.

      Maybe you mean the context of the assignment?

      All three of your assignments are evaluated in void context, so I think you mean "the context imposed by the assignment" onto its operands.

      assignment forces the context of the LHS onto the RHS.

      So what determines the context of the LHS?

      The context in which both operands of an assignment are evaluated is determined by the type of assignment (scalar assignment or list assignment). Both types of assignments are represented by "=" in the code, so the type of assignment is determined by the literal code on the LHS.

      Expressions in parens, arrays, hashes, local() and my() cause the creation of a list assignment. Everything else causes the creation of a scalar assignment.

Re: scalar in list context
by LanX (Abbot) on Jan 26, 2010 at 21:56 UTC
    You seem to believe that parens force list context. That's wrong, parens do only group and influence precedence (with the exception of the empty list).

    A scalar is technically just a one element list.

    There is no functional▓ difference between @list= (1) and @list=1 or between @list= (1,2,3,4);╣ and @list=(1,(2,3,(4)));

    It's the comma operator which builds the list value (in list context).

    Cheers Rolf

    UPDATE: ╣)Parens corrected, THX chromatic! :)

    Added second example.

    ▓) Ikegami pointed out in a /msg that different grouping force different optrees, but of course the result is here the same...

      There is no difference between @list= 1,2,3,4; and @list=(1,(2,3,(4)));

      Careful! Precedence makes a big difference between those two. You're thinking of @array = (1, 2, 3, 4) versus @array = (1, (2, 3, (4))).

        Wow thanks a lot.

        Indeed, the = has a higher precedence than comma ...

        UPDATE: what I really wanted to show is that  sub list1 { 1,2,3,4 } and  sub list2 { (1,(2,3,(4))) } return the same result. The parens are not necessary to produce a list but are saver and often better to read...

        Cheers Rolf

Re: scalar in list context
by biohisham (Priest) on Jan 27, 2010 at 23:09 UTC
    This is just an explanation code, Crackers2 and ikegami explained it all well, however, notice the context of the assignment in both cases when the scalars are surrounded with () and when they're not ...
    #title "scalar in list context" my @array = qw(zero one two); my ($listItemOne, $listItemTwo, $listItemThree); #CASE ONE ##assign array to different scalars, notice the ()## ($listItemOne, $listItemTwo, $listItemThree)=@array; #CASE TWO ##This assigns the length of @array to $var3 ##generates alot of complaints if warnings on... $var1, $var2, $var3=@array; print $var1,"\n"; print $var2,"\n"; print $var3,"\n"; print $listItemOne,"\n"; print $listItemTwo,"\n"; print $listItemThree,"\n";



    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (15)
As of 2014-04-16 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (434 votes), past polls