Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Scalar context with list and range operator

by vinoth.ree (Monsignor)
on May 28, 2013 at 04:38 UTC ( #1035518=perlquestion: print w/replies, xml ) Need Help??
vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
#!/usr/bin/env perl use strict; use warnings; my @array = ( 1..5 ); my $array_count = @array; my $scalar_context = ( 1..5 ); print "$array_count\n"; print "$scalar_context\n";

In the above code where I expected $array_count to be 5 and $scalar_context to be 5, the last element, but surprise, $scalar_context turns out to be an empty string. Instead of using range operator I tried this, my $scalar_context = ( 1, 2, 3, 4, 5); the result is 5 indeed.

Also I am getting the following error,

Use of uninitialized value $. in range (or flip) at line 8.

Is that error related to range operator in list context? Thanks in advance.

All is well

Replies are listed 'Best First'.
Re: Scalar context with list and range operator
by kcott (Canon) on May 28, 2013 at 05:06 UTC

    G'day vinoth.ree,

    All of these issues are explained in perlop - Range Operators. Quickly pointing you to the relevant parts:

    • "Binary ".." is the range operator, which is really two different operators depending on the context. In list context, it returns ..." (doco mirrors your expectations)
    • Two paragraphs down: "In scalar context, ".." returns ..." (doco explains how a false value, i.e. your 'empty string', is returned)
    • Two more paragraphs down: "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)." (explanation for your "uninitialized value $." warning)

    -- Ken

      Thanks, kcott

      In scalar context where 1..5 is no longer a range, but a flip-flop operator?

      All is well


Re: Scalar context with list and range operator (flip flop)
by Anonymous Monk on May 28, 2013 at 04:52 UTC
Re: Scalar context with list and range operator
by hdb (Prior) on May 28, 2013 at 08:13 UTC

    I have been trying a few variants and in addition to the "flip flop complication" I also got interference with print interpreting the parantheses as the function call operator.

    my $x = ( 1 .. 5 ); # scalar context, flip flop print "$x <= 1\n"; # " <= 1\n" as $x is empty string print ( 1 .. 5 ), " <= 2\n"; # "12345" but no " <= 2\n", ( ) consumed + by print print +( 1 .. 5 ), " <= 3\n"; # "12345 <= 3\n" my $y = () = ( 1 .. 5 ); # enforces list context but then assigne +d to scalar print "$y <= 4\n"; # "5 <= 4\n"
Re: Scalar context with list and range operator
by choroba (Chancellor) on May 28, 2013 at 07:10 UTC

      Oh nice!,Its Coincidence I guess, Today I was explaining the list context and scalar context assignment to my colleague, I usually write the full list like this (1,2,3,4,5), but today i tried with range operator and got this issue. I have tried for something and got something, anyhow learned flip-flop operator.

      All is well

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1035518]
Approved by kcott
Front-paged by 2teez
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2016-12-08 22:29 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (148 votes). Check out past polls.