使用定义与Filehandle和while循环(Usage of defined with Filehandle and while Loop)
在阅读关于高级Perl编程(1)的书时,我遇到了这样的代码:
while (defined($s = <>)) { ...
在这里使用
defined
有什么特别的理由吗? perlop的文档说:在这些循环结构中,分配的值(无论赋值是自动的还是显式的)都会被测试以确定它是否被定义。 定义的测试避免了行中有一个字符串值被Perl视为假的问题,例如
""
或没有尾随换行符的"0"
。 如果你真的想要这样的值来终止循环,应该明确地测试它们:[...]那么,会不会有一个角落案例,或者仅仅是因为这本书太旧了,自动
defined
测试是在最近的Perl版本中添加的?
(1) 高级Perl编程,第一版,Sriram Srinivasan。 O'Reilly(1997)
While reading a book on advanced Perl programming(1), I came across this code:
while (defined($s = <>)) { ...
Is there any special reason for using
defined
here? The documentation for perlop says:In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see whether it is defined. The defined test avoids problems where line has a string value that would be treated as false by Perl, for example a
""
or a"0"
with no trailing newline. If you really mean for such values to terminate the loop, they should be tested for explicitly: [...]So, would there be a corner case or that's simply because the book is too old and the automatic
defined
test was added in a recent Perl version?
(1) Advanced Perl Programming, First Edition, Sriram Srinivasan. O'Reilly (1997)
原文:https://stackoverflow.com/questions/9621221
最满意答案
您可以即时创建对象。 任何C ++程序都会一直这样做。 这样的对象被称为临时对象。
尽管如此,你不能接受临时地址。 所以
&B(5)
是非法的。 如果您尝试编译此代码,您的编译器会告诉您。You can create an object on the fly. Any C++ program does it all the time. Such objects are called temporaries.
You cannot take an address of a temporary though. So
&B(5)
is illegal. Your compiler will tell you that if you try to compile this code.
相关问答
更多-
请问下面一段C++中arg是什么意思?[2024-02-28]
arg就是这个函数的参数 这是一个带泛型(模板)的函数,返回类型为T,参数为一个T类型的参数arg,返回的是arg的平方 arg 应该是英文 argument 的缩写 -
尝试以下方法: function myFunc() { var a, b; if (arguments.length === 1) { a = arguments[0]; console.log('no b passed'); } else if (arguments.length > 1) { a = arguments[0]; b = arguments[1]; if (b === undefined) { console.log(' ...
-
你忘了函数中的第二个int参数: template
Functor* DeferCall2(const CalleePtr& pCallee, Ret (Callee::*pFunction)(Type1, Type1), const Parm1& rParm1, const Parm2& rParm2) ... -
sender是被点击的UI元素,因此以下内容应该有效: private void TextBoxOnClick(object sender, MouseEventArgs e) { var theTextBox = sender as TextBox; if (theTextBox != null) { theTextBox.Text = string.Empty; } } as和check for null只是防御性编程。 如果您确定只能从TextBox中 ...
-
您可以即时创建对象。 任何C ++程序都会一直这样做。 这样的对象被称为临时对象。 尽管如此,你不能接受临时地址。 所以&B(5)是非法的。 如果您尝试编译此代码,您的编译器会告诉您。 You can create an object on the fly. Any C++ program does it all the time. Such objects are called temporaries. You cannot take an address of a temporary though. S ...
-
好的,谢谢@Bergi! 他的线索让我想到了javascript - 为什么不能从原型改变构造函数? 。 我在想我应该更深入地访问该对象,并且我不必复制整个构造函数 - 但我错了。 我的最终代码如下: "use strict"; var blessed = require('blessed'), contrib = require('blessed-contrib'), Node = blessed.Node, Box = blessed.Box; //... // monke ...
-
通过arg执行功能(Execute function via arg)[2022-03-30]
当然,你可以使用add作为type参数: def add(args): conn = sqlite3.connect('todo.db') c = conn.cursor() c.execute("INSERT INTO todo VALUES (args, timestamp)") parser.add_argument('-a', '--add', type=add) 如果这还不够好,你可以argparse.Action并且几乎每次遇到参数时都可以随意做任何你想做的事情。 S ... -
以下是将%x和%y转换为void*的示例 %x = alloca i32, align 4 %y = alloca i32*, align 8 store i32 123, i32* %x, align 4 store i32* %x, i32** %y, align 8 ; Convert x to void* %1 = bitcast i32* %x to i8* call void @instrument(i8* %1) ; Convert y to void* ...
-
Qt c ++字符串中的.arg错误(Qt c++ .arg error in string)[2023-05-04]
QString t = tr("Student number:%1 Module Code: %2 Mark: %3").arg(stuNum).arg(modCode).arg(mark); tr尝试查找字符串的翻译版本 - 这是用户可见字符串的好习惯。 QString t = tr("Student number:%1 Module Code: %2 Mark: %3").arg(stuNum).arg(modCode).arg(mark); tr tries to look up a transl ... -
C ++警告到typedef void(* somename)(arg1,arg2);(C++ Warning to typedef void (* somename) (arg1, arg2);)[2022-05-14]
ParameterServer::sendCallback是成员函数或方法(其类型为void (ParameterServer::*)(CORE_NS_Connection_Type, CORE_NS_Connection_Type) )因此它不能用作函数(类型为void (*)(CORE_NS_Connection_Type, CORE_NS_Connection_Type) )。 你需要使它成为一个静态成员函数: static void sendCallback (CORE_NS_Connection_ ...