print (a..c) # this prints: abc
print ($a = "abc") # this prints: abc
print ($a = a..c); # this prints: 1E0
I would have thought it would print: abc
use strict;
print ($a = "a".."c"); # this prints 1E0
Why? Is it just my computer?
edit: I’ve got a partial answer (the range operator .. returns a boolean value in scalar context – thanks) but what I don’t understand is:
why does: print ($a = “a”…”c”) produce 1 instead of 0
why does: print ($a = “a”..”c”) produce 1E0 instead of 1 or 0
There are a number of subtle things going on here. The first is that
..is really two completely different operators depending on the context in which it’s called. In list context it creates a list of values (incrementing by one) between the given starting and ending points.Because
printinterprets its arguments in list contextIn scalar context,
..is flip-flop operator. From Range Operators in perlop:Assignment to a scalar value as in
$a = ...creates scalar context. That means that the..inprint ($a = 'a' .. 'c')is an instance of the flip-flop operator, not the list creation operator.The flip-flop operator is designed to be used when filtering lines in a file. e.g.
would print all of the lines in a file starting with the one that contained
firstand ending with the one that containedlast.The flip-flop operator has some additional magic designed to make it easy to filter based on the line number.
will print lines 10 through 20 of a file. It does this by employing special case behavior:
The strings
aandcare both constant expressions so they trigger this special case. They aren’t numbers, but they’re used as numbers (==is a numeric comparison). Perl will convert scalar values between strings and numbers as needed. In this case, both values nummify to 0. ThereforeWe’re getting close to the bottom of the mystery. On to the next bit. More from perlop:
If you haven’t read any lines from a file yet,
$.will beundefwhich is0in a numerical context.0 == 0is true, so the..returns a true value. It’s the first true value, so it’s1. Because both the left-hand and right-hand sides are true the first true value is also the last true value and theE0“this is the last value” suffix is appended to the return value. That is whyprint ($a = 'a' .. 'c')prints1E0. If you were to set$.to a non-zero value the..would be false and return the empty string.The very final piece of the puzzle (and I might be going too far now) is that the assignment operator returns a value. In this case that’s the value assigned to
$a1 —1E0. This value is what is ultimately spit out by theprint.1: Technically, the assignment produces a lvalue for the item assigned to. i.e. it returns an lvalue for the variable
$awhich then evaluates to1E0.