 |
| |
Perl scalar Function
NEW!!!
Check my new resource: Perl How To Tutorial eBooks
The Perl scalar function enables you to evaluate an expression in a scalar context and returns the value of that expression. This short tutorial has the meaning to point out a few cases where and how you can use this function.The syntax form of the Perl scalar function is as follows: that will return the EXPR in scalar context.One of the powers of the Perl language is the fact that it provides the variable context. A lot of functions and operators can operate either in scalar or list context. The context feature distinguishes the Perl language from other programming languages and helps you write more accurate code, closer to the human language.
Check my new How To Tutorial eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions in your scripts.
And here comes the Perl scalar function that allows you to determine the context in which you want to evaluate an expression. You don’t need to explicitly use the scalar function anytime you invoke a scalar context, but it’s usually a good thing. For example, see the following code:my @array1 = qw(one two);
my @array2 = qw(three four five);
# implicit scalar context (caused by the + operator)
print @array1 + @array2, "\n";
# explicit scalar context
print scalar @array1 + scalar @array2, "\n";
Both print function will display the number 5 which is the total number of elements of the two arrays (see also Get the size of an array example).Generally, there’s no equivalent function to invoke a list context, it suffices to put an item enclosed in round parentheses, and you have a list context. The Perl scalar function can be also assimilated with a unary operator. Be cautious if you use the expression as a parenthesized list because this will behave as a scalar comma expression (in scalar context the comma ',' operator evaluates its left argument, throws that value away, then evaluates its second argument and so on, finishing by evaluating the last element in scalar context and returning this last element). For example, in the next code snippet: my ($one, $two, $three) = (1, 2, 3);
print scalar ($one, $two, $three), "\n";
because of the comma operator used in scalar context, the print function will display the value of the last element of the list included in the parentheses, i.e. the value 3. If you want to see the Perl scalar function in action, the following examples will show you some specific ways to use it.
Let’s suppose that you have the @colors array and you need to find the number of the array elements. One way to do this is to use the more cryptic notation $#colors which means the subscript, or index of the last element of the array. So, because the first element of an array has the index 0, the size of the array (or the number of its elements) is $#colors+1.An alternative way is to use the Perl scalar function with @colors as argument; this will force the @colors to be interpreted in scalar context and it will return the size of the array. Look at the below code for more details: # initialize an array
my @colors = qw(cyan magenta white brown red);
print "The \@colors array has: ", $#colors+1, " elements\n";
# it prints: The @colors array has: 5 elements
print "The \@colors array has: ", scalar @colors, " elements\n";
# it prints: The @colors array has: 5 elements
Because scalar (@colors) – 1 is the index of the last element of the array, you can use this construct to traverse an array using foreach:my @colors = qw(cyan magenta white brown red);
foreach my $color (0..scalar @colors - 1) {
print $color + 1 . " $colors[$color]\n";
}
The output after running this code is as follows:1 cyan
2 magenta
3 white
4 brown
5 red
| Use the scalar function in conjunction with the <> operator |
As you know, you can use the diamond operator <> to read from a file; if you don’t supply any handle to the file and any argument to the script in command line, the input operator will read from <STDIN> - either from the keyboard or from a redirected file. You can use it like this: In this case, because of the scalar context forced by the $str, the <> operator will read only one line from the keyboard. If you have an array on the left side of the <> operator, Perl will read as many lines until the EOF is reached. Each line is stored as an independent element into the array: But if you precede the <> operator with the Perl scalar function like in the following line of code: the Perl interpreter will read only one line and it will store it into the @array variable; this array will have only one element – the content of the line read from <STDIN>.
Check my new How To Tutorial eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions in your scripts.
| Use the scalar function in conjunction with the localtime function |
Invoked in a list context, the localtime function returns a list. But if you invoke it in a scalar context, you’ll get a system formatted string. You can achieve this by using the Perl scalar function. If you invoke the localtime function without using the Perl scalar function, you’ll get a list of elements: ($sec, $min, $hour, $mday, $mon, $year, wday, $yday, $isdst)
where:- $sec, $min, $hour are the seconds, minutes and hours of the specified time
- $mday is the day of the month
- $mon is the month in the range 0..11
- $year is the number of years since 1900
- $wday is the day of the week with 0 indicating Sunday
- $yday is the day of the year in the range 0..364 (or 0..365 in leap years)
- $isdst is true if the specified time occurs during Daylight Saving Time, false otherwise
If you use the Perl scalar function, you’ll get a readable time format. See the following lines of code:my @time = localtime(time);
print "@time\n";
print scalar localtime(time);
A sample output could be as follows:47 19 0 21 1 110 0 51 0
Sun Feb 21 00:19:47 2010
Please click here to download the Perl scalar script with all the above examples included.
Subscribe now to my FREE newsletter (regarding tips and tricks to manipulate multidimensional arrays and hashes in Perl) and you'll receive a link to download for FREE my Perl chomp Function Tutorial eBook (a $7.50 value), it's a bonus for you and I hope you'll enjoy it. You can see its Table of Contents here (if you subscribe to my free newsletter, you don't have to pay for this eBook or use your paypal account).
Don't forget to check my new How To Tutorial eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions in your scripts.
NEW!!!
Do you want more information about the basic Perl topics?
Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :
Perl How To Tutorial eBooks
Table of Contents:
A Perl Script Install Perl Running Perl Perl Data Types Perl Variables Perl Operators Perl Lists Perl Arrays Array Size Array Length Perl Hashes Perl Statements Perl if Perl unless Perl switch Perl while Perl do-while Perl until Perl do-until Perl for Perl foreach Built-in Perl Functions Functions by Category String Functions Array Functions Functions in alphabetical order chomp chop chr crypt defined grep hex index join lc lcfirst length map oct ord pack pop push qw reverse rindex scalar shift sort splice split substr uc undef unshift
return from Perl scalar function to Perl Basics
Would you like to create your own website like this one? Hit the Alarm Clock!


|
|