Re: Scalar Vs. List context
by chromatic (Archbishop) on Jul 11, 2009 at 16:29 UTC
|
my $calar = ...; # scalar context
my @rray = ...; # list context
my ($calar) = ...; # list context
my @rray = scalar ...; # scalar context
Some operators, such as concatenation and addition, enforce scalar context. Some circumstances, such as an argument list or list interpolation, enforce list context.
Certain circumstances, such as the return value of a subroutine, depend on the calling context -- you can't analyze them at compilation time.
Is there a circumstance you find especially confusing? | [reply] [d/l] |
|
|
What has been particularly confusing is that in the literature I have gone through-so far-there is nothing that clearly addresses this distinction and the "usage outcomes" of these contexts with supporting examples in a central manner like a chapter or topic , I thought it was not serious in the start until I have landed at the regexes and that is when I am fully aware I need to understand this before missing the chance.. and for the benefit of everyone who is caught in a similar fix I thought I am obliged to talk this out with the Monks and seek their wisdom therefore, consider this
$text="Tommy tom ticked a tick at the ticket counter";
$t_count=($text=~ y/[tT]/t/);
print "This text has $t_count Ts\n";
#result
#This text has 9 Ts
and this
$_ = "1.0 and 2.4 and 310 and 4.7 and so on";
@a = m/([\d|\.]+)\D+/g;
print "@a\n";
#result
#1.0 2.4 310 4.7
so we used the =~ in the first example and the only = in the second example (matching is used here with list context, but I would know it only after having run the code not while reading the code), this might be very plain to see for the expert but not for me. Hence I want to know the parameters for using scalar and list contexts, the differences between these contexts and the common pitfalls the novice would come across with regard to this.
So what I seek is a short sharp to the point explanation of what list context usage WOULD return and what scalar context usage WOULD return for some of the operations and techniques in Perl...
Excellence is an Endeavor of Persistence.
Chance Favors a Prepared Mind
| [reply] [d/l] [select] |
|
|
I think what you're confused with is matching against the special variable $_, not list context vs scalar context. Note that this:
$_ = "1.0 and 2.4 and 310 and 4.7 and so on";
@a = m/([\d|\.]+)\D+/g;
print "@a\n";
#result
#1.0 2.4 310 4.7
can be rewritten as this
$_ = "1.0 and 2.4 and 310 and 4.7 and so on";
@a = ($_ =~ m/([\d|\.]+)\D+/g);
print "@a\n";
#result
#1.0 2.4 310 4.7
without changing the meaning whatsoever. Your two examples only differ in that the first matches against a defined scalar, whereas the second matches against $_. | [reply] [d/l] [select] |
Re: Scalar Vs. List context
by jdporter (Paladin) on Jul 11, 2009 at 17:49 UTC
|
| [reply] |
|
|
the context tutorial has been very very informative for me indeed, the rule of thumbs also were amazing, and also I thought I would link this other page in here, it has quite a story which testifies to the end that somebody can end up in misery if they do not comprehend the contexts well enough.... http://www.stonehenge.com/merlyn/UnixReview/col38.html this has shown me about Larry Wall and I got to know him better than reading his name around and not knowing who he is/has been.
Thanks every one for having given me a day of rightful resources to study, I was like blindly punching in the dark when I tried to understand contexts for I was digging in a junkyard before you guys got my back :)
| [reply] [d/l] |
Re: Scalar Vs. List context
by ELISHEVA (Prior) on Jul 11, 2009 at 18:51 UTC
|
Here are some rules of thumb:
- assignments: if the lvalue (the thing assigned to) is an array (e.g. @aSomeArray = ...) or surrounded in parenthesis (e.g. ($var1, $var2) = ... then the thing on the right of the equal sign is in list context.
- array and list elements: these are implicitly in list context. Thus if @x=(1,2); @y=(3,4) then @z=(@x, @y) is equivalent to @z=(1,2,3,4) and $z=[@x,@y] is equivalent to $z=[1,2,3,4]
- flow of control constructs: depends on the construct. These need to be learned construct by construct, like grammar in a natural language. The most common example is for and foreach - whatever goes between the parenthesis is in list context, e.g. foreach (list-context-here)
- built-in functions or subroutines - the syntax description in Perl documentation will have the word list for any parameter that is in list context. The most commonly used functions expecting list context include
push,pop, sort, grep, map, and join. If the built-in function or subroutine has a prototype defined, parameters in list context will also be represented using @. You can get the prototype of any subroutine using the prototype function. For more about prototypes, see perlsub.
- user defined subroutines - check the documentation or prototype as above.
Best, beth
Update struck out push and pop. Pop needs an actual array, not just a list e.g. pop @x or pop @$y as noted by jwkrahn below. The same applies to the first parameter of push as well. This is because both modify the contents of an address in memory rather than simply process a set of elements in sequence. | [reply] [d/l] [select] |
|
|
The most commonly used functions expecting list context include push, pop,
pop does not impose list context. pop only accepts one argument, an array, and that is in scalar context.
Update: on the other hand, push does impose a list context on everything after the initial array argument.
| [reply] |
|
|
pop only accepts one argument, an array, and that is in scalar context.
Depending on how you look at it, it's either evaluated in list context (technical reality) or neither since it's not evaluated as an array would in list and scalar context (practical reality).
In no way is it evaluated in scalar context like you claim.
| [reply] |
|
|
|
|
|
|
|
|
|
|
|
|
array and list elements: these are implicitly in list context.
Not true. Every expression can be evaluated in any context. The context in which the expression is evaluated is determined by the operator to which the expression is an operand.
Expressions that are list elements are no exception. The list operator decides the context of its operands based on its context:
@a = (1,2,3,4); # list, list, list, list
$a = (1,2,3,4); # void, void, void, scalar
Expressions that are arrays are no exception.
@b = @a; # @a is evaluated in list context.
$c = @a; # @a is evaluated in scalar context.
@a; 1; # @a is evaluated in void context.
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
use strict;
use warnings;
sub foo {
print "wantarray? ", wantarray?1:0, "\n";
return wantarray? (1,2,3) : 's-s-sc-scalar';
}
my @x =('a','b', foo(), 'c');
print "\@x=(@x)\n";
prints
wantarray? 1
@x=(a b 1 2 3 c)
Looks like list context to me.
Best,beth | [reply] [d/l] [select] |
|
|