With all this talk of scalar, I had to look it up because, despite using Perl heavily for twenty years, I've never actually used it! Which reminded me that I have employed the equivalent "secret" version, namely the infamous inchworm ~~ secret operator
Just a little nitpick: the "inchworm" isn't exactly equivalent, as it doesn't pass through lvalue context, which scalar does as of 5.22.
$ perlbrew exec perl -e 'for(scalar($#foo)) { $_=3 } warn "$] ".@foo."
+\n"' >/dev/null
5.034000 4
5.032001 4
5.030003 4
5.028003 4
5.026003 4
5.024004 4
5.022004 4
5.020003 0
5.018004 0
$ perlbrew exec perl -e 'for(~~$#foo) { $_=3 } warn "$] ".@foo."\n"' >
+/dev/null
5.034000 0
5.032001 0
5.030003 0
5.028003 0
5.026003 0
5.024004 0
5.022004 0
5.020003 0
5.018004 0
Update: The example above is one of the exceptions for when ~~ is not equivalent to scalar anyway:
$ perl -le 'print scalar($#foo)'
-1
$ perl -le 'print ~~$#foo'
18446744073709551615
... but the point still stands (though interestingly, my $foo="x"; for(scalar($foo)) {$_.="y"} worked even before 5.22). |