在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在Perl中,你可以做到:
my $test = "Hello World";
substr($test, 0, 5) = "Goodbye";
print $test;
这在其他语言中可行吗?
其他回答
在PHP中,字符串字母不能像在C中那样使用,你需要使用ord()和chr()来将数字转换为字符,反之亦然:"a" != 97,但ord("a") == 97。
不过,有一个例外:
for ($i = 'a'; $i < 'z'; $i++) {
print "$i ";
}
将打印字母a到y,就像你所期望的,就像它是C风格的数据类型一样。
但是,如果将测试条件更改为<=,它将不会像您想象的那样输出a到z,而是输出a到yz!(共打印676项)
同样,如果您将676项列表中“z”后面的“z”更改为“aa”,并再次将测试条件更改为<,您将只看到“a”被打印出来!不是像你想的那样从a到z。
如果你将增量变量改为$i+=2,它将再次只打印“a”!唯一的方法是按顺序使用$i++, $i++,现在它像预期的那样工作。
尽管如此,这是PHP中输出字母a-z组合的好方法,尽管实际使用起来非常困难。
我不敢说XML是一种编程语言,但它不是贴近我们的内心吗?: -)
在我看来,XML中最奇怪的特性是以下是一个格式良好的文档:
<_....>
</_....>
下面是允许连续点的NT-Name的词法定义。
我有点纠结:
1;
在perl中,模块需要返回true。
可能已经说过了(也许这对一些人来说并不奇怪),但我认为这很酷:
在Javascript中,声明函数接受的参数只是为了方便程序员。通过函数调用传递的所有变量都可以通过关键字“arguments”访问。所以下面会提醒“world”:
<script type="text/javascript">
function blah(){
alert(arguments[1]);
}
blah("hello", "world");
</script>
注意,虽然这些参数看起来像是存储在数组中(因为您可以以与数组元素相同的方式访问对象属性),但事实并非如此。arguments是一个对象,而不是数组(因此,它们是存储在数值索引下的对象属性),如下例所示(typeOf函数来自Crockford的JavaScript补救页面):
argumentsExample = function(){
console.log(typeOf(arguments));
anArray = [];
console.log(typeOf(anArray));
anObject = {};
console.log(typeOf(anObject));
}
function typeOf(value) {
var s = typeof value;
if (s === 'object') {
if (value) {
if (typeof value.length === 'number' &&
!(value.propertyIsEnumerable('length')) &&
typeof value.splice === 'function') {
s = 'array';
}
} else {
s = 'null';
}
}
return s;
}
argumentsExample("a", "b");
A very tiny thing that annoyed me in COBOL was that there was no dedicated modulo operation. Instead you could do a division specifying that you only wanted whole number results and store the rest in a different variable. Since COBOL is very sensitive when it comes to variables that means that you ended up with a variable you didn't really need, i.e. the actual result of the division. This is the story of how I once named a variable "USELESS" - that was the most appropriate name I could think of.