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:
"scalar " confused

 



sangfroid
Novice

Sep 19, 2010, 1:30 PM

Post #1 of 5 (673 views)
"scalar " confused Can't Post

Hi , I wrote a small perl prog. which goes like this




Quote


#!/bin/perl -w
print scalar localtime();
printf "\n";
print localtime();



How the keyword "scalar" converts all those timefields and prints time in human readable format ? What is scalar doing here ???


BillKSmith
Veteran

Sep 19, 2010, 1:53 PM

Post #2 of 5 (669 views)
Re: [sangfroid] "scalar " confused [In reply to] Can't Post

The keywork "scalar" forces the function "localtime()" to execute in scalar context. According to the documentation for localtime() (perldoc -f localtime):


Quote
In scalar context, "localtime()" returns the ctime(3) value:

$now_string = localtime; # e.g., "Thu Oct 13 04:54:34 1994"

Good Luck,
Bill


sangfroid
Novice

Sep 19, 2010, 2:05 PM

Post #3 of 5 (668 views)
Re: [BillKSmith] "scalar " confused [In reply to] Can't Post

what does "scalar context" actually imply ??


BillKSmith
Veteran

Sep 19, 2010, 2:50 PM

Post #4 of 5 (667 views)
Re: [sangfroid] "scalar " confused [In reply to] Can't Post

"Scalar" is a perl data type. "Scalar context" means that perl expects the function to return a single scalar datum. (Refer to the 'Context' and 'Scalar Values' sections of perldoc perldata)
Good Luck,
Bill


7stud
Enthusiast

Sep 19, 2010, 7:09 PM

Post #5 of 5 (662 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)

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)

 
 


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

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