|There's more than one way to do things|
Confused as to why the "casting context" is mis-behavingby kiz (Monk)
|on Oct 20, 2010 at 11:28 UTC||Need Help??|
kiz has asked for the
wisdom of the Perl Monks concerning the following question:
I have always understood that perl is context driven, and I'm quite happy that the "equals" assignment gets over-loaded depending on context:
I was teaching an "Introduction to Perl" course the other day, and one of the students had this code (I've paraphrased, as it were):
Now, My understanding is that the code would print "2", being the number of elements in the array-slice. it prints "d"Indeed, my slides make the point that @array[$n] is a one-element array, not a scalar, thus $b=@array[$n] will set $b to 1, not the value of the array element ($b=$array[$n])!
So, I tried forcing a scalar context on the assignement:
.... still prints "d"!Switch the array-slice around, and we still get the last element in the scalar:
prints "b"Can someone explain why?
-- Ian Stuart
A man depriving some poor village, somewhere, of a first-class idiot.