CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [sangfroid] "scalar " confused: Edit Log



7stud
Enthusiast

Sep 19, 2010, 7:09 PM


Views: 645
Re: [sangfroid] "scalar " confused

'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)

Code
my $total = x + 10;


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)

Code
my @result = sort x;


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:


Code
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:


Code
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:


Code
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)


Edit Log:
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 7:10 PM
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 7:12 PM
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 7:17 PM
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 7:17 PM
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 7:20 PM
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 7:21 PM
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 7:24 PM
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 7:36 PM
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 8:01 PM
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 8:05 PM
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 8:12 PM
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 9:09 PM
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 9:12 PM
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 9:15 PM
Post edited by 7stud (Enthusiast) on Sep 19, 2010, 9:16 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives