I'm a Perl newbie. I want to understand Perl context in conjunction with range operator. This is my code.
use strict;
use warnings;
my $asc = ( 10 .. 50 );
print "$asc\n";
I have two doubts.
If the expression ( 10 .. 50 ) returns an array, then, as it's a scalar context, "asc" variable should be assigned the length of the array, that is, 41.
If expression ( 10 ..50 ) returns a list, then, as it's a scalar context, "asc" variable should be assigned the last item from the list, that is, 50.
But, I get the following shout ..
Use of uninitialized value in range (or flip) at main.pl line ..
Appreciate and welcome any guide.
In Perl, range operator is used for creating the specified sequence range of specified elements. So this operator is used to create a specified sequence range in which both the starting and ending element will be inclusive. For example, 7 .. 10 will create a sequence like 7, 8, 9, 10.
The range operator is used as a shorthand way to set up arrays. When used with arrays, the range operator simplifies the process of creating arrays with contiguous sequences of numbers and letters. We'll start with an array of the numbers one through ten.
Just as with the =~ regex match operator, the left side is the "subject" of the match, and the right side is the "pattern" to match against -- whether that be a plain scalar, a regex, an array or hash reference, a code reference, or whatever.
q operator in Perl can be used in place of single quotes. It uses a set of parentheses to surround the string. Returns: a single-quoted string. Note: Any set of delimiters can be used, not just the parentheses.
You're working with the Range Operator ..
in an scalar context, which is otherwise known as the flip-flop operator.
You should read the entire documentation, but the following excerpts are relevant to your situation:
In scalar context, "
..
" returns a boolean value. The operator is bistable, like a flip-flop, and emulates the line-range (comma) operator of sed, awk, and various editors....
If either operand of scalar "
..
" is a constant expression, that operand is considered true if it is equal (==
) to the current input line number (the$.
variable).
The “exact“ error message explains what's going on:
Use of uninitialized value $. in range (or flip)
Basically, Perl interprets this usage as a flip/flop test.
It's testing if the current line number $.
is equal to the integer values you specified:
my $asc = ( $. == 10 .. $. == 50 );
However, because you haven't read from a file handle, the $.
variable is uninitialized and throws a warning.
It is possible to get the list context behavior that you described, but you'll need to adjust the code to make your intent more explicit:
my $count = () = (10..50); # Forces a list context
my $last_element = (10..50)[-1]; # Also forces a list context
print "$count\n";
print "$last_element\n";
Outputs:
41
50
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With