Thursday, March 3, 2011

NetApp的Phone Interview惨败(二)

4. Perl里面,$_的含义是什么?

说实话,我的Perl都是自学的,工作中基本用不到。不过既然写进简历里面(号称精通Perl),就免不了要被问到。而且一问就问倒了。

以下是从Perl in A Nutshell的Section 4.4.1里面抄来的:

The most common special variable is $_, which contains the default input and pattern-searching string. For example:

foreach ('hickory','dickory','doc') {
        print;
}

The first time the loop is executed, "hickory" is printed. The second time around, "dickory" is printed, and the third time, "doc" is printed. That's because in each iteration of the loop, the current string is placed in $_ and is used by default by print. Here are the places where Perl will assume $_, even if you don't specify it:
  • Various unary functions, including functions such as ord and int, as well as the all file tests (-f, -d), except for -t, which defaults to STDIN.
  • Various list functions such as print and unlink.
  • The pattern-matching operations m//, s///, and tr/// when used without an =~ operator.
  • The default iterator variable in a foreach loop if no other variable is supplied.
  • The implicit iterator variable in the grep and map functions.  
  • The default place to put an input record when a line-input operation's result is tested by itself as the sole criterion of a while test (i.e., ). Note that outside of a while test, this does not happen.
说实话,还是看不太明白,毕竟没有真正在工作中应用过,印象不深。Interviewer看我这个粗浅的问题都答不上来,就没再往下问Perl的东西了。


5. C++里面,virtual class的作用是什么?

操,我只听说过virtual function,可从来没听说过virtual class,这下抓瞎了。听他的意思好象是用在inheritance里面,防止出现多个instance?我记不太清楚他说的答案了。

万能的Google又一次找到了答案:http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/com.ibm.xlcpp8a.doc/language/ref/cplr135.htm

其实就是多重继承时,若中间层的class共有同一个祖先,则定义中间层级的class时,将他们声明为继承virtual祖先class就可以防止底层class(从多个中间层的class继承下来)拥有多个祖先class的copy。其实,Bjarne Stroustrup大神的C++ Bible之Section 15.2.4里面也谈到了这个问题。

No comments:

Post a Comment