Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Perl context with range operator?

Tags:

perl

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.

  1. 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.

  2. 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.

like image 354
Rabin Halder Avatar asked Aug 07 '14 06:08

Rabin Halder


People also ask

What is range operator in Perl?

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.

What is the range operator?

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.

What does ~~ mean in Perl?

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.

What is the purpose of Q operator?

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.


1 Answers

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.

Achieving a List Context

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
like image 193
Miller Avatar answered Oct 25 '22 19:10

Miller