
7stud
Enthusiast
Sep 19, 2010, 7:09 PM
Post #5 of 5
(378 views)
|
|
Re: [sangfroid] "scalar " confused
[In reply to]
|
Can't Post
|
|
'scalar context' means that one value is expected in your code, and 'list context' means that multiple values are expected in your code. perl allows you to define a function so that it returns different values depending on whether the function call is made from a 'scalar context' or a 'list context'. In certain places in your code, perl expects a single value, and in other places in your code perl expects multiple values to be present. Here are some examples: 1) What would you expect x to be in that code? A single value or a list of values? In the context of that code, you would expect x to be a single value, i.e. a scalar value. That's because the addition operator requires a single value on the left hand side and a single value on the right hand side. So if you replaced x with a function call, then the function call would be in scalar context. 2) What would you expect x to be in that code? A single value or a list of values? In that particular context, you would expect x to be a list of values. So if you replaced x with a function call, it would be in list context. The replacement function could be the exact same function in both examples, but in the first example the function call would be made from a 'scalar context' and in the second example the function call would be made from a 'list context'. As a result, the following will lead to different output:
use strict; use warnings; use 5.010; my @array = (2, 4, 6, 8); my @x = @array; my $y = @array; say @x; say $y; You get different results because the assignment operator is defined to return different things depending on whether it is in scalar context v. list context. In the first assignment statement, the left hand side is an array variable(@), so the right hand side of the assignment statement must produce a list, i.e. it is a 'list context'. In the second assignment statement, the left hand side is a scalar variable($), so the right hand side must produce a scalar or single value, i.e. it is a scalar context. In perl, print expects a list, so a print statement is a list context, and any function call in a print statement is expected to produce a list. In perl, you can't necessarily know what a function returns because it depends on where in your code the function is called, i.e. the return value might depend on the 'context' in which the function call is made. The 'scalar' operator tells perl not to look at the context of the code, and instead just produce a scalar value. How does a scalar value subsequently work with print when print expects a list? The scalar value is automatically promoted to a list with one element. Tricky, perl, tricky. Here is an example of how you can define a subroutine to return different things depending on the context it is called in:
use strict; use warnings; use 5.010; sub do_it { if (wantarray) { return (1, 2, 3); } else { return 'hello'; } } for my $number (do_it()) { say $number; } my $answer = do_it(); say $answer; --output:-- 1 2 3 hello And using the scalar operator:
use strict; use warnings; use 5.010; sub do_it { if (wantarray) { return (1, 2, 3); } else { return 'hello'; } } for my $number (scalar do_it()) { say $number; } --output:-- hello
(This post was edited by 7stud on Sep 19, 2010, 9:16 PM)
|